pku 2488 A Knight's Journey (搜索 DFS)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 28697 | Accepted: 9822 |
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
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开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
随机推荐
- linq 在查询表达式中处理异常
在查询表达式的上下文中可以调用任何方法. 但是,我们建议避免在查询表达式中调用任何会产生副作用(如修改数据源内容或引发异常)的方法. 此示例演示在查询表达式中调用方法时如何避免引发异常,而不违反有关异 ...
- 静态代理设计模式(StaticProxy)
静态代理设计模式: 要求:真实角色,代理角色:真实角色和代理角色要实现同一个接口,代理角色要持有真实角色的引用. 在Java中线程的设计就使用了静态代理设计模式,其中自定义线程类实现Runable接口 ...
- Java Enum用法详解
Java Enum用法详解 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举 ...
- [java基础] java中的自动装箱与自动拆箱
自动装箱的一个例子: Integer i = 1; //实际上是执行了Integer i = Integer.valueOf(1) 自动拆箱的一个例子: Integer a =1; int b = a ...
- 通过PING命令中的TTL来判断对方操作系统
---恢复内容开始--- 通过PING命令中的TTL来判断对方操作系统简单来说,TTL全程Time to Live,意思就是生存周期.首先要说明ping命令是使用的网络层协议ICMP,所以TTL指的是 ...
- Lucene全文检索学习笔记
全文索引 介绍Lucene的作者:Lucene的贡献者Doug Cutting是 一位资深全文索引/检索专家,曾经是V-Twin搜索引擎(Apple的Copland操作系统的成就之一)的主要开发者,后 ...
- Spring的69个知识点
目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...
- selenium3.7+ python3 添加cookie模拟登陆
一.背景介绍 最近做一个爬虫项目,用selenium调用浏览器去获取渲染后的源码,但是每次登陆都需要手机验证,这真的是头痛啊,这种验证方式不要想着去破解,还是老老实实用手机收验证码去吧!反正我是不知道 ...
- pt-log-player
简介 pt-log-player是MySQL日志回放工具,在pt2.4中被去除,由percona-playback取代. 在2.1中还是保留,如果想使用的话,需要下载2.1版本的. 使用方法 pt-l ...
- Python之数据类型-[bisect,heap]
bisect >>> import bisect >>> >>> b = [ 20, 34, 35, 65, 78 ] >>> ...