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

poj 2488 DFS

时间:2015-08-26 10:48:13      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:dspan idtransmarkspa   span idtransmarkspan   

A Knight‘s Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35974   Accepted: 12272

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

题意是:看 马 是否可以在不重复走相同点的情况下,将所有的点都遍历一遍,并按字典序输出 .

     A B C D E F

1

2

3

4

</pre><pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f

using namespace std;

int n,m,num=0,flag=0;
int hang[100];
char lie[100];
int vis[100][100];
int dx[]= {-1,1,-2,2,-2,2,-1,1};  //注意顺序,为字典序输出
int dy[]= {-2,-2,-1,-1,1,1,2,2};
int Judge(int x,int y)
{
    if(x<=n&&y<=m&&x>0&&y>0)
        return 1;
    return 0;
}
int DFS(int x,int y,int ans)
{
    if(!Judge(x,y))
        return 0;
    if(ans==n*m)
    {
        hang[ans]=x;
        lie[ans]=(char)(y-1+'A');
        flag=1;
        return ans;
    }
    if(vis[x][y]==0&&!flag) //!flag 会保证第一次遍历成功后就不遍历了.
    {
        hang[ans]=x;
        lie[ans]=(char)(y-1+'A');
        vis[x][y]=1;
        for(int i=0; i<8; i++)
        {
            int fx=dx[i]+x;
            int fy=dy[i]+y;
            if(Judge(fx,fy)&&vis[fx][fy]==0)
            {
			  DFS(fx,fy,ans+1);
            }
        }
        vis[x][y]=0; //找不到还原
    }
}
int main()
{
    int T;
    int Case=0,kk=1;
    while(~scanf("%d",&T))
    {
        while(T--)
        {
		    if(kk==1)
				kk=0;
			else
				printf("\n");
        	flag=0;
            num=0;
            memset(vis,0,sizeof(vis));
            scanf("%d%d",&n,&m);
            DFS(1,1,1);
            Case++;
            printf("Scenario #%d:\n",Case);
            if(flag==0)
			{
			 printf("impossible\n");
			 continue;
			}
            for(int i=1; i<=n*m; i++)
				printf("%c%d",lie[i],hang[i]);
            printf("\n");
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 2488 DFS

标签:dspan idtransmarkspa   span idtransmarkspan   

原文地址:http://blog.csdn.net/became_a_wolf/article/details/47998713

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