POJ-2488-A Knight's Journey
2016-07-29 08:05
405 查看
A Knight's Journey
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.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40833 | Accepted: 13882 |
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.
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4 // 马的走法很恶心,横坐标按从小到大的顺序,横坐标相同纵坐标从小到大#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int n, m, t; int Map[100][100]; int tx[]= { -2,-2,-1, -1,1,1,2,2}; int ty[]= { -1,1,-2,2,-2,2,-1,1}; int flag; int p,top; struct node { int x; int y; }way[10010]; void dfs(int x, int y,int s) { if(s==n*m) { flag=1; return ; } for(int i = 0; i<8; ++i) { int xx =x+tx[i]; int yy =y+ty[i]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!Map[xx][yy]) { Map[xx][yy] = 1; way[top].x = xx; way[top].y = yy; ++top; dfs(xx, yy, s+1); if(flag)return; --top; Map[xx][yy] = 0; } } } int main() { scanf("%d", &t); for(int tt = 1; tt<=t ;++tt) { scanf("%d%d", &m, &n); memset(Map, 0,sizeof(Map)); printf("Scenario #%d:\n",tt); flag = 0; top = 1; way[0].x = 1; way[0].y = 1; Map[1][1] = 1; dfs(1,1,1); if(!flag)printf("impossible\n"); else { for(int i = 0; i<top;++i) printf("%c%d",way[i].x+'A'-1,way[i].y); printf("\n"); } printf("\n"); } return 0; }
相关文章推荐
- 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(搜索)
- poj2488 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【dfs】
- 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 http://poj.org/problem?id=2488
- POJ - 2488 A Knight's Journey (DFS)
- POJ2488 A Knight's Journey(DFS)
- 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 DFS