題目鏈接:http://poj.org/problem?id=2488
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 36695 | Accepted: 12462 |
Description
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.
Input
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, . . .
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 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.
If no such path exist, you should output impossible on a single line.
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
題目大意: 任選一個起點,按照國際象棋馬的跳法,不重復的跳完整個棋盤,如果有多種路線則選擇字典序最小的路線(路線是點的橫縱坐標的集合,注意棋盤的橫坐標的用大寫字母,縱坐標是數字)
題目分析:
1. 應該看到這個題就可以想到用DFS,當首先要明白這個題的意思是能否只走一遍(不回頭不重復)將整個地圖走完,而普通的深度優先搜索是一直走,走不通之后沿路返回到某處繼續深搜。所以這個題要用到的回溯思想,如果不重復走一遍就走完了,做一個標記,算法停止;否則在某種DFS下走到某一步時按馬跳的規則無路可走而棋盤還有為走到的點,這樣我們就需要撤消這一步,進而嘗試其他的路線(當然其他的路線也可能導致撤銷),而所謂撤銷這一步就是在遞歸深搜返回時重置該點,以便在當前路線走一遍行不通換另一種路線時,該點的狀態是未訪問過的,而不是像普通的DFS當作已經訪問了。
2. 如果有多種方式可以不重復走一遍的走完,需要輸出按字典序最小的路徑,而注意到國際象棋的棋盤是列為字母,行為數字,如果能夠不回頭走一遍的走完,一定會經過A1點,所以我們應該從A1開始搜索,以確保之后得到的路徑字典序是最小的(也就是說如果路徑不以A1開始,該路徑一定不是字典序最小路徑),而且我們應該確保優先選擇的方向是字典序最小的方向,這樣我們最先得到的路徑就是字典序最小的。
參考代碼:
#include <cstdio> #include <cstring> using namespace std; const int MAX_N = 27; //字典序最小的行走方向 const int dx[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; const int dy[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; bool visited[MAX_N][MAX_N]; struct Step{ char x, y; } path[MAX_N]; bool success; //是否成功遍歷的標記 int cases, p, q; void DFS(int x, int y, int num); int main() { scanf("%d", &cases); for (int c = 1; c <= cases; c++) { success = false; scanf("%d%d", &p, &q); memset(visited, false, sizeof(visited)); visited[1][1] = true; //起點 DFS(1, 1, 1); printf("Scenario #%d:\n", c); if (success) { for (int i = 1; i <= p * q; i++) printf("%c%c", path[i].y, path[i].x); printf("\n"); } else printf("impossible\n"); if (c != cases) printf("\n"); //注意該題的換行 } return 0; } void DFS(int x, int y, int num) { path[num].y = y + 'A' - 1; //int 轉為 char path[num].x = x + '0'; if (num == p * q) { success = true; return; } for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 < nx && nx <= p && 0 < ny && ny <= q && !visited[nx][ny] && !success) { visited[nx][ny] = true; DFS(nx, ny, num+1); visited[nx][ny] = false; //撤銷該步 } } }
