poj 2488 A Knight's Journey
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 19356 | Accepted: 6515 |
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
这题思路不难,就是深搜加回溯
但是我把行列搞错了 导致wa了好几次,还是需要仔细读题啊。。。
还有一个关键点在于输出的结果需要时字典序中的第一个,但是把所有结果都找出来再排序就太麻烦了,所以在搜索的时候先规定好方向,得到的结果就是字典序了
这一点也是参考别人的代码的
解决这两点剩下的就很简单了
#include<iostream> using namespace std; int MAX; int map[30][30]; int resx[1000]; int resy[1000]; int flag; int p, q; int movey[8] = {-1,1,-2,2,-2,2,-1,1}; int movex[8] = {-2,-2,-1,-1,1,1,2,2}; int judge(int a, int b) { if(a<0||b<0||a>=p||b>=q) return 0; return 1; } void dfs(int num, int cur, int x, int y) { resx[cur] = x; resy[cur] = y; if(flag) return; if(num == MAX) { for(int i=0;i<MAX;i++) { cout<<char(resx[i]+'A')<<resy[i]+1; } cout<<endl; flag = 1; return; } for(int j=0;j<8;j++) { int a = x+movex[j]; int b = y+movey[j]; if(judge(a, b) && map[a][b]==0) { map[a][b]=1; dfs(num+1,cur+1,a,b); map[a][b]=0; } } } int main() { int n; int i, j; freopen("e:\\data.txt", "r", stdin); freopen("e:\\out.txt", "w", stdout); cin>>n; for(int k=1; k<=n;k++) { cin>>q>>p; MAX = p*q; cout<<"Scenario #"<<k<<":"<<endl; memset(map, 0, sizeof(map)); flag=0; map[0][0]=1; dfs(1,0,0,0); if(flag==0) cout<<"impossible"<<endl; cout<<endl; } return 0; }
浙公网安备 33010602011771号