POJ 2488 A Knight's Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29226 | Accepted: 10023 |
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
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
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
深索水题。,,。主要是方向被规定了。
AC代码例如以下:
- #include<iostream>
- #include<cstring>
- using namespace std;
- struct H
- {
- int x;
- char y;
- }b[30],c[30];
- int dx[8]={-1,1,-2,2,-2,2,-1,1};
- int dy[8]={-2,-2,-1,-1,1,1,2,2};
- int a[30][30],vis[30][30];
- int n,m,bj;
- void dfs(int h,int z,int cur)
- {
- int i;
- if(cur==n*m)
- {
- if(bj==0)
- {
- for(i=0;i<cur;i++)
- cout<<b[i].y<<b[i].x;
- cout<<endl<<endl;
- bj=1;
- }
- }
- else
- {
- for(i=0;i<8;i++)
- {
- int xx,yy;
- xx=h+dx[i];
- yy=z+dy[i];
- if(!vis[xx][yy]&&xx>=1&&xx<=n&&yy>=1&&yy<=m)
- {
- vis[xx][yy]=1;
- b[cur].x=xx;b[cur].y=(char)(yy+'A'-1);
- dfs(xx,yy,cur+1);
- vis[xx][yy]=0;
- }
- }
- }
- }
- int main()
- {
- int t,cas=0;
- cin>>t;
- while(t--)
- {
- cas++;
- memset(a,0,sizeof a);
- memset(vis,0,sizeof vis);
- cin>>n>>m;
- bj=0;
- b[0].x=1;b[0].y=1+'A'-1;
- vis[1][1]=1;
- cout<<"Scenario #"<<cas<<":"<<endl;
- dfs(1,1,1);
- if(bj==0)
- {
- cout<<"impossible"<<endl<<endl;
- }
- }
- return 0;
- }
POJ 2488 A Knight's Journey的更多相关文章
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- 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(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- 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 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个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 ...
随机推荐
- jdk1.8安装后查看Java -version出错
最近在电脑行安装了多个jdk的版本 分别是jdk1.6,jdk1.7,jdk1.8三个版本,在配置环境变量的时候,选择的是jdk1.7; 但是奇怪的是,当我在cmd中输入java -version后, ...
- 1019 General Palindromic Number (20)(20 point(s))
problem A number that will be the same when it is written forwards or backwards is known as a Palind ...
- JavaScript ES6箭头函数指南
前言 胖箭头函数(Fat arrow functions),又称箭头函数,是一个来自ECMAScript 2015(又称ES6)的全新特性.有传闻说,箭头函数的语法=>,是受到了CoffeeSc ...
- MongoDB 进阶
一.MongoDB 复制(副本集) MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允 ...
- poj 3264 线段树
题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 多次求任一区间Ai-Aj中最大数和最小数的差 线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天 ...
- hdu 4540 dp
题意: 假设: 1.每一个时刻我们只能打一只地鼠,并且打完以后该时刻出现的所有地鼠都会立刻消失: 2.老鼠出现的位置在一条直线上,如果上一个时刻我们在x1位置打地鼠,下一个时刻我们在x2位置打地鼠,那 ...
- zoj 3229 上下界网络最大可行流带输出方案
收获: 1. 上下界网络流求最大流步骤: 1) 建出无环无汇的网络,并看是否存在可行流 2) 如果存在,那么以原来的源汇跑一次最大流 3) 流量下界加上当前网络每条边的流量就是最大可行流了. 2. 输 ...
- CDOJ 1401 谭爷的黑暗沙拉 数学
谭爷的黑暗沙拉 题目连接: http://mozhu.today/#/problem/show/1401 Description 谭爷有\(n\)种不同种类的食材(水果&蔬菜),他想做出一份总 ...
- php 利用fsockopen GET/POST 提交表单及上传文件
1.GET get.php <?php $host = 'demo.fdipzone.com'; $port = 80; $errno = ''; $errstr = ''; $timeout ...
- mt7620 wireless驱动特性意外发现
前言 今天又客户反映无线參数SSID编程了HT_AP0, 同事通过后台给他改动后反映给我,我想不正确啊,难道是无线驱动crash了?那应该不能玩才对啊... 追查线索 我们的路由器会定期汇报数据SSI ...