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 h

dfs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int p,q;
int flag=0;
bool vis[100][100];
int path[100][100];
int tx[8]={-1,1,-2,2,-2,2,-1,1};
int ty[8]={-2,-2,-1,-1,1,1,2,2};
bool judge(int x,int y)
{
if(x>=1&&y>=1&&x<=p&&y<=q&&!flag&&!vis[x][y]) return true;
return false;
}
void dfs(int x,int y,int step)//这里不知道你们有没有问题,反正我碰到了,就是,这里的x和y,与坐标系不一样
{
path[step][0]=x;//x代表行
path[step][1]=y;//y代表列
if(step==p*q)//直角坐标系中x虽然是横轴,但是x的改变则是列的变化
{
flag=1;
return ;
}
for(int i=0;i<8;i++)
{
int sx=x+tx[i];
int sy=y+ty[i];
if(judge(sx,sy))
{
vis[sx][sy]=1;
dfs(sx,sy,step+1);
vis[sx][sy]=0;
}
}
} int main()
{
int n,c=0;
cin>>n;
while(n--)
{
scanf("%d%d",&p,&q);
printf("Scenario #%d:\n",++c);
memset(vis,0,sizeof(vis));
flag=0;
vis[1][1]=1;
dfs(1,1,1);
if(flag==1)
{
for(int i=1;i<=p*q;i++)
{
printf("%c%d",path[i][1]-1+'A',path[i][0]);
}
printf("\n");
}
else printf("impossible\n");
if(n!=0) printf("\n");
}
return 0;
}

  

ow 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
意思很明确,不过毕竟是英文题,有点难读

寒假训练 A - A Knight's Journey 搜索的更多相关文章

  1. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  2. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

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

  3. HDOJ-三部曲一(搜索、数学)- A Knight's Journey

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  4. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

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

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

  6. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  7. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  8. poj2488 A Knight's Journey裸dfs

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

  9. POJ2488A Knight's Journey[DFS]

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

随机推荐

  1. tar命令的使用方法

    tar [-cxtzjvfpPN] 文件与目录参数说明:-c :建立一个打包文件:-x :解开一个打包文件:-t :查看 tar包里面的文件:-z :打包后用gzip压缩,生成.tar.gz文件:-j ...

  2. 精读JavaScript模式(四),数组,对象与函数的几种创建方式

    一.前言 放了个元旦,休息了三天,加上春运抢票一系列事情的冲击,我感觉我的心已经飞了.确实应该收收心,之前计划的学习任务也严重脱节了:我恨不得打死我自己. 在上篇博客中,笔记记录到了关于构造函数方面的 ...

  3. PowerDesigner版本控制器设置权限

    PowerDesigner版本控制权限之前一直在Groups里面设置,一直没有效果,原因终于找出来了,PowerDesigner是要对每个库单独赋权限的,步骤如下: 连接上版本控制,在左侧菜单 Obj ...

  4. BGP笔记

    BGP:用于AS与AS之间的路由,但现在也越来越多的用在IDC内部了 BGP是应用层协议,应用TCP协议(唯一一个运用TCP的路由协议) IGP和EGP的区别:IGP运行在一个AS之内,EGP运行在A ...

  5. SQL 行列转换数据转换为字符串

    行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_cente ...

  6. 搜藏一个php文件上传类

    <?php /** * 上传文件类 * @param _path : 服务器文件存放路径 * @param _allowType : 允许上传的文件类型和所对应的MIME * @param _f ...

  7. 创建一个背景为蓝色的pygame窗口

    import sys import pygame def creat_screen(): #初始化pygame pygame.init() #设置窗口大小并保存在screen对象中 screen = ...

  8. PHP7.27: MySqlhelper class

    https://github.com/ThingEngineer/PHP-MySQLi-Database-Class https://github.com/wildantea/php-pdo-mysq ...

  9. 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)

    题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...

  10. TurboLinux系统管理习题一

    TurboLinux系统管理习题一 1. 使用vi编辑文本只读时,强制存盘并退出的命令是?(单选题)A :w!        B :q!       C   :wq!        D   :e!答案 ...