POJ 2488 A Knight's Journey(DFS)
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 55239 | Accepted: 18788 |
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
Source
TUD Programming Contest 2005, Darmstadt, Germany
题意:
给定一个棋盘N*M(1<=N*M<=26),从任意位置出发,如果有路径能够把棋盘上每一个点走一遍,输出字典序最小的路径,如果没有输出impossible。
解题思路:
给的列使用字母表示,行用数字表示,
要想使得找到的路径字母序是最小的,说明要列优先查找
搜索的方向也有限制:应该从出发点最左上的方向先找,然后左下,右上,右下
故方向数组Move[8][2] = {{-1,-2},{1,-2},{-2,-1},{-2,1},{-2,1},{2,1},{-1,2},{1,2}}
剪枝:
1.过滤掉访问过的,出界的格子
2.如果在搜索过程中找到路径直接返回//因为是列优先查找并且搜索的方向也是有大小顺序的,第一次找到的路径一定是字母序最小的
#include <iostream> #include<cstdio> using namespace std; int N,M; int Move[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; int visit[26][26];//访问标志 1*26,26*1 int a[27][2];//记录路径 bool check(int x,int y){//检查是否走过或者出界 return !visit[x][y]&&x>=0&&x<N&&y>=0&&y<M; } char convert(int num){ return char(num+65); } bool DFS(int x,int y,int time){ if(time == N*M) {//说明整个棋盘都遍历结束了 return true; } bool flag = false; for(int i=0;i<8;i++){ int nx = x + Move[i][0]; int ny = y + Move[i][1]; if(check(nx,ny)){ visit[nx][ny] = 1; a[time+1][0] = nx; a[time+1][1] = ny; flag = DFS(nx,ny,time+1); if(flag) return flag;//若过找到提前直接返回 visit[nx][ny] = 0; } } return flag; } //行是数字,列是字母 int main(){ int Case; scanf("%d",&Case); int first = 1; for(int k = 1;k<=Case;k++){ if(first){ first = 0; }else{ printf("\n"); } scanf("%d%d",&N,&M); for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ visit[i][j] = 0; } } bool flag = false; for(int j=0;j<M;j++){//列优先搜索 for(int i=0;i<N;i++){ if(!visit[i][j]){ visit[i][j] = 1; a[1][0] = i; a[1][1] = j; flag = DFS(i,j,1); if(flag) break; visit[i][j] = 0; } } if(flag) break; } printf("Scenario #%d:\n",k); if(flag){ for(int i=1;i<=N*M;i++){ printf("%c%d",convert(a[i][1]),a[i][0]+1); } } else printf("impossible"); printf("\n"); } return 0; }

浙公网安备 33010602011771号