POJ - 2488 A Knight's Journey
来源:互联网 发布:手机淘宝能收藏店铺吗 编辑:程序博客网 时间:2024/06/03 13:29
A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
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.
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
31 12 34 3
Sample Output
Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4
#include<iostream>#include<string>#include<cstring>#include<algorithm>#include<stack>using namespace std; //很经典的dfs题,需要记录合法路径,用栈。struct node{char c; //要求输出路径的字典序最小,这与搜索的方向先后有关。int r; //细心,恒心,注意细节就会做出来。};int p, q;int xt, yt;int mark[30][30];bool isillegal(int x, int y) {if (x >= 1 && x <= p &&y >= 1 && y <= q && !mark[x][y]) return true;else return false;}void move(int casen, int x, int y){switch (casen){case 1:{ xt = x - 1; yt = y - 2; break; }case 2:{ xt = x + 1; yt = y - 2; break; }case 3:{ xt = x - 2; yt = y - 1; break; }case 4:{ xt = x + 2; yt = y - 1; break; }case 5:{ xt = x - 2; yt = y + 1; break; }case 6:{ xt = x + 2; yt = y + 1; break; }case 7:{ xt = x - 1; yt = y + 2; break; }case 8:{ xt = x + 1; yt = y + 2; break; }}return;}stack<node>stk;bool dfs(int step, int x, int y){mark[x][y] = 1;node u;u.r = x;u.c = 'A' + y - 1;stk.push(u);if (step == p*q)return true;for (int i = 1; i <= 8; i++){move(i, x, y);if (isillegal(xt, yt)){if (dfs(step + 1, xt, yt))return true;}}stk.pop();mark[x][y] = 0;return false;}int main(){int casen;cin >> casen;for (int cas = 1; cas <= casen; cas++){cin >> p >> q;int ok = 0;memset(mark, 0, sizeof(mark));for (int i = 1; i <= p; i++){for (int j = 1; j <= q; j++){if (dfs(1, i, j)){ok = 1; break;}}if (ok) break;}if (cas != 1) cout << endl;cout << "Scenario #" << cas << ":" << endl;if (ok){int i = 0;node ans[900];while (!stk.empty()){ans[i++] = stk.top();stk.pop();}for (int j = i - 1; j >= 0; j--)cout << ans[j].c << ans[j].r;cout << endl;}else cout << "impossible" << endl;}}
0 0
- 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 (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
- 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
- POJ 2488(A Knight's Journey)
- POJ 2488 A Knight's Journey
- POJ-2488-A Knight's Journey
- POJ - 3278 Catch That Cow
- CodeForces - 244B Undoubtedly Lucky Numbers
- HDU - 1557 权利指数
- HDU - 3023 Dirt
- CodeForces - 202A LLPS
- POJ - 2488 A Knight's Journey
- HDU - 1009 FatMouse' Trade
- HDU - 1052 Tian Ji -- The Horse Racing
- HDU - 1053 Entropy
- HDU - 2037 今年暑假不AC
- 数据结构和算法经典100题-第2题
- POJ - 1328 Radar Installation
- POJ - 3253 Fence Repair
- CodeForces - 484D Kindergarten