编程介的小学生 2016-12-29 15:15 采纳率: 20.5%
浏览 1057
已采纳

Traveling Queen Problem

Description

Black has been defeated and the white army has won, but unfortunately the white king has been killed in the fight, and so the white queen is looking for a new mate. She is unsure whom of the knights to marry and has decided to visit them all. Afterwards she plans to see the bishop to arrange for the marriage.

Given a chessboard with the current situation, find the shortest number of moves such that the queen visits every knight and, finally, visits the bishop.

The queen visits by standing on one of the (at most) eight neighbouring squares and she does not necessarily have to move between two visits. For each move the queen can go an arbitrary number of squares in one of the eight directions (horizontal, vertical or diagonal). No move may pass through or stop at a non-empty square.

Input

The first line contains the number of scenarios. Each scenario consists of a chessboard description. The rows 8, …, 1 are given in this order, one line per row. Each line contains 8 characters to represent the squares at columns a, …, h of this row. Each description is followed by an empty line.

There is one character Q to denote the starting position of the queen, and one B to denote the square on which the bishop stands. There is an arbitrary number of pawns, given as P, who simply block movement, as well as 2–14 knights denoted as N. All other squares are given as ‘.’ and are empty.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1.

Then print the lexicographical first path that contains the minimal number of moves, ends adjacent to the bishop and visits each knight at least once. The path shall be given on a single line by concatenating in order the names of the squares on which the queen stands. Each square name consists of a small letter followed by a decimal digit. If no such path exists, output “impossible” on a single line. Terminate the output for the scenario with a blank line.

Sample Input

2
.......Q
...P.P..
...PNP..
..NP.P..
........
........
..B.....
........

B.P.....
..P.....
PPP..N..
........
........
.N...Q..
........
........
Sample Output

Scenario #1:
h8h2e5d4b2

Scenario #2:
impossible

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-12-29 15:43
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题