POJ 2488 A Knight's Journey (DFS), poj2488
The question shows the size of the Board. The server guard can start from anywhere and can only use the word "day. The result is output in Lexicographic Order without repeating the vertex. Because A1 has the smallest Lexicographic Order, the requested path must start with A1.
There is no colon in the output, and it takes several times to enter WA. Pay attention to it later.
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; int a, B; bool visit [30] [30]; char path [30] [5]; int dx [10] = {-,-}; // eight directions, int dy [10] = {-2,-2,-1,-, 1, 2}; bool dfs (int x, int y, int num) {if (num = a * B) return true; for (int I = 0; I <8; I ++) {int nx = x + dx [I]; int ny = y + dy [I]; if (nx> 0 & nx <= a & ny> 0 & ny <= B & visit [nx] [ny] = false) {path [num + 1] [0] = nx + '0'; path [num + 1] [1] = ny + 'a'-1; visit [nx] [ny] = true; if (dfs (nx, ny, num + 1) return true; visit [nx] [ny] = false ;}} return false;} int main () {// freopen ("in.txt", "r", stdin); int n; scanf ("% d", & n ); for (int j = 1; j <= n; j ++) {scanf ("% d", & a, & B); memset (visit, 0, sizeof (visit); memset (path, 0, sizeof (path); path [1] [0] = '1 '; path [1] [1] = 'a'; visit [1] [1] = true; printf ("Scenario # % d: \ n", j ); if (dfs (1, 1) for (int I = 1; I <= a * B; I ++) printf ("% c ", path [I] [1], path [I] [0]); else printf ("impossible"); printf ("\ n");} return 0 ;}