题目地址:http://poj.org/problem?id=2488

Sample Input

3
1 1
2 3
4 3

Sample Output

Scenario #1:
A1 Scenario #2:
impossible Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4

题目:给你一个p*q的棋盘,规则布局参考上图。列用字母表示,行用数字表示。这样一个左上角的节点就是(A,1)。
骑士的棋子走日字形状,可以从任意节点出发,终点也是任意的。问你能不能遍历所有的位置,并输出这样的路径,如果
存在多条路径,输出字典序最小的那条路径。
分析:按照上面那个图片的提示,从当前位置出发遍历8个方向,如果最后能到达,得到的就是字典序最小的序列。
起点一定会是(A1)。直接从A1开始dfs。
通过这道题,再次体会到回溯的地方。。。。。。
code:
#include<stdio.h>
#include<string.h>
#include <queue>
#include <stack>
#include <algorithm>
#define Max 21 using namespace std; int n, m;
bool vis[30][30];
int x[]={-1, 1, -2, 2,-2, 2,-1, 1};
int y[]={-2,-2, -1,-1, 1, 1, 2, 2};
//从当前马的位置可以走到的8个位置
//在满足字典序最小的前提下 这8个方向的遍历先后不能变动
bool ok; struct node
{
int col, row;
}path[1000];
int e=0; bool sure(int i, int j)
{
if(i>=1 && i<=n && j>=1 && j<=m)
return true;
else
return false;
} void dfs(int i, int j, int cnt)//cnt表示我们在当前这次dfs偶中搜索到的节点
{
path[cnt].col=j; path[cnt].row=i;//当前位置进入路径队列 if(cnt==n*m){
ok=true; return; //所有节点都已经遍历到
}//在这return 开始往回回溯 int a, b;
for(int k=0; k<8; k++){ a=i+x[k];
b=j+y[k];
if(sure(a, b) && !vis[a][b]){
vis[a][b]=true;
dfs(a, b, cnt+1);
if(ok==true) return;//在这return 返回主函数
vis[a][b]=false;
}
}
return;
} int main()
{
int tg; scanf("%d", &tg);
int cnt=1; while(tg--)
{
scanf("%d %d", &n, &m);
ok=false;
if(n==1 && m==1){
printf("Scenario #%d:\n", cnt++);
printf("A1\n\n");
continue;
}
else{
ok=false;
memset(vis, false, sizeof(vis));
vis[1][1]=true;
e=0;
dfs(1, 1, 1); if(!ok){
printf("Scenario #%d:\n", cnt++);
printf("impossible\n\n");
}
else{
printf("Scenario #%d:\n", cnt++);
for(int i=1; i<=n*m; i++)
{
printf("%c%d", path[i].col+'A'-1, path[i].row );
}
printf("\n\n");
}
}
}
return 0;
}


poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】的更多相关文章

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

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

  2. POJ 2488 A Knight's Journey (回溯法 | DFS)

    题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...

  3. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  4. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  5. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  6. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  7. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  8. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

  9. POJ 2488 A Knight's Journey

    题意:给一个n×m的棋盘,如果一个骑士可以从任意一个位置出发不重复的走遍棋盘的每个格子就输出字典序最短的路径. 解法:dfs.暴搜n×m次,只是被字典序输出坑了……而且字母是列序号数字是行序号……这两 ...

随机推荐

  1. hdu 4576(概率dp+滚动数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 思路:由于每次从某一位置到达另一位置的概率为0.5,因此我们用dp[i][j]表示第i次操作落在 ...

  2. 输入一个十进制数N,将它转换成R进制数输出(运用递归实现)

    #include<stdio.h> int cnt=0;                             //用来记录每个进制存放的位置 char num[20];   //用来存 ...

  3. 3280 easyfinding

    3280 easyfinding  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给一个M ...

  4. Linux I/O 进阶

    非阻塞I/O 阻塞I/O对应于低速的系统调用,可能会使进程永远阻塞.非阻塞I/O可以使我们发出open.read.write这样的I/O操作,并使这些操作不会永远阻塞.如果这种操作不能完成,则调用立即 ...

  5. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  6. Viewpage实现左右无限滑动

    实现逻辑参考:http://www.cnblogs.com/xinye/archive/2013/06/09/3129140.html 代码:如下 public class MainActivity ...

  7. 1070 Bash游戏 V4

    1070 Bash游戏 V4 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量最少1个,最多不超过对手上 ...

  8. 初学习-python打印乘法表、正方形、三角形

    for x in range(1,4): for o in range(0,x-1): print('*',end='') pass pass print('*') print('\n')print( ...

  9. 练习: 省市联动(Ajax)

    // 示例一: china.xml (位于 src 目录下) <?xml version="1.0" encoding="utf-8"?> < ...

  10. JavaWeb 之监听器

    1. JavaWeb 监听器概述 在 JavaWeb 被监听的事件源为: ServletContext, HttpSession, ServletRequest, 即三大域对象. 监听域对象" ...