pku 2488 A Knight's Journey (搜索 DFS)
A Knight's Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28697 | Accepted: 9822 |
Description
![](http://poj.org/images/2488_1.jpg)
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
#include <cstdio> #include <cstdlib> #include <stack> #include <cstring> using namespace std; const int MAX = 9; const int dirx[8]={-1,1,-2,2,-2,2,-1,1},diry[8]={-2,-2,-1,-1,1,1,2,2}; typedef struct Point{ int x,y; }point; int p,q,n; bool visit[MAX][MAX]; point pre[MAX][MAX]; bool mark; stack<int> stx,sty; void printPath(int x,int y){ stx.push(x); sty.push(y); int tx,ty; tx = pre[x][y].x; ty = pre[x][y].y; while(tx!=-1){ stx.push(tx); sty.push(ty); x = pre[tx][ty].x; y = pre[tx][ty].y; tx = x; ty = y; } while(!stx.empty()){ printf("%c%d",sty.top()-1+'A',stx.top()); stx.pop(); sty.pop(); } printf("\n\n"); } void dfs(int x,int y,int len){ if(mark)return; if(len==p*q){ printPath(x,y); mark = true; return; } int i,tx,ty; for(i=0;i<8;++i){ tx = x+dirx[i]; ty = y+diry[i]; if(tx<1 || tx>p || ty<1 || ty>q)continue; if(visit[tx][ty])continue; pre[tx][ty].x = x; pre[tx][ty].y = y; visit[tx][ty] = true; dfs(tx,ty,len+1); visit[tx][ty] = false; } } int main() { //freopen("in.txt","r",stdin); //(Author : CSDN iaccepted) int i; scanf("%d",&n); for(i=1;i<=n;++i){ printf("Scenario #%d:\n",i); scanf("%d %d",&p,&q); memset(visit,0,sizeof(visit)); mark = false; pre[1][1].x = -1; pre[1][1].y = -1; visit[1][1] = true; dfs(1,1,1); visit[1][1] = false; if(!mark){ printf("impossible\n\n"); } } return 0; }
题目意思:象棋中的马在一张棋盘上是否能不反复的走全然部格子。假设能走完输出走的路径(以字典序),假设没有一种走法能达到这种目标,则输出impossible。
思路就是DFS 搜下去,当走过的格子数达到格子总数时就打印路径。所以要用一个数组记录每一个定点的前驱节点。