题目链接

题目大意:

给定一个矩阵,马的初始位置在(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. Android下 scrollview的滚动停止事件的监听方法

    使用递归调用的方法,每隔5毫秒检查一下是否已经停止,如果已经停止,就拿到事件啦! 不扯蛋,直接上代码. scrollContent就是我的scrollview. [代码]java代码: ? 1 2 3 ...

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

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

  3. J2EE之WebLogic Server

    WebLogic是用于开发.集成.部署和管理大型分布式Web应用. 网络应用和数据库应 用的Java应用server. 将Java的动态功能和Java Enterprise标准的安全性引入大型网络应用 ...

  4. [Ruby] LEVEL 2 Methods and Classes

    Optional Arguments Set default arguments, when we don't need to call it, we can simply skip it. def ...

  5. TCP/IP之分层

    网络协议通常分不同层次进行开发,每一层分别负责不同的通信功能.一个协议族,比方T C P / I P,是一组不同层次上的多个协议的组合.T C P / I P通常被觉得是一个四层协议系统. 1.每层的 ...

  6. Qt 学习之路 :自定义只读模型

    model/view 模型将数据与视图分割开来,也就是说,我们可以为不同的视图,QListView.QTableView和QTreeView提供一个数据模型,这样我们可以从不同角度来展示数据的方方面面 ...

  7. Android短信的发送和接收监听

    /**发送与接收的广播**/ String SENT_SMS_ACTION = "SENT_SMS_ACTION"; String DELIVERED_SMS_ACTION = & ...

  8. Phonegap 极光推送api 服务器端推送代码

    .net 版本 极光推送 后台接口 HttpWebResponseUtility类 using System; using System.Collections.Generic; using Syst ...

  9. plsql 把数据导出成为 .sql文件,记住了

    今天上午,同事 提醒我.可以直接把数据导出成 .sql 文件,类似于 反编译.见下图

  10. Java中long和double的原子性

    Java中long和double的原子性 java中基本类型中,long和double的长度都是8个字节,32位(4字节)处理器对其读写操作无法一次完成,那么,JVM,long和double是原子性的 ...