A Knight's Journey_DFS
Description

The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.
Input
Output
If no such path exist, you should output impossible on a single line.
Sample Input
3
1 1
2 3
4 3
Sample Output
Scenario #1:
A1 Scenario #2:
impossible Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
【题意】给出m*n的方阵(但输入时先输入的是n,再输入m),问马是否能走遍棋盘,输出字典序的第一种路径。
【思路】用mp[i][0]表示第i步所在那个格子的横坐标,用mp[i][1]表示第i步所在那个格子的纵坐标。
字典序的话,注意di数组的顺序。用一个dfs就好啦。
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int vis[N][N];
int mp[N][];
int n,m;
bool flag;
int di[][]={-,-,-,,-,-,-,,,-,,,,-,,};
bool go(int x,int y)
{
if(x<||x>=n||y<||y>=m) return false;
else return true;
} void dfs(int i,int j,int k)
{
if(k==n*m)
{
for(int i=;i<k;i++)
{
printf("%c%d",mp[i][]+'A',mp[i][]+);
}
printf("\n");
flag=true;
// return ;
}
else
for(int x=;x<;x++)
{
int xx=i+di[x][];
int yy=j+di[x][];
if(!vis[xx][yy]&&go(xx,yy)&&!flag)
{
vis[xx][yy]=;
mp[k][]=xx;
mp[k][]=yy;
dfs(xx,yy,k+);
vis[xx][yy]=; }
}
} int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
memset(vis,,sizeof(vis));
vis[][]=;
mp[][]=;
mp[][]=;
flag=false;
printf("Scenario #%d:\n",cas++);
dfs(,,); if(!flag) printf("impossible\n");
puts("");
}
return ;
}
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 ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...
随机推荐
- java SE学习之线程同步(详细介绍)
java程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题: 当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...
- SAP BDC说明
简单说一下这个DBC,之前也一直在用,每次找记录都很麻烦,所以今天干脆就记下来吧 T-CODE:SHDB 输入个NAME,T-CODE.然后执行...最后用保存或者返回来结束录屏. 然后选择记录,创建 ...
- Graphical installers are not supported by the vm
http://www-01.ibm.com/support/docview.wss?uid=swg21462180 Technote (troubleshooting) Problem(Abstrac ...
- linux bash: sqlplus: command not found 错误处理
在oracle用户下 ,执行sqlplus命令,抛出如上错误. 解决办法: 1.su oracle 2.cd /home/oracle 3. 执行命令 source .bash_pro ...
- Eclipse导出可执行Jar文件(包含第三方Jar包)
1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...
- ace-下载-安装
安装ACE 1.获取安装包 到ACE的官方网站http://www.cs.wustl.edu/~schmidt/ACE.html或者http://riverace.com/index.htm下载最新版 ...
- 使用Zen coding高效编写html代码
zen-Coding是一款快速编写HTML,CSS(或其他格式化语言)代码的编辑器插件,这个插件可以用缩写方式完成大量重复的编码工作,是web前端从业者的利器. zen-Coding插件支持多种编辑器 ...
- PostgreSQL数据库系统优点
PostgreSQL 是世界上可以获得的最先进的开放源码的数据库系统, 它提供了多版本并行控制,支持几乎所有 SQL 构件(包括子查询,事务和用户定 义类型和函数), 并且可以获得非常广阔范围的(开发 ...
- eclipse常用10个快捷键[转载]
转载自:http://www.jb51.net/softjc/139467.html
- LinearLayout练习
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...