POJ2248 A Knight's Journey(DFS)
题目链接。
题目大意:
给定一个矩阵,马的初始位置在(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)的更多相关文章
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- A Knight's Journey(dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25950 Accepted: 8853 Description Back ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- 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 ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
随机推荐
- 从 Kubernetes 谈容器网络
基本概念 在 Kubernetes 中.资源从管理粒度上分为三级:容器.Pod.Service. 容器 即 Docker 或者 Rocket 容器(1.0 中仅支持这两种容器). 容器是最低粒度的资源 ...
- linux下查看磁盘空间
如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda2 ...
- ubuntu 连接 mssql <转>
转自 http://www.sendong.net/thread-90941-1-1.html 在linux下连接MSSQL,因为微软同志没有提供任何接口给开发人员,大约他们认为要用MSSQL的,只 ...
- 高健壮性css---Float详细
(一)关于float 首先我们了解到,CSS网页布局的原理,就是按照HTML代码中对象声明的顺序,以流布局的方式来显示它,而流布局就不得不说到float浮动技术..在HTML中的所有对象,默认分为两种 ...
- 各版本 linux(转)
Linux各种版本下载 ftp://ftp.linuxforum.net/ISO/Redhat7.3/valhalla-i386-disc1.iso ftp://ftp.linuxforum.net/ ...
- 删除重复记录的SQL语句
1.所有字段均重复的记录(重复记录保留一条) Select distinct * into #Tmp from tblName Drop table tblName Select * into tbl ...
- DbProviderFactories.GetFactory Oracle.ManagedDataAccess.Client
因为最近项目,要使用微软的EF框架不安装Oracle客户端的情况下,访问Oracle数据库.调用如下代码的时候会报错. System.Data.Common.DbProviderFactories.G ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...
- [转] 与调试器共舞 - LLDB 的华尔兹
你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...
- 写一个基于NSURLSession的网络下载库
前段时间AFNetworking 更新到3.0 ,彻底废弃NSURLConnection的API转由NSURLSession来实现,代码并没有改动很大,AF封装的很好了,读完源码感觉收获很大. 下载不 ...