POJ 2488 DFS
Posted liylho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2488 DFS相关的知识,希望对你有一定的参考价值。
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 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
|
代码:
1 //#include "bits/stdc++.h" 2 #include "cstdio" 3 #include "map" 4 #include "set" 5 #include "cmath" 6 #include "queue" 7 #include "vector" 8 #include "string" 9 #include "cstring" 10 #include "time.h" 11 #include "iostream" 12 #include "stdlib.h" 13 #include "algorithm" 14 #define db double 15 #define ll long long 16 #define vec vector<ll> 17 #define Mt vector<vec> 18 #define ci(x) scanf("%d",&x) 19 #define cd(x) scanf("%lf",&x) 20 #define cl(x) scanf("%lld",&x) 21 #define pi(x) printf("%d\n",x) 22 #define pd(x) printf("%f\n",x) 23 #define pl(x) printf("%lld\n",x) 24 #define rep(i, x, y) for(int i=x;i<=y;i++) 25 const int N = 1e6 + 5; 26 const int mod = 1e9 + 7; 27 const int MOD = mod - 1; 28 const db eps = 1e-18; 29 const db PI = acos(-1.0); 30 using namespace std; 31 int n,m,ok=0; 32 int dx[8]={-2,-2,-1,-1,1,1,2,2}; 33 int dy[8]={-1,1,-2,2,-2,2,-1,1}; 34 bool v[30][30]; 35 struct P 36 { 37 int x,y; 38 }; 39 P a[N]; 40 int R() 41 { 42 int x=0,f=1;char ch=getchar(); 43 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 44 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 45 return x*f; 46 } 47 void dfs(int x,int y,int cnt,int ii) 48 { 49 v[x][y]=1;//(1) 50 a[cnt].x=x,a[cnt].y=y;//(1) 51 if(cnt==n*m&&!ok) 52 { 53 ok=1; 54 printf("Scenario #%d:\n",ii); 55 for(int i=1;i<=cnt;i++){ 56 char e=(a[i].x+‘A‘-1); 57 printf("%c%d",e,a[i].y); 58 } 59 puts(""); 60 puts(""); 61 return; 62 } 63 for(int i=0;i<8;i++) 64 { 65 int xx=x+dx[i],yy=y+dy[i]; 66 if(xx<=0||yy<=0||xx>m||yy>n||v[xx][yy]!=0) continue; 67 /*------(1)不能放在这里?------*/ 68 69 dfs(xx,yy,cnt+1,ii); 70 v[xx][yy]=0; 71 } 72 } 73 int main() 74 { 75 int t; 76 t=R(); 77 for(int ii=1;ii<=t;ii++) 78 { 79 memset(v,0, sizeof(v)); 80 n=R();m=R(); 81 ok=0; 82 dfs(1,1,1,ii); 83 if(!ok){ 84 printf("Scenario #%d:\n",ii); 85 puts("impossible"); 86 puts(""); 87 } 88 89 } 90 return 0; 91 }
以上是关于POJ 2488 DFS的主要内容,如果未能解决你的问题,请参考以下文章
POJ2488A Knight's Journey[DFS]
POJ 2488-A Knight's Journey(DFS)