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

POJ 2488-A Knight's Journey(dfs)

时间:2015-02-18 14:09:55      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

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

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

题意:给出一个规格小于8*8的棋盘,判断一个只能走“日”的骑士能否不重复的走遍整个棋盘,如果能,按字典序输出走的路径,否则输出“impossible”

思路:其实这道题唯一的难点就是按照字典序输出,只要解决了这个就是一个简单的dfs。其实在做很多题的时候我们在设置走的路径的时候都是随意设置的,这道题则不行,既然是按照字典序输出,则你设置路径的时候先按照列排序,让小的在前面,然后列的字母相同时,你按照行排序,同样让小的在前面。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>

using namespace std;
char map[30][2];
int vis[30][30];
int p,q;
int jx[]={-2,-2,-1,-1,1,1,2,2};
int jy[]={-1,1,-2,2,-2,2,-1,1};
int dfs(int x,int y,int cnt)
{
    int i;
    if(cnt==p*q){
        return 1;
    }
    for(i=0;i<8;i++){
        int dx=x+jx[i];
        int dy=y+jy[i];
        if(dx>=1&&dx<=q&&dy>=1&&dy<=p&&!vis[dx][dy]){
            map[cnt][0]=dx+'A'-1;
            map[cnt][1]=dy+'0';
            vis[dx][dy]=1;
            if(dfs(dx,dy,cnt+1))
                return 1;
            vis[dx][dy]=0;
        }
    }
    return 0;
}

int main()
{
    int n,i;
    int icase;
    scanf("%d",&n);
    for(icase=1;icase<=n;icase++){
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        scanf("%d %d",&p,&q);
        vis[1][1]=1;
        map[0][0]='A';
        map[0][1]='1';
        printf("Scenario #%d:\n",icase);
        if(dfs(1,1,1)){
            for(i=0;i<p*q;i++)
                printf("%c%c",map[i][0],map[i][1]);
        }
        else
            printf("impossible");
        printf("\n\n");
    }
    return 0;
}


POJ 2488-A Knight's Journey(dfs)

标签:

原文地址:http://blog.csdn.net/u013486414/article/details/43876255

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