POJ 2488 A Knight's Journey__深搜
2016-12-04 20:17
519 查看
Description
BackgroundThe 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.
Sample Input
31 1
2 3
4 3
Sample Output
Scenario #1:A1
Scenario #2:
impossible
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
分析
题目大意:骑士要在国际象棋上走,见描述中的图,骑马只能走日字,问有无办法每个点只走一次走完全部棋盘。只用搜索一种可行方案呗,那就从第一个点出发向8个方向搜索,棋盘坐标横向为字母A开始,纵向为数字1开关,需要注意的是搜索的方法要按字典序来,横向字母从小到大加纵向数字从小到大。实现
#include <iostream> #include <cstring> #define PATH_NUM 51 using namespace std; int visited[26][26]; char path[PATH_NUM][2]; //方向只能是这个顺序,具体看*分析*节描述。 int to[8][2] = { { -2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1} }; int cases, m, n, maxStep; bool success = false; //从(r, c)点开始再次跳跃,搜索下一个落脚点,当前走了step步。 void dfs(int r, int c, int step) { if (success) return; path[step][0] = r + '1'; path[step][1] = c + 'A'; if (step == maxStep) { success = true; return; } for (int i = 0; i < 8; i++) { int newR = r + to[i][1]; int newC = c + to[i][0]; if ((newC >= 0 && newC < n) && (newR >= 0 && newR < m) && !visited[newR][newC]) { visited[newR][newC] = 1; dfs(newR, newC, step+1); visited[newR][newC] = 0; } } } int main() { // freopen("in.txt", "r", stdin); cin >> cases; int index = 0; while(cases--) { cin >> m >> n; maxStep = m * n; memset(visited, 0, sizeof(visited)); memset(path, 0, sizeof(path)); success = false; index++; visited[0][0] = 1; dfs(0, 0, 1); cout << "Scenario #" << index << ":" << endl; if (success) { for(int i = 1; i <= maxStep; i++) { cout << path[i][1] << path[i][0]; } }else { cout << "impossible"; } cout << endl << endl; } return 0; }
相关文章推荐
- poj 2488 A Knight's Journey http://poj.org/problem?id=2488
- POJ - 2488 A Knight's Journey (DFS)
- poj 2488 A Knight's Journey
- Poj 2488 A Knight's Journey(Dfs)
- POJ - 2488 A Knight's Journey
- poj 2488 A Knight's Journey
- POJ_2488: A Knight's Journey
- POJ-2488-A Knight's Journey
- POJ - 2488 A Knight's Journey (DFS)
- poj2488——A Knight's Journey
- POJ 2488 A Knight's Journey
- POJ 2488 A Knight's Journey
- POJ 2488 A Knight's Journey
- POJ 2488 A Knight's Journey
- POJ 2488 A Knight's Journey
- POJ 2488 A Knight's Journey DFS
- POJ 题目 2488 A Knight's Journey(dfs)
- A Knight's Journey(POJ_2488)
- 搜索 -dfs-POJ-2488-A Knight's Journey
- POJ 2488 A Knight's Journey --- DFS