题目链接

题目大意:

给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点。

列为数字,行为字母,搜索按字典序。

分析:

用 vis[x][y] 标记是否已经访问。因为要搜索所有的可能,所以没搜索完一次要把vis[x][y]标记为未访问。详情见代码。

用 p[x][y] 表示 (x,y)点的祖先,以便输出路径。

dfs搜索即可。

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <cstring> using namespace std; const int maxn = ;
const int VAL = ; int dx[] = {-, , -, , -, , -, }; //搜索顺序是字典序
int dy[] = {-, -, -, -, , , , }; bool vis[maxn][maxn];
int p[maxn][maxn], ex, ey;
int cnt, total, n, m; bool dfs(int x, int y, int px, int py) {
p[x][y] = px*VAL+py; cnt++; //如果已经走遍的点等于总点数,表示已经全部访问完
if(cnt == total) {
ex = x; ey = y;
return true;
} for(int d=; d<; d++) {
int nx = x+dx[d];
int ny = y+dy[d]; if(nx < || ny < || nx >= n || ny >= m) continue;
if(vis[nx][ny]) continue; vis[nx][ny] = true; //标记已访问 if(dfs(nx, ny, x, y)) return true; vis[nx][ny] = false; //标记未访问
cnt--; //已访问的数要减一
} return false;
} void print(int x, int y) {
if(x == && y == ) return;
int e = p[x][y];
int px = e/VAL, py = e%VAL;
print(px, py);
printf("%c%d", y+'A', x+); //y对应字母,x对应数字
} int main(){
int T;
scanf("%d", &T);
for(int kase=; kase<=T; kase++) {
scanf("%d%d", &n, &m);
cnt = ; total = n*m; memset(vis, , sizeof(vis)); vis[][] = true; printf("Scenario #%d:\n", kase); if(dfs(, , , )) { //有方案,输出
printf("A1");
print(ex, ey);
putchar('\n');
}
else printf("impossible\n");
putchar('\n');
} return ;
}

POJ2248 A Knight's Journey(DFS)的更多相关文章

  1. POJ2488A Knight's Journey[DFS]

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

  2. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  3. A Knight's Journey(dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25950   Accepted: 8853 Description Back ...

  4. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

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

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

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

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

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

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

  8. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  9. poj2488 A Knight's Journey裸dfs

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

随机推荐

  1. Hibernate命名空间怎样实现?

    什么是命名查询?  Hibernate同意在映射文件里定义字符串形式的查询语句.这样的查询方式成为命名查询  使用命名查询有什么优点?  因为使用Hibernate的HQL经常须要在Java代码中写字 ...

  2. 开发环境下jboss 7.1.1 Final 的jsp热部署解决方案--转

    公司的网站系统使用的中间是jboss7.1.1 Final,由于有些数据和服务测试环境不具备,免不了要在测试环境调试些jsp页面,发现从jboss社区里下载的jboss应用有个问题,新增jsp页面会实 ...

  3. vim 学习相关记录

    VIM 相关内容****************** vim 的三个模式: 编辑模式 --> 输入模式 --> 末行模式 编辑模式: 通常键入键盘值被理解成一个操作; 如: dd(删除行) ...

  4. composer 说明<转>

    转自  http://blog.csdn.net/zzulp/article/details/18981029 Composer是一个基于项目的依赖管理器,负责将PHP项目所依赖的包或库安装到项目所在 ...

  5. css.day01

    1.CSS 初步认识 web 标准 w3c 结构   xhtml    table  p  hr  br   td    img  a 表现   CSS CSS 最大的好处  就是让  结构和表现   ...

  6. fastjson反序列化

    package cn.jsonlu.passguard.utils; import com.alibaba.fastjson.JSON; import java.lang.reflect.Type; ...

  7. ​ScrollView、ListView嵌套

    1.手动设置ListView高度 经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动代码设置ListV ...

  8. 【转】数据库分页Java实现

    [转]数据库分页Java实现 MySQL分页 主要是MySQL数据库内置LIMIT函数 注意添加mysql的JAR包mysql-connector-java-5.1.13-bin.jar 在中小数据量 ...

  9. My First Blog.

    I just wanna mark my first blog in order to have a wonderful memories in the future.

  10. asp.net基础概念总结

    1  什么是asp.net?asp.net是一种编程语言吗? asp.net是Microsoft公司推出的新一代建立动态web应用程序的开发平台,是一种建立动态web应用程序的新技术. 不是,asp. ...