HDU 2488 A Knight's Journey(dfs)

我就是我 2022-06-10 11:26 215阅读 0赞

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. Scenario #2:
  4. impossible
  5. Scenario #3:
  6. A1B3C1A2B4C2A3B1C3A4B2C4

题解:

题意:

给一个n*m的棋盘,和马走的八个方向,问你是否能走满整个棋盘,如果可以输出字典序最小的

思路:

这题以前在搜索专题做过,就是如果可以走满就直接从(1,1)这个点开始搜索,要保证字典序最小那么搜索的方向就要注意了,越右边的方向越优先考虑,找到一种就可以了,然后输出的时候先输出y再输出x这个有点坑

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<vector>
  12. #include<deque>
  13. using namespace std;
  14. #define lson k*2
  15. #define rson k*2+1
  16. #define M (t[k].l+t[k].r)/2
  17. #define INF 1008611111
  18. #define ll long long
  19. #define eps 1e-15
  20. int p[30][30];//存是否走过
  21. int xx[30];//存路径的x坐标
  22. int yy[30];
  23. int dirx[8]={-1,1,-2,2,-2,2,-1,1};//方向比较重要
  24. int diry[8]={-2,-2,-1,-1,1,1,2,2};
  25. int n,m,tag;
  26. void dfs(int x,int y,int step)
  27. {
  28. if(step>=m*n)//如果走满了
  29. {
  30. tag=1;
  31. }
  32. if(tag)
  33. {
  34. return;
  35. }
  36. int i,j,sx,sy;
  37. for(i=0;i<8;i++)//八个方向dfs搜索回溯
  38. {
  39. sx=x+dirx[i];
  40. sy=y+diry[i];
  41. if(sx>=1&&sy>=1&&sx<=m&&sy<=n&&!p[sx][sy])
  42. {
  43. p[sx][sy]=1;
  44. xx[step]=sx;
  45. yy[step]=sy;//记录路径
  46. dfs(sx,sy,step+1);
  47. if(tag)
  48. return;
  49. p[sx][sy]=0;
  50. }
  51. }
  52. }
  53. int main()
  54. {
  55. int i,j,test,q;
  56. scanf("%d",&test);
  57. for(q=1;q<=test;q++)
  58. {
  59. scanf("%d%d",&m,&n);
  60. memset(p,0,sizeof(p));
  61. tag=0;
  62. xx[0]=1;
  63. yy[0]=1;
  64. p[1][1]=1;
  65. dfs(1,1,1);//起点为1,1
  66. printf("Scenario #%d:\n",q);
  67. if(tag)
  68. for(i=0;i<n*m;i++)
  69. {
  70. printf("%c%d",'A'+yy[i]-1,xx[i]);//输出是先y再x
  71. }
  72. else
  73. printf("impossible");
  74. printf("\n");
  75. if(q!=test)//注意格式
  76. printf("\n");
  77. }
  78. return 0;
  79. }

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读

    相关 poj 2488 A Knight's Journey(DFS)

    题目大意:给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径。   马的遍历是一道经典回溯题,当然还是DFS...这题有2个要密切注