POJ 2488 A Knight's Journey
题意:给一个n×m的棋盘,如果一个骑士可以从任意一个位置出发不重复的走遍棋盘的每个格子就输出字典序最短的路径。
解法:dfs。暴搜n×m次,只是被字典序输出坑了……而且字母是列序号数字是行序号……这两个总弄反……搜索的时候会只要按字典序搜那8个方向就可以了,搜到第一条满足条件的路径就结束。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long using namespace std; int dir[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -2, 1, 2, 1, -1, 2, 1, 2};
struct node
{
int x, y;
node(int x, int y) : x(x), y(y) {}
node() {}
};
int n, m;
bool vis[30][30];
node path[30][30];
bool dfs(node tmp, int step)
{
if(step == n * m)
return true;
for(int i = 0; i < 8; i++)
{
int tx = tmp.x + dir[i][0], ty = tmp.y + dir[i][1];
if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty])
{
path[tmp.x][tmp.y] = node(tx, ty);
vis[tx][ty] = 1;
if(dfs(node(tx, ty), step + 1))
return true;
vis[tx][ty] = 0;
}
}
return false;
}
int main()
{
int T;
while(~scanf("%d", &T))
{
int cse = 1;
while(T--)
{
scanf("%d%d", &n, &m);
int ans = 1;
int x, y;
for(int i = 0; i < n && ans; i++)
{
for(int j = 0; j < m && ans; j++)
{
memset(vis, 0, sizeof vis);
x = i, y = j;
vis[i][j] = 1;
if(dfs(node(i, j), 1))
ans = 0;
}
}
printf("Scenario #%d:\n", cse++);
if(!ans)
{
for(int i = 0; i < n * m; i++)
{
printf("%c%d", 'A' + y, x + 1);
node tmp = path[x][y];
x = tmp.x, y = tmp.y;
}
puts("");
}
else
puts("impossible");
puts("");
}
}
return 0;
}
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 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ 2488 A Knight's Journey (回溯法 | DFS)
题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...
- 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(搜索)
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+路径打印
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 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
随机推荐
- 【Spring-boot多数据库】Spring-boot JDBC with multiple DataSources sample
application.properties spring.ds_items.driverClassName=org.postgresql.Driver spring.ds_items.url=jdb ...
- 中文变英文字母(ios)
[_EnglishName setString:addressPerson.name]; if (CFStringTransform((__bridge CFMutableStringRef)_Eng ...
- 对.net orm工具Dapper在多数据库方面的优化
Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...
- C#设计模式学习资料--外观模式
http://www.cf17.com/html/article/172.html http://blog.csdn.net/scucj/article/details/1374657 http:// ...
- 相似元素存在的意义---HTML&CSS
1.<q> 效果: 告诉浏览器这是一段短引用,让浏览器以合适的方法来显示 注: 不能直接以双引号直接代替<q>,因为有些浏览器<q>的效果不是双引号. 不要忘了移动 ...
- java第六课 oop
java oop 1.面向过程的结构化程序设计弊端:方法和数据结构都是毫无规律的定义在程序中任何位置 方法定义和方法要处理的数据结构也都是分开定义 2.对象:每new一次,就创建1个新对 ...
- uva 437 hdu 1069
dp 将石块按三个面存入队列 按底面积排序 dp就最大高度 按嵌套矩形最长路做做法 #include <iostream> #include <cstdio> #inc ...
- Akka Stream文档翻译:Motivation
动机 Motivation The way we consume services from the internet today includes many instances of streami ...
- Java在Windows的环境配置
JDK环境变量配置的步骤如下: 1.我的电脑-->属性-->高级-->环境变量. 2.配置用户变量: 系统变量 a.新建 JAVA_HOME C:\Program Files\Jav ...
- highChartTable 切换
<!doctype html> <html lang="en"> <head> <script type="text/javas ...