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 ...
随机推荐
- chown
chown 命令 用途:更改与文件关联的所有者或组 chown [ -f ] [ -h ] [ -R ] Owner [ :Group ] { File ... | Directory ... } c ...
- Code for the Homework1 改进
#include <iostream> #include <vector> #include "shape.h" //using namespace std ...
- 实时数据处理环境搭建flume+kafka+storm:3.kafka安装
1. 解压 tar -zxvf 2.配置/app/kafka_2.9.2-0.8.1.1/config/server.properties #标识-- broker.id=0 ...
- 你想建设一个能承受500万PV/每天的网站吗?服务器每秒要处理多少个请求才能应对?
你想建设一个能承受500万PV/每天的网站吗?服务器每秒要处理多少个请求才能应对? 你想建设一个能承受500万PV/每天的网站吗? 500万PV是什么概念?服务器每秒要处理多少个请求才能应对?如果计算 ...
- IntelliJ IDEA 进行js Debug调试
idea的js调试目前看来不同给力,一是玩转它需要安装谷歌插件支持,二是貌似存在一些bug... 一.新建一个jsp并打上断点 二.调试 idea出现提示: 安装JetBrains IDE Suppo ...
- Java在mysql插入数据的时候的乱码问题解决
今天在使用hibernate的时候,插入mysql的数据中的中文总是显示乱码,之前出现过类似的问题,但是没有太在意,今天又发生了.所以向彻底的解决一下. 参考的博文: http://www.cnblo ...
- bzoj 2876: [Noi2012]骑行川藏 拉格朗日数乘
2876: [Noi2012]骑行川藏 Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1033 Solved: ...
- struts2 标签的使用之一 s:if
struts2 的web 项目中为了方便的编写jsp,标签是最好的选择 1:struts2 标签库的定义在**-core-版本号.jar META-INF 路径下找到struts-tags.tld文件 ...
- 其实,前面倒腾那么多,只是为了想玩SPRING BOOT
嘿嘿,,曲线达到.. 看来看来很多国内的速成,都不爽. 官方教程最体贴~~~:) http://docs.spring.io/spring-boot/docs/current/reference/ht ...
- ubuntu无线上网静态ip配置以及配置静态IP 之后无法正常上网的解决方案
一. 配置无线网络的静态IP 编辑/etc/network/interfaces文件如下: auto lo wlan0 iface lo inet loopback iface wlan0 inet ...