A Knight's Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29226   Accepted: 10023

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

The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents 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

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares 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

  1. 3
  2. 1 1
  3. 2 3
  4. 4 3

Sample Output

  1. Scenario #1:
  2. A1
  3.  
  4. Scenario #2:
  5. impossible
  6.  
  7. Scenario #3:
  8. A1B3C1A2B4C2A3B1C3A4B2C4

深索水题。,,。主要是方向被规定了。

AC代码例如以下:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. struct H
  6. {
  7. int x;
  8. char y;
  9. }b[30],c[30];
  10.  
  11. int dx[8]={-1,1,-2,2,-2,2,-1,1};
  12. int dy[8]={-2,-2,-1,-1,1,1,2,2};
  13.  
  14. int a[30][30],vis[30][30];
  15. int n,m,bj;
  16.  
  17. void dfs(int h,int z,int cur)
  18. {
  19. int i;
  20. if(cur==n*m)
  21. {
  22. if(bj==0)
  23. {
  24. for(i=0;i<cur;i++)
  25. cout<<b[i].y<<b[i].x;
  26. cout<<endl<<endl;
  27. bj=1;
  28. }
  29.  
  30. }
  31. else
  32. {
  33. for(i=0;i<8;i++)
  34. {
  35. int xx,yy;
  36. xx=h+dx[i];
  37. yy=z+dy[i];
  38. if(!vis[xx][yy]&&xx>=1&&xx<=n&&yy>=1&&yy<=m)
  39. {
  40. vis[xx][yy]=1;
  41. b[cur].x=xx;b[cur].y=(char)(yy+'A'-1);
  42. dfs(xx,yy,cur+1);
  43. vis[xx][yy]=0;
  44. }
  45. }
  46.  
  47. }
  48. }
  49.  
  50. int main()
  51. {
  52. int t,cas=0;
  53. cin>>t;
  54. while(t--)
  55. {
  56. cas++;
  57. memset(a,0,sizeof a);
  58. memset(vis,0,sizeof vis);
  59. cin>>n>>m;
  60. bj=0;
  61. b[0].x=1;b[0].y=1+'A'-1;
  62. vis[1][1]=1;
  63. cout<<"Scenario #"<<cas<<":"<<endl;
  64. dfs(1,1,1);
  65. if(bj==0)
  66. {
  67. cout<<"impossible"<<endl<<endl;
  68. }
  69. }
  70. return 0;
  71. }

POJ 2488 A Knight&#39;s Journey的更多相关文章

  1. poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...

  2. pku 2488 A Knight&#39;s Journey (搜索 DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28697   Accepted: 98 ...

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

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

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

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

  5. POJ 2488-A Knight&#39;s Journey(DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10 ...

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

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

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

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

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

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

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

随机推荐

  1. jdk1.8安装后查看Java -version出错

    最近在电脑行安装了多个jdk的版本 分别是jdk1.6,jdk1.7,jdk1.8三个版本,在配置环境变量的时候,选择的是jdk1.7; 但是奇怪的是,当我在cmd中输入java -version后, ...

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

  3. JavaScript ES6箭头函数指南

    前言 胖箭头函数(Fat arrow functions),又称箭头函数,是一个来自ECMAScript 2015(又称ES6)的全新特性.有传闻说,箭头函数的语法=>,是受到了CoffeeSc ...

  4. MongoDB 进阶

    一.MongoDB 复制(副本集) MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允 ...

  5. poj 3264 线段树

    题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 多次求任一区间Ai-Aj中最大数和最小数的差 线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天 ...

  6. hdu 4540 dp

    题意: 假设: 1.每一个时刻我们只能打一只地鼠,并且打完以后该时刻出现的所有地鼠都会立刻消失: 2.老鼠出现的位置在一条直线上,如果上一个时刻我们在x1位置打地鼠,下一个时刻我们在x2位置打地鼠,那 ...

  7. zoj 3229 上下界网络最大可行流带输出方案

    收获: 1. 上下界网络流求最大流步骤: 1) 建出无环无汇的网络,并看是否存在可行流 2) 如果存在,那么以原来的源汇跑一次最大流 3) 流量下界加上当前网络每条边的流量就是最大可行流了. 2. 输 ...

  8. CDOJ 1401 谭爷的黑暗沙拉 数学

    谭爷的黑暗沙拉 题目连接: http://mozhu.today/#/problem/show/1401 Description 谭爷有\(n\)种不同种类的食材(水果&蔬菜),他想做出一份总 ...

  9. php 利用fsockopen GET/POST 提交表单及上传文件

    1.GET get.php <?php $host = 'demo.fdipzone.com'; $port = 80; $errno = ''; $errstr = ''; $timeout  ...

  10. mt7620 wireless驱动特性意外发现

    前言 今天又客户反映无线參数SSID编程了HT_AP0, 同事通过后台给他改动后反映给我,我想不正确啊,难道是无线驱动crash了?那应该不能玩才对啊... 追查线索 我们的路由器会定期汇报数据SSI ...