题目地址: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. WPF 中双向绑定通知机制之ObservableCollection使用

    msdn中   ObservableCollection<T> 类    表示一个动态数据集合,在添加项.移除项或刷新整个列表时,此集合将提供通知. 在许多情况下,所使用的数据是对象的集合 ...

  2. day13迭代器与生成器

    三个作业: # 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码 login_dic = {'alex':False} def ...

  3. JavaScript 初步认识

    首先呢 要成为WEB全栈工程师呢 JavaScript 是必须要会的 高级技术看自身兴趣爱好,但是基础必须掌握 因为有良好的基础学习jQuery会比较轻松. js是一门轻量的脚本语言 我学它主要目的是 ...

  4. iOS内存管理之浅见

    当我们用alloc.new.copy创建对象时,对象的应用计数为1,当把这个对象retain时.引用计数+1.当对这个对象发送release消息时,引用计数-1,当对象的引用计数为0时,系统回收这个对 ...

  5. ubuntu12.04 安装nginx+php+mysql (lnmp)的web服务器环境

    1.Ubuntu12.04 安装nginx+php+mysql (lnmp)的web服务器环境 http://blog.db89.org/ubuntu12-04-install-nginx-php-m ...

  6. MySQL-库的操作

    05-库的操作   本节重点: 掌握库的增删改查   一.系统数据库 执行如下命令,查看系统库 show databases; nformation_schema: 虚拟库,不占用磁盘空间,存储的是数 ...

  7. 部署vuejs dist文件,通过node.js编译

    前期准备: 1. Linux环境,安装配置node.js ① 下载地址:http://nodejs.cn/download/  ,下载linux 64位 ② 已编译好的压缩包,解压到指定目录 cd / ...

  8. JavaScript数据结构与算法-队列练习

    队列的实现 // 队列类 function Deque () { this.dataStore = []; this.enqueueFront = enqueueFront; this.enqueue ...

  9. CSS标签内多余内容隐藏

    CSS: <style> .mazey{width:100px;} .nowrap{overflow:hidden;text-overflow:ellipsis;white-space:n ...

  10. Bootstrap Paginator分页插件+ajax

    Bootstrap Paginator分页插件下载地址: DownloadVisit Project in GitHub  Bootstrap分页插件属性介绍: http://www.cnblogs. ...