poj2488--A Knight's Journey(dfs,骑士问题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 31147 | Accepted: 10655 |
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
a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
Output
of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int t , n , m , sum ;
int mm[30][30] ;
int pre[1000] ;
int a[8][2] = { {-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1} };
int dfs(int x,int y,int temp)
{
if( temp == sum )
return 1 ;
int flag = 0 , xx , yy , i ;
for(i = 0 ; i < 8 ; i++)
{
xx = x + a[i][0] ;
yy = y + a[i][1] ;
if( xx >= 0 && xx < n && yy >= 0 && yy < m && !mm[xx][yy] )
{
mm[xx][yy] = 1 ;
pre[x*m+y] = xx*m+yy ;
flag = dfs(xx,yy,temp+1);
if( flag ) return flag ;
mm[xx][yy] = 0 ;
}
}
return flag ;
}
int main()
{
int i , j , k , tt ;
scanf("%d", &t);
for(tt = 1 ; tt <= t ; tt++)
{
memset(mm,0,sizeof(mm));
memset(pre,-1,sizeof(pre));
scanf("%d %d", &m, &n);
sum = n*m ;
mm[0][0] = 1 ;
k = dfs(0,0,1);
printf("Scenario #%d:\n", tt);
if(k == 0)
printf("impossible");
else
{
for(i = 0 , k = 0 ; i < sum ; i++)
{
printf("%c%c", k/m+'A', k%m+'1');
k = pre[k] ;
}
}
printf("\n\n");
}
return 0;
}
poj2488--A Knight's Journey(dfs,骑士问题)的更多相关文章
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- pku 2488 A Knight's Journey (搜索 DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28697 Accepted: 98 ...
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- POJ 2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29226 Accepted: 10 ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
- 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 ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
随机推荐
- FWT板子
板子: #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> u ...
- ECSHOP二次开发文档【文件结构说明和数据库表分析】
最近公司又把之前的ecshop进行二次开发,之前看过一些ecshop的东西,但是都过了很久差不多都忘完了,现在得把之前的重新捡回来,特此搜到这些文档,接下来的开发工作中会方便不少. Ecshop文件结 ...
- (3) openssl genrsa(生成rsa私钥)
genrsa用于生成RSA私钥,不会生成公钥,因为公钥提取自私钥,如果需要查看公钥或生成公钥,可以使用openssl rsa命令. 使用man genrsa查询其用法. openssl genrsa ...
- Go:单元测试
测试用的文件名必须以 _test.go 结尾: 测试用的函数名必须以 Test 开头,一般来说:Test+被测试的函数名(第一个字母必须大写): func TestXx(t *testing.T) { ...
- 不同子系统采用不同MySQL编码LATIN1和UTF8的兼容
程序处理 这是一个历史遗留系统, 旧的系统是C++开发的, 插入数据的时候, 没有统一MYSQL各个层次(服务器, 数据库, 表, 列)的编码, 这个情况基本上是MYSQL的默认安装导致的, 实际的数 ...
- 为公司架构一套高质量的 Vue UI 组件库
有没有曾遇过,产品要我们实现一个功能,但是 iview 或者 elementui 不支持,我们然后义正言辞的说,不好意思,组件库不支持,没法做到. 有没有曾和设计师争论得面红耳赤,其实也是因为组件库暂 ...
- PHP 配置文件php.ini文件优化
PHP 5.3.3 safe_mode = On #控制php中的函数执行比如system() 这个函数可以调用系统目录 比如 rm ,打开这个配置之后,同时把很多文件操作的函数进行了权限控制 saf ...
- Android 图片设置圆角 方法之二
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片.接下来我们再介绍一种方法. 首先, 自定义ImageView: android:id="@+id/iv" ...
- pytest以函数形式的测试用例
from __future__ import print_function#pytest以函数形式形成测试用例def setup_module(module): print('\nsetup_modu ...
- 大数据学习——yum练习安装jdk
yum list | grep jdk 安装jdk-1.8.0版本 -openjdk* 安装后,执行java -version 配置环境变量 使用vim /etc/profile 编辑profile文 ...