标签:des style blog http color os strong io
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30388 | Accepted: 10404 |
Description
Input
Output
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
题目大意: 让骑士以字典序遍历整个图,每个点只能到一次。
思路:数据不大,直接搜索即可。注意每个测试数据后面都有一个空行。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : AKnightsJourney.cpp 4 * Creat time : 2014-07-31 16:22 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 30 15 using namespace std; 16 struct Node 17 { 18 int x,y; 19 }steps[M*M]; 20 bool vis[M][M]; 21 int p,q; 22 int dir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; 23 int DFS(int x,int y,int cnt) 24 { 25 if(vis[x][y]){ 26 return -1; 27 } 28 vis[x][y] = true; 29 steps[cnt].x = x; steps[cnt].y = y; 30 if(cnt == p*q-1) return 1; 31 for(int i = 0; i < 8; i++){ 32 int xx = x+dir[i][0]; 33 int yy = y+dir[i][1]; 34 if(xx >= 1 && xx <= p && yy >= 1 && yy <= q){ 35 int ans = DFS(xx,yy,cnt+1); 36 if(ans == 1){ 37 return true; 38 } 39 else if(ans == 0){ 40 vis[xx][yy] = false; 41 } 42 } 43 } 44 return 0; 45 } 46 int main(int argc,char *argv[]) 47 { 48 int n,t = 1; 49 scanf("%d",&n); 50 while(n--){ 51 clr(steps,0); 52 scanf("%d%d",&p,&q); 53 int cnt = 0; 54 printf("Scenario #%d:\n",t++); 55 int flag = 0; 56 for(int i = 1; i <= q; i++){ 57 for(int j = 1; j <= p; j++){ 58 clr(vis,0); 59 if(DFS(j,i,cnt) == 1){ //因为i与j位置反了,贡献了几次wr 60 flag = 1; 61 break; 62 } 63 } 64 if(flag) break; 65 } 66 if(!flag){ 67 printf("impossible"); 68 } 69 else{ 70 for(int i = 0; i < q*p; i++){ 71 printf("%c%d",steps[i].y-1+‘A‘,steps[i].x); 72 } 73 } 74 printf("\n\n"); 75 } 76 return 0; 77 }
poj 2488 -- A Knight's Journey,布布扣,bubuko.com
poj 2488 -- A Knight's Journey
标签:des style blog http color os strong io
原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3885090.html