poj2488 bfs
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

#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的更多相关文章
- poj2243 bfs
O - 上一个题的加强版 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 6 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- spring AOP 的几种实现方式(能测试)
我们经常会用到的有如下几种 1.基于代理的AOP 2.纯简单Java对象切面 3.@Aspect注解形式的 4.注入形式的Aspcet切面 一.需要的java文件 public class ChenL ...
- 营业额统计(bzoj1588)
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- iPhone:4.7 5.5 4 3.5 对应的各个设备屏幕尺寸对应的像素及App上线信息
Shared App Information You can access these properties from the App Details page in the App Informat ...
- 直接放个DB2 SQL STATEMENT大全好了!
SQL statements This topic contains tables that list the SQL statements classified by type. SQL sch ...
- Linux 查看 删除进程
这东西,时间久不用总容易忘....记下来! 1. 在 LINUX 命令平台输入 1-2 个字符后按 Tab 键会自动补全后面的部分(前提是要有这个东西,例如在装了 tomcat 的前提下, 输入 to ...
- hdu 5653 Bomber Man wants to bomb an Array
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5653 题意:已知炸弹可以炸掉左边L个位置,右边R个位置,那么炸点炸掉的总数是L+R+1.给定每个炸弹的 ...
- CLR via C#(17)--接口
CLR不允许继承多个基类,但是可以继承多个接口.凡是能使用具名接口类型的实例的地方,都能使用实现了接口的一个类型的实例. 接口是对一组方法签名进行了统一命名,但不提供任何实现,而具体类则必须为继承的全 ...
- Faster-rnnlm代码分析2 - HSTree的构造
也就是构造一棵Huffman Tree,输入是按照词汇频次由高到低排序的 采用层次SoftMax的做法,是为了使得训练和预测时候的softmax输出加速,原有multinomal softmax,是和 ...
- VS对路径的访问被拒绝
问题:权限问题. 1.检查自己电脑账户.是否是Administrator.如果没有启用.在[我的电脑]——[管理]——[本地用户和组]——[用户]——[Administrator]——[属性启用] 2 ...
- Android4.4 以太网和DHCP启动过程介绍
转自:http://blog.csdn.net/wlwl0071986/article/details/51451843 Android4.4已经加入了以太网的支持.现在对以太网的初始化流程.网络策略 ...