A Knight's Journey
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

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 <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&#39;s Journey (搜索 DFS)的更多相关文章

  1. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...

  2. POJ 2488 A Knight&#39;s Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29226   Accepted: 10 ...

  3. POJ 2488-A Knight&#39;s Journey(DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10 ...

  4. poj2488--A Knight&#39;s Journey(dfs,骑士问题)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31147   Accepted: 10 ...

  5. DFS深搜——Red and Black——A Knight&#39;s Journey

    深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...

  6. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

  7. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  8. POJ 2243 简单搜索 (DFS BFS A*)

    题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...

  9. 【算法入门】深度优先搜索(DFS)

    深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...

随机推荐

  1. webmagic学习-使用注解编写爬虫

    写在前面: 官方文档:http://webmagic.io/docs/zh/posts/ch5-annotation/README.html WebMagic支持使用独有的注解风格编写一个爬虫,引入w ...

  2. 使用 gulp-file-include 构建前端静态页面

    前言 虽然现在单页面很流行,但是在 PC 端多页面还是常态,所以构建静态页面的工具还有用武之地.最近也看到了一些询问如何 include HTML 文件的问题. 很多时候我们在写静态页面的时候也希望能 ...

  3. JavaScript正则表达式知识点

    通过学习imooc课程<JavaScript正则表达式>http://www.imooc.com/video/12539,对视频教学内容做一个知识整理. 一个正则表达式在线工具:http: ...

  4. 最大流——Dinic算法

    前面花了很长时间弄明白了压入-重标记的各种方法,结果号称是O(V3)的算法测demo的时候居然TLE了一个点,看了题解发现所有人都是用Dinic算法写的,但它的复杂度O(V2E)明显高于前者,具体是怎 ...

  5. C#中SQL语句参数写法

    OracleConnection oc=new OracleConnection("data source=osserver;User Id=****;password=**"); ...

  6. Socket网络编程之概述理解

    今天主要讲讲什么是socket网络编程 socketde 英文原义是"孔"或者"插座".是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机 ...

  7. ssh远程登录,禁止root登录

    1,useradd xiaobingpasswd xiaobing (设置密码) 2,禁止root登陆,修改 /etc/ssh/sshd_configPermitRootLogin yes 改为 Pe ...

  8. 以太网接口芯片W5300使用说明

    一.芯片简介 引用百度百科对芯片的一个简介,我就不再赘述. W5300的目标是在高性能的嵌入式领域,如多媒体数据流服务.与WIZnet现有的芯片方案相比较,W5300在内存空间和数据处理能力等方面都有 ...

  9. SpringMVC 表单验证

    SpringMVC 表单验证 本章节内容很丰富,主要有基本的表单操作,数据的格式化,数据的校验,以及提示信息的国际化等实用技能. 首先看效果图 项目结构图 接下来用代码重点学习SpringMVC的表单 ...

  10. 解决WebSocket兼容ie浏览器版本问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7942323.html 在使用Netty进行WebSocket开发时,测试发现:ie 11系列个别低版本连接W ...