题目:

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 题意:
给你一个p*q的棋盘,跳马在上面的任意一格开始移动,只能走‘日’字,问你能不能经过棋盘上面所有的格子;(百度的题意,明明是说经过所有的格子,有道硬是翻译
成了“找到一条这样的路,骑士每一次都要去一次”),输出要按照字典顺序输出 分析:
深度优先搜索,要经过所有的格子,那就肯定经过(1,1),所以就可以从(1,1)开始搜索; AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int t,p,q,flag;
int a[30][30];
int step[30][30];
int f[8][2]={{1,-2},{-1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
void dfs(int x,int y,int z)
{
    step[z][1]=x;
    step[z][2]=y;
     if (z==p*q)
     {
            flag=1;
            return ;
     }


for (int i=0;i<8;i++)
        {
            int xi=x+f[i][0];
            int yi=y+f[i][1];
            if (xi>=1&&xi<=p&&yi>=1&&yi<=q&&!a[xi][yi]&&!flag)
            {
                a[xi][yi]=1;
                dfs(xi,yi,z+1);
                a[xi][yi]=0;
            }
        }


}
int main()
{
    cin>>t;
   for (int i=1;i<=t;i++)
    {
        flag=0;
       scanf("%d%d",&p,&q);
        memset(a,0,sizeof(a));
        memset(step,0,sizeof(step));
        a[1][1]=1;
        dfs(1,1,1);
        printf("Scenario #%d:\n",i);
        if (flag==1)
         {
             for (int j=1;j<=p*q;j++)
           printf("%c%d",step[j][2]+'A'-1,step[j][1]);
             printf("\n");
         }
        else
            printf("impossible\n");
        if (i!=t)
            printf("\n");
    }
    return 0;
}


 
												

A Knight's Journey (DFS)的更多相关文章

  1. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  2. POJ 2488 A Knight's Journey (DFS)

    poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...

  3. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  4. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  5. POJ 2488-A Knight&#39;s Journey(DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10 ...

  6. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  7. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  8. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  9. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

随机推荐

  1. 关于电脑识别不出自己画的板子上的USB接口问题

    现在在画一个Cortex-A5的底板,现在已经完成,正在测试各个模块,发现USB插上后,电脑提示报错,如下: 网上查了很多,有的说是配置问题,有的说是走线问题,首先配置肯定没问题,因为同一台电脑,在买 ...

  2. PAT甲级——1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...

  3. 893C. Rumor#谣言传播(赋权无向图&搜索)

    题目出处:http://codeforces.com/problemset/problem/893/C 题目大意:一个城中有一些关系圈,圈内会传播谣言,求使每个人都知道谣言的最小花费 #include ...

  4. [SDOI2010]魔法猪学院(k短路)

    A*板子题.我的code只能在luogu上过,bzoj上RE/MLE不清楚为啥. 蒟蒻到AFO前2个月不到的时间才学A*,A*其实就是bfs过程中进行剪支删除没必要的搜索.然后其实上这样剪支即可:如果 ...

  5. 解决python pip 出现read time out问题

    出现read time out问题是一般由于python包被墙导致下载速度变慢,pip下载超时. 解决方法一: 添加 --default-timeout:自定义timeout时间,一般能解决大部分ti ...

  6. crontab不执行service命令

    我这里的需求是每30分钟重启一次 写成下面的格式就可以正确执行了,后面执行的命令写绝对路径 */30 * * * * /usr/bin/supervisorctl restart all

  7. Spring IOC 和AOP

    Spring是什么? Spring是一个轻量级的IoC和AOP容器框架. IOC:IOC就是控制反转,控制反转指的是把创建对象和管理对象之间的依赖关系交给了IOC容器来管理.以前new对象由程序员来控 ...

  8. 新年在家学java之基础篇-参数&修饰符&构造器

    可变参数 不知道可能给方法传递多少个参数时使用这个方法 public void printInfo (String[] args) --可以定义一个数组,在调用这个方法适合赋值给一个数组 public ...

  9. day04-函数,装饰器初成

    面试的时候,经常被问过装饰器,所以掌握好装饰器非常重要. 一.装饰器形成的过程:1.最简单的装饰器.2.被装饰的函数有返回值.3.被装饰的函数有一个参数.4.被装饰的函数有多个位置参数.5.被装饰的函 ...

  10. Linux安装svn服务

    安装svn yum -y install subversion 创建保存仓库/版本库的目录 mkdir -p /opt/data/svndir 创建仓库/版本库, 这里同时创建两个仓库(project ...