码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2488 A Knight's Journey(DFS)

时间:2015-10-03 16:53:17      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

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

题意:给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径。

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 using namespace std;
 7 const int maxn=26;
 8 int n,p,q;
 9 int dir[8][2]= {-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};//记录8个方向
10 bool flag,vis[maxn][maxn];//用于标记
11 int path[maxn][2];
12 void dfs(int i,int j,int k)
13 {
14     path[k][0]=i,path[k][1]=j;
15     if(k==p*q-1)//走完了
16     {
17         flag=true;
18         return ;
19     }
20     for(int u=0; u<8; u++)
21     {
22         int a=i+dir[u][0];
23         int b=j+dir[u][1];
24         if(!vis[a][b]&&a>=0&&b>=0&&a<q&&b<p)
25         {
26             vis[a][b]=true;//标记已走
27             dfs(a,b,k+1);
28             if(flag)
29                 return ;
30             vis[a][b]=false;//清楚标记
31         }
32     }
33 }
34 int main()
35 {
36     int k=0;
37     cin>>n;
38     while(n--)
39     {
40 
41         cin>>p>>q;
42         printf("Scenario #%d:\n",++k);
43         memset(vis,false,sizeof(vis));
44         memset(path,0,sizeof(path));
45         for(int i=0; i<q; i++)
46         {
47             for(int j=0; j<p; j++)
48             {
49                 flag=false;
50                 vis[i][j]=true;
51                 dfs(i,j,0);
52                 if(flag)
53                     break;
54                 vis[i][j]=false;
55             }
56             if(flag)
57                 break;
58         }
59         if(!flag)
60             printf("impossible");
61         else
62         {
63             for(int i=0; i<p*q; i++)
64                 printf("%c%d",path[i][0]+A,path[i][1]+1);
65         }
66         printf("\n\n");
67     }
68     return 0;
69 }
View Code

 

poj 2488 A Knight's Journey(DFS)

标签:

原文地址:http://www.cnblogs.com/cxbky/p/4853519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!