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. 解决Android下元素滑动问题

    移动端左右.上下滑动: 当页面中既需要页面滑动操作,又需要上下或左右滑动页面上的某个元素时,直接使用zepto中提供的swipe事件是不能直接达到目的的,原因如下: (1)在Android低端机上to ...

  2. 无阻赛的脚本(js脚本延迟方法)

    js脚本的加载与执行 1.延迟脚本(defer属性) 带有defer属性的script标签,可以放置在文档的任何位置,在页面解析到该标签时,会开始下载该脚本,但是不会立即执行,直到dom加载完成(on ...

  3. PTA 循环单链表区间删除 (15 分)

    本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...

  4. EOJ 3242 重复数

    重复数 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: 256 megabytes 有 ...

  5. selenium元素定位

    在网页自动化测试中,我们要让程序自动模拟我们的点击.输入.悬浮.拖动等操作,完成我们的测试用例组. 输入.点击.打开这样的动词,已经包含在了selenium的方法中,可以直接调用(当然你也可以自己写) ...

  6. distcc (dcc_execvp) ERROR: failed to exec XX: Permission denied

    首先先确保一下是不是能执行下面语句: # sudo -u nobody XX --version 如果能看见版本信息,则可以不用往下看. 再检查一下distccd.service # sudo cat ...

  7. C语言控制流语句

    title: 2017-10-18控制流 tags: binsearch else-if, shellsort, insertsort grammar_cjkRuby: true --- 前段时间忙着 ...

  8. Django总结

    Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 web 应用上有 趣的关键性的东西.为了达到这个目标,Django 提供了通用W ...

  9. php中向前台js中传送一个二维数组

    在php中向前台js中传送一个二维数组,并在前台js接收获取其中值的全过程方法: (1),方法说明:现在后台将数组发送到前台 echo json_encode($result); 然后再在js页面中的 ...

  10. YII2 models非常好用的控制输出数据【重写Fields】

    models里重写Fields真的很好用,用于分类.评论功能 列子:评论表models/Comment.php 1.关联商品表 2.获取父级(即管理员)评论 public function Field ...