poj2488 A Knight's Journey
A Knight's Journey
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 24840 | Accepted: 8412 |
Description

Background
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
Source
简单的深搜,不多说,直接上代码!
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int pathlow[30],pathdown[30],visit[30][30];
int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}},n,m;//这里注意是字典序最小
bool dfs(int low,int down,int num)
{
int i,x,y;
if(num==n*m)
{ for(i=0;i<n*m;i++)
{ printf("%c%d",'A'+pathdown[i],pathlow[i]+1);
}
return true;
} for(i=0;i<8;i++)
{
x=low+dir[i][0];
y=down+dir[i][1];
pathlow[num]=x;
pathdown[num]=y;
if(x>=0&&x<n&&y>=0&&y<m&&(!visit[x][y]))
{ visit[x][y]=1;
if(dfs(x,y,num+1))
{ return true;
}
else
{ visit[x][y]=0;//这里要注意,一定要重新标记为0
}
} }
return false; }
int main ()
{
int t,i;
while(scanf("%d",&t)!=EOF)
{ for(i=1;i<=t;i++)
{
printf("Scenario #%d:\n",i);
scanf("%d%d",&n,&m);
memset(visit,0,sizeof(visit));
visit[0][0]=1;
pathlow[0]=0;
pathdown[0]=0;
if(!dfs(0,0,1))
{ printf("impossible");
}
printf("\n\n"); }
} return 0;
}
poj2488 A Knight's Journey的更多相关文章
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ2488A Knight's Journey
http://poj.org/problem?id=2488 题意 : 给你棋盘大小,判断马能否走完棋盘上所有格子,前提是不走已经走过的格子,然后输出时按照字典序排序的第一种路径 思路 : 这个题吧, ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
- A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...
- HDOJ-三部曲一(搜索、数学)- 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)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
随机推荐
- Redis 3.0集群 Window搭建方案
Redis 3.0集群 Window搭建方案 1.集群安装前准备 安装Ruby环境,安装:rubyinstaller-2.3.0-x64.exe http://dl.bintray.com/onecl ...
- 1、elasticsearch简介
1.elasticsearch简介 中文帮助文档地址:http://es.xiaoleilu.com/ • Elasticsearch是一个基于Lucene的实时的分布式搜索和分析引擎.设计用于云计算 ...
- Unity扩展 四种Menu的区别
[MenuItem("Tools\AddColor")] : 在Unity菜单中添加一种快捷,执行public static方式 [AddComponentMenu(" ...
- textContent、innerText 以及Event事件兼容性问题
今天在完成前端的简单练习时发现了一些兼容性的问题,百度后得以解决. 这里主要讨论Firefox与Chrome的兼容性问题. textContent与 innerText 在javascript中, 为 ...
- EasyMonkeyDevice vs MonkeyDevice&HierarchyViewer API Mapping Matrix
1. 前言 本来这次文章的title是写成和前几篇类似的<EasyMonkeyDevice API实践全记录>,内容也打算把每一个API的实践和建议给记录下来,但后来想了下认为这样子并非最 ...
- Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException
异常Log: Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessE ...
- SQL case when
jack 1tom 2anni 3poly 4 select buyer_name, ( case ' then '东' ' then '南' ' then '西' ' the ...
- vector 汇总
Vector成员函数 函数 表述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数据赋值给c. 将n个elem的拷贝赋值给c. c.at(idx) ...
- 安装TDM-GCC
TDM-GCC是一组免费的编译器套件,有32位和64位两种版本.其中64位版既可以编译生成64位的可执行文件,又可以编译生成32位的可执行文件.从TDM-GCC的官网可以下载到相应的安装包,安装完成后 ...
- hdu 4612 Warm up(无向图Tarjan+树的直径)
题意:有N个点,M条边(有重边)的无向图,这样图中会可能有桥,问加一条边后,使桥最少,求该桥树. 思路:这个标准想法很好想到,缩点后,求出图中的桥的个数,然后重建图必为树,求出树的最长直径,在该直径的 ...