题意:给一个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的更多相关文章

  1. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

  2. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  3. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  4. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  5. POJ 2488 A Knight's Journey (回溯法 | DFS)

    题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...

  6. 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 ...

  7. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  8. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  9. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

  10. POJ 2488 A Knight's Journey (DFS)

    poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...

随机推荐

  1. ios NavBar+TarBar技巧

    NavBar+TarBar iphone开发 NavBar+TarBar 1  改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2  隐藏“back”按钮: sel ...

  2. 在cmd命令行下登录本地oracle数据库与服务器上的oracle

    一.本地登录 cmd->sqlplus 用户名/密码@数据库名 (as sysdba)->enter注:如果此用户名不是系统用户sys,scott,system就不需要加as sysdba ...

  3. setPreferredSize和setSize的区别及用法

    我以前很喜欢borderlayout的布局方式,每次想特别调整每个区域的大小,但是每次将一个panel放入到north或者其他4个区域时,总是达不到想要的效果,刚刚才发现原来setPreferredS ...

  4. WebUploader API

    Uploader new Uploader( opts ) ⇒ Uploader 上传入口类. var uploader = WebUploader.Uploader({ swf: 'path_of_ ...

  5. phpd读取txt文件(自动解析换行)

    <form id="form" method="post" action="whois.php"> <?php $newf ...

  6. [转载]MongoDB查询优化原则

    .在查询条件.排序条件.统计条件的字段上选择创建索引,可以显著提高查询效率. .用$or时把匹配最 多 结果的条件放在最前面,用$and时把匹配最 少 结果的条件放在最前面. .使用limit()限定 ...

  7. Machine Learning for Developers

    Machine Learning for Developers Most developers these days have heard of machine learning, but when ...

  8. codeforces #310 div1 A

    首先我们考虑最暴力的拆解拼凑 显然拆分掉所有的链需要 n-m 次 之后拼凑需要 n-1 次 然后由题目规定可知:只有从1出发且连续的链不用拆掉,其余的都必须拆掉(因为两个都套有娃娃的套娃不能组合) 我 ...

  9. 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求

    李洪强iOS开发本人集成环信的经验总结_06_发送好友请求 同步好友请求 异步好友请求

  10. live555源码研究(十)------在编译过程中遇到的问题及解决方法

    一.编译testOnDemandRTSPServer.cpp. 在testProgs项目中,加入testOnDemandRTSPServer.cpp进行编译,编译类型是编译成exe文件,在编译过程中会 ...