-
总时间限制:
- 1000ms 内存限制:
- 65536kB
-
描述
-
Background
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.
输入
- The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . . 输出
-
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 a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.
样例输入
-
3 1 1 2 3 4 3
样例输出
-
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
思路:这是一个简单的搜索题,马是按国际象棋的走法,即“两横一竖”或“两竖一横”,则马一共有八种走法,在代码中用move函数表示。move的走法,按照题中给出的字典顺序进行搜索,即b-2,b-1……。只要按字典顺序搜索,则找到的第一条路径就是符合题目要求的输出,不需要按照回溯方法遍历整个图。递归时,首先判断是否已经找到路径,再判断该路径是否已经走过,然后判断马是否走出边界。然后根据上未走的格子数判断马是否已经走完全部格子,若全部走完,则输出返回,否则将走过的该格子填入vector中,并进行相应处理,然后继续递归,最后根据回溯的方法,返回操作。
注意:
1.这里要注意输出是需要按字典顺序的,即不需要遍历搜索整个图,只需要找到按字典顺序输出的第一条记录即可,这就需要安排好搜索的路径,否则还要对输出进行排序,增加时间。
2.进行数组操作的时候注意不要产生runtime error。
代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
char qletter[27]={'0','A','B','C','D','E','F','G','H','I','J',
<span style="white-space:pre"> </span>'K','L','M','N','O','P','Q','R','S','T','U',
<span style="white-space:pre"> </span>'V','W','X','Y','Z'};
int CHESS[27][27];
int totalN,p,q;
char buf[10];
vector<string> result;
bool isfind;
void clear(int ix, int jy){
<span style="white-space:pre"> </span>++ix;
<span style="white-space:pre"> </span>++jy;
<span style="white-space:pre"> </span>for (int i = 0; i < ix; ++i)
<span style="white-space:pre"> </span>for(int j = 0; j < jy; ++j)
<span style="white-space:pre"> </span>CHESS[i][j] = 0;
}
void move(int a, int b){
<span style="white-space:pre"> </span>if (isfind)
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>if (CHESS[a][b])
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>if (a < 1 || b < 1 || a > p || b > q)
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>CHESS[a][b] = 1;
<span style="white-space:pre"> </span>--totalN;
<span style="white-space:pre"> </span>sprintf(buf, "%c%d", qletter[b], a);
<span style="white-space:pre"> </span>string string_buf(buf);
<span style="white-space:pre"> </span>result.push_back(string_buf);
<span style="white-space:pre"> </span>if (totalN == 0){
<span style="white-space:pre"> </span>isfind = true;
<span style="white-space:pre"> </span>for (vector<string>::iterator it = result.begin(); it != result.end(); ++it){
<span style="white-space:pre"> </span>cout << *it;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << endl;<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//result.pop_back();
<span style="white-space:pre"> </span>//CHESS[a][b] = 0;
<span style="white-space:pre"> </span>//++totalN;
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>move(a-1, b-2);
<span style="white-space:pre"> </span>move(a+1, b-2);
<span style="white-space:pre"> </span>move(a-2, b-1);
<span style="white-space:pre"> </span>move(a+2, b-1);
<span style="white-space:pre"> </span>move(a-2, b+1);
<span style="white-space:pre"> </span>move(a+2, b+1);
<span style="white-space:pre"> </span>move(a-1, b+2);
<span style="white-space:pre"> </span>move(a+1, b+2);
<span style="white-space:pre"> </span>result.pop_back();
<span style="white-space:pre"> </span>CHESS[a][b] = 0;
<span style="white-space:pre"> </span>++totalN;
}
void search(int p, int q){
<span style="white-space:pre"> </span>move(1,1);
<span style="white-space:pre"> </span>if (isfind == false)
<span style="white-space:pre"> </span>cout << "impossible" << endl;
}
int main(){
<span style="white-space:pre"> </span>int n;
<span style="white-space:pre"> </span>int num = 1;
<span style="white-space:pre"> </span>cin >> n;
<span style="white-space:pre"> </span>while (n--){
<span style="white-space:pre"> </span>cin >> p >> q;
<span style="white-space:pre"> </span>isfind = false;
<span style="white-space:pre"> </span>totalN = p*q;
<span style="white-space:pre"> </span>clear(p, q);
<span style="white-space:pre"> </span>cout << "Scenario #"<<num<<":\n";
<span style="white-space:pre"> </span>search(p, q);
<span style="white-space:pre"> </span>result.clear();
<span style="white-space:pre"> </span>++num;
<span style="white-space:pre"> </span>cout << "\n";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}