1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const int MAXN = 30; int p, q; bool vis[MAXN][MAXN]; int dir[8][2] = {{-1, -2}, {1, -2}, {-2, -1}, {2, -1}, {-2, 1}, {2, 1}, {-1, 2}, {1, 2}};
bool dfs(int x, int y, int step, string res) { if (step == p * q) { cout << res << "\n"; return true; } for (int i = 0; i < 8; ++i) { int nx = x + dir[i][0], ny = y + dir[i][1]; if (nx < 0 || nx >= p || ny < 0 || ny >= q || vis[nx][ny]) continue; char r = nx + '1', c = ny + 'A'; vis[nx][ny] = 1; if (dfs(nx, ny, step + 1, res + c + r)) return true; vis[nx][ny] = 0; } return false; }
|