搜索 || DFS || POJ 2488 A Knight's Journey
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[30][30];
int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};
int vis[30][30];
int P, Q, flag = 0;
struct node
{
int x, y;
}pre[30][30];
void dfs(int x, int y, int step)
{
if(step == P * Q)
{
flag = 1;
return;
}
if(flag) return;//全走完了之后就不用再走了
for(int i = 0; i < 8; i++)
{
int xx = x + dx[i], yy = y + dy[i];
if(xx >= 0 && xx < P && yy >= 0 && yy < Q && !vis[xx][yy])
{
vis[xx][yy] = 1;
step++;
pre[x][y] = (node){xx, yy};//用pre输出路径
dfs(xx, yy, step);
if(flag) return;//===全走完了之后就不用再走了===
vis[xx][yy] = 0;
step--;
}
}
return;
}
int main()
{
int T, cc = 0;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &Q, &P);
memset(vis, 0, sizeof(vis));
flag = 0;
vis[0][0] = 1;
dfs(0,0,1);
printf("Scenario #%d:\n", ++cc);
if(!flag) printf("impossible\n");
else
{
int ux = 0, uy = 0, vx, vy;
for(int i = 0; i < P * Q; i++)
{
char c = ux + 'A';
int z = uy + 1;
printf("%c%d", c, z);
vx = pre[ux][uy].x, vy = pre[ux][uy].y;
ux = vx, uy = vy;
}
printf("\n");
}
printf("\n");
}
return 0;
}
搜索 || DFS || POJ 2488 A Knight's Journey的更多相关文章
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ 2488 A Knight's Journey (回溯法 | DFS)
题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...
- Poj 2488 A Knight's Journey(搜索)
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- POJ 2488 A Knight's Journey【DFS】
补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
随机推荐
- 用 SDL2 进行事件驱动编程
其实没必要说得太复杂...就是读取用户输入啦. 沿用上一篇的代码,加入事件轮询. 环境:SDL2 + VC++2015 下面的代码将打开background.png和event.png,将backgr ...
- 从零开始构建一个Reactor模式的网络库(二)线程类Thread
线程类Thread是对POSIX线程的封装类,因为要构建的是一个Linux环境下的多线程网络库,对线程的封装是很必要的. 首先是CurrentThread命名空间,主要是获取以及缓存线程id: #if ...
- Mysql数据库实现高可用
Mysql实现高可用 MMM MMM(master-master replication manager for mysql)mysql主主复制管理器. MMM是一套灵活的脚本程序,基于perl实现, ...
- 批处理(cmd)的学习记录
批处理的使基本使用 Command Introduction Example set 设置环境变量 set name="小明" call 启动应用程序 rem 解释说明,可通过 ...
- 洛谷P2564 [SCOI2009]生日礼物(单调队列)
传送门 准确的来说这个应该叫尺取法? 先对所有的点按$x$坐标进行排序 我们维护两个指针$l,r$,每一次令$r$不断右移直到所有颜色齐全,再不断右移$l$直到颜色数不足,那么此时$[l-1,r]$这 ...
- 高级开发不得不懂的Redis Cluster数据分片机制
Redis 集群简介 Redis Cluster 是 Redis 的分布式解决方案,在 3.0 版本正式推出,有效地解决了 Redis 分布式方面的需求. Redis Cluster 一般由多个节点组 ...
- iperf工具学习记录
源码下载地址:http://sourceforge.net/projects/iperf/ 编译命令: tar -zxvf iperf-2.0.5.tar.gz cd iperf-2.0.5 ./co ...
- 改变UITabbar顶部分割线颜色
项目中是使用UITabbarController 因此改变UITabbar的分割线代码如下 由于美术没提供图片,所以自己创建了个图片 //改变tabbar 线条颜色 CGRect rect = CGR ...
- 关于协程:nodejs和golang协程的不同
nodejs和golang都是支持协程的,从表现上来看,nodejs对于协程的支持在于async/await,golang对协程的支持在于goroutine.关于协程的话题,简单来说,可以看作是非抢占 ...
- nginx上游模块
1 概念 The ngx_http_upstream_module is used to define groups of servers that can be referenced by the ...