pku 2488 A Knight's Journey (搜索 DFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28697 | Accepted: 9822 |
Description
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
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
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 <cstdlib>
#include <stack>
#include <cstring> using namespace std; const int MAX = 9; const int dirx[8]={-1,1,-2,2,-2,2,-1,1},diry[8]={-2,-2,-1,-1,1,1,2,2}; typedef struct Point{
int x,y;
}point; int p,q,n;
bool visit[MAX][MAX];
point pre[MAX][MAX];
bool mark;
stack<int> stx,sty; void printPath(int x,int y){
stx.push(x);
sty.push(y); int tx,ty; tx = pre[x][y].x;
ty = pre[x][y].y; while(tx!=-1){
stx.push(tx);
sty.push(ty);
x = pre[tx][ty].x;
y = pre[tx][ty].y;
tx = x;
ty = y;
} while(!stx.empty()){
printf("%c%d",sty.top()-1+'A',stx.top());
stx.pop();
sty.pop();
} printf("\n\n");
} void dfs(int x,int y,int len){ if(mark)return;
if(len==p*q){
printPath(x,y);
mark = true;
return;
} int i,tx,ty; for(i=0;i<8;++i){ tx = x+dirx[i];
ty = y+diry[i];
if(tx<1 || tx>p || ty<1 || ty>q)continue;
if(visit[tx][ty])continue; pre[tx][ty].x = x;
pre[tx][ty].y = y;
visit[tx][ty] = true;
dfs(tx,ty,len+1);
visit[tx][ty] = false;
}
} int main()
{
//freopen("in.txt","r",stdin);
//(Author : CSDN iaccepted) int i;
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Scenario #%d:\n",i);
scanf("%d %d",&p,&q);
memset(visit,0,sizeof(visit));
mark = false;
pre[1][1].x = -1;
pre[1][1].y = -1;
visit[1][1] = true;
dfs(1,1,1);
visit[1][1] = false; if(!mark){
printf("impossible\n\n");
}
}
return 0;
}
题目意思:象棋中的马在一张棋盘上是否能不反复的走全然部格子。假设能走完输出走的路径(以字典序),假设没有一种走法能达到这种目标,则输出impossible。
思路就是DFS 搜下去,当走过的格子数达到格子总数时就打印路径。所以要用一个数组记录每一个定点的前驱节点。
pku 2488 A Knight's Journey (搜索 DFS)的更多相关文章
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- POJ 2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29226 Accepted: 10 ...
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2243 简单搜索 (DFS BFS A*)
题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
随机推荐
- js input输入事件兼容性问题
if(navigator.userAgent.indexOf('Android') > -1){ $("#sign").on("input", funct ...
- js实现前端下载文件
在前端下载文本格式的文件时,可采用下面的方式: (1)创建基于文件内容的Blob对象: (2)通过URL上的createObjectURL方法,将blob对象转换成一个能被浏览器解析的文件地址. (3 ...
- 设备类型检测大全---userAgent
对各种类型的设备的检测,以及所使用的浏览器的类型 function detect(ua) { var os = this.os = {}; var browser = this.browser = { ...
- Java的DAO设计模式
用java的DAO模式实现对一个学生实体的增加,查询操作. 1.建立一个学生实体类 Student.java public class Student { private String sid; pr ...
- 一脚踏进Memcached的大门
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- Hibernate开发文档
hibernate配置 映射约束文件 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3. ...
- 分页查询时,使用cookie保存上次的查询条件。jQuery实现方法以及中间遇到的坑
今天做分页查询时需要在跳转页面时保存上次查询的条件,如下: 实现的大致思路就是用cookie本地保存. 其中需要用到jQuery.Cookie插件. 使用方法很简单: 存数据:$.cookie(“ke ...
- (11.13)Java小知识!
今天想要与大家分享一下有关于构造方法的知识! 构造方法的定义与作用 构造方法是一种特殊类型的方法.当一个对象被创建的时候,构造方法用来初始化对象,也就是说构造方法其实是一个名词而不是动词,像我刚刚开始 ...
- phpcms 的getcache()函数
一直没有去研究phpcms 的getcache()函数是干嘛的,今天有空去看了一下,原来就那样. 1 function getcache($name, $filepath='', $type='fil ...
- 休息,归类一下CSS初级的东西
css基础的东西集中体现在了"磊盒子"这一个枯燥无味的东西上面,灵活的运用盒子的内外边距,浮动,定位以及一些基础的属性,将一个静态的页面变得磊出来,这是CSS基础的练习. 在css ...