http://poj.org/problem?id=2488

A Knight's Journey

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 41951

 

Accepted: 14274

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

 
记录路径。
 
AC代码:
#include<cstdio>
#include<cstring>
using namespace std; int T,p,q;
int To[][]= {{-,-},{-,},{-,-},{-,},{,-},{,},{,-},{,}};
int trp[][],visit[][]; int dfs(int ed,int be,int x,int y)
{
if(be==ed)return ;
for(int i=; i<; i++)
{
int xx=x+To[i][];
int yy=y+To[i][];
if(xx>= && xx<q && yy>= && yy<p && !visit[xx][yy])
{
visit[xx][yy]=;
if(dfs(ed,be+,xx,yy))
{
trp[be][]=xx;
trp[be][]=yy;
return ;
}
visit[xx][yy]=;
}
}
return ;
} int main()
{
int k=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&p,&q);
memset(visit,,sizeof(visit));
trp[][]=;
trp[][]=;
visit[][]=;
printf("Scenario #%d:\n",++k);
if(dfs(p*q,,,))
{
for(int i=; i<p*q; i++)
printf("%c%d",trp[i][]+'A',trp[i][]+);
}
else
printf("impossible");
printf("\n");
if(T!=)printf("\n");
}
return ;
}

poj2488 bfs的更多相关文章

  1. poj2243 bfs

    O - 上一个题的加强版 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     6 ...

  2. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  3. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  4. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  5. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  6. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  7. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  8. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  9. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

随机推荐

  1. Greedy:Jessica's Reading Problem(POJ 3320)

    Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...

  2. Spring Data JPA初使用(转载)

    我们都知道Spring是一个非常优秀的JavaEE整合框架,它尽可能的减少我们开发的工作量和难度. 在持久层的业务逻辑方面,Spring开源组织又给我们带来了同样优秀的Spring Data JPA. ...

  3. Gson解析复杂的json数据

    最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的json解析方法即JsonObject ...

  4. 【转】CV_EXPORT定义的作用,lib及dll的区别

    http://blog.csdn.net/viewcode/article/details/8021989 在core.hpp中,CV_EXPORT是出现频率最高的词之一. 1. CV_EXPORT是 ...

  5. [Android Pro] adb 进入 recovery, adb 进入 bootloader

    reference to : http://blog.csdn.net/mldxs/article/details/18699965 重启到Recovery界面 adb reboot recovery ...

  6. python基础——定制类

    python基础——定制类 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方 ...

  7. No space left on device you must specify the filesystem type--Linux重启挂在失败

    在Linux中拷贝了一个文件比较大5G,直接提示:No SPace Left On Device,很明显是磁盘空间不够了,我因为是在虚拟机上面建的,直接右击虚拟机==>编辑设置 如图片1所示, ...

  8. NYOJ题目809摸底

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtEAAAI4CAIAAAAj4CIaAAAgAElEQVR4nO3drXLjTLeG4X0S4TmQYB

  9. 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

  10. Win7 DCOM 配置中我的电脑出现红色箭头并且无属性显示的解决方法

    http://blog.csdn.net/embededvc/article/details/19234479