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. mac使用brew安装mysql

    1.安装mysql #brew install mysql 报错 Error: The following directories are not writable by your user: /us ...

  2. 一张图读懂PBN旁切转弯计算

    当DOC8168进入PBN章节以后,所有的保护区不再标注风螺旋的字母位置点.似乎ICAO已经有了精确计算的方法,只是没有告诉我们.沿着风螺旋的轨迹一路走来,切线与角度的换算方法想必已经相当熟悉了吧,这 ...

  3. VirtualBox centos7扩容

    有时候扩容还真不如重新建立一个大硬盘的系统,但是如果你安装了好多东西的话,那还是来扩容一下吧. 查看磁盘格式           在virtualBox中右键点击虚拟机->设置->存储,如 ...

  4. Mongodb 集群实战

    该实战过程完全跟着官网一步一步实现 ,官网教程:https://docs.mongodb.com/manual/tutorial/atlas-free-tier-setup/ 使用Mongo Shel ...

  5. C#多线程——优先级

    在我的公司这里,因为要跟很多特殊的设备打交道,所以会用到多线程的东西,那么我们在进行多线程处理的时候,怎么去设置优先级 我这里用听歌和下载小说做了个例子,我们用电脑的时候肯定是可以边听歌边下载小说的, ...

  6. Python全栈学习_day009知识点

    今日大纲: . 函数的初识 . 函数的返回值 . 函数的参数 1. 函数的初识 统计字符串s的总个数(不能用len) s='fkahfkahofijalkfkadhfkjadhf' count = f ...

  7. Harbor api 操作

    harbor 的版本为 1.5.2 为 Harbor 配置 swagger 官网参考: https://github.com/goharbor/harbor/blob/v1.5.2/docs/conf ...

  8. 2017-12-01 中英文代码对比之ZLOGO 4 & LOGO

    基于前文中文编程语言之Z语言初尝试: ZLOGO 4的一些评论, 此文尝试作一个非常简单的代码对比, 使讨论更加有实例根据. 下图是节选自前文最后的示例代码, 由于选取的对照LOGO版本 (alanc ...

  9. 【代码笔记】Web-HTML-颜色

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  10. 【读书笔记】iOS-音频设备访问

    音频的输入是通过麦克风实现,音频的输出是通过扬声气实现的.在iOS系统中无法直接操控麦克风和扬声器,苹果提供了丰富的音频API. 一,音频API介绍 在iOS和Mac OS X上开发音频应用,主要有两 ...