A Knight's Journey

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 51   Accepted Submission(s) : 17
Problem 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
3
1 1
2 3
4 3
 
Sample Output
Scenario #1:
A1
 
Scenario #2:
impossible
 
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
 
Source
PKU
 
 
第一次写深搜,感觉还不错
 
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. int m,n;
  5. int c; //已走过的步数
  6. bool f; //标记是否找到答案了
  7. int chess[30][30]={0};
  8. int step[2][8]={{-1, 1,-2, 2,-2,2,-1,1}, //每一种可能的走法,注意要按字典序排列
  9. {-2,-2,-1,-1, 1,1, 2,2}};
  10. char ans1[64];
  11. int ans2[64];
  12. bool move(int i,int j,int k)
  13. {
  14. if(i+step[0][k]>=0&&i+step[0][k]<m&&j+step[1][k]>=0&&j+step[1][k]<n&&!chess[i+step[0][k]][j+step[1][k]])
  15. {
  16. chess[i+step[0][k]][j+step[1][k]]=1;
  17. return true;
  18. }
  19. else
  20. return false;
  21. }
  22. void DFS(int i,int j)
  23. {
  24.  
  25. ans1[c]=j+'A';
  26. ans2[c]=i+1;
  27.  
  28. if(c==m*n) //找到结果,回退
  29. {
  30. f=true;
  31. return;
  32. }
  33. for(int t=0;t<8;t++) //尝试每一种走法
  34. {
  35. if(move(i,j,t))
  36. {
  37. c++;
  38. DFS(i+step[0][t],j+step[1][t]);
  39. if(c==m*n) //如果找到结果就一直回退
  40. return;
  41. chess[i+step[0][t]][j+step[1][t]]=0; //还原
  42. c--;
  43. }
  44. }
  45. return;
  46. }
  47.  
  48. int main()
  49. {
  50. int T;
  51. cin>>T;
  52. int t=T;
  53. while(T--)
  54. {
  55. int i,j,k;
  56. cin>>m>>n;
  57. for(j=0;j<n;j++)
  58. {
  59. for(i=0;i<m;i++)
  60. {
  61. c=1;
  62. memset(chess,0,sizeof(chess));
  63. chess[i][j]=1;
  64. f=false;
  65. DFS(i,j);
  66. if(f)
  67. {
  68. cout<<"Scenario #"<<t-T<<":"<<endl;
  69. for(k=1;k<=m*n;k++)
  70. cout<<ans1[k]<<ans2[k];
  71. cout<<endl;
  72. break;
  73. }
  74. }
  75. if(f)
  76. break;
  77. }
  78. if(!f)
  79. {
  80. cout<<"Scenario #"<<t-T<<":"<<endl;
  81. cout<<"impossible"<<endl;
  82. }
  83. cout<<endl;
  84. }
  85. }
 
 
 
 

HDOJ-三部曲一(搜索、数学)- A Knight's Journey的更多相关文章

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

  2. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

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

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

  4. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  5. poj2488 A Knight's Journey裸dfs

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

  6. POJ2488A Knight's Journey[DFS]

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

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

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

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

  9. poj2488 A Knight's Journey

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

随机推荐

  1. (24)odoo中模型标识汇总

    * 设置->技术->数据结构->模型                模型    模型描述    类型    瞬态模型account.account    科目    基础对象    ...

  2. 补交git、ssh

    本来应该早就应该交的,自己给忘记了,非常抱歉,现在补交上来 词频统计: 代码地址:https://coding.net/u/liuff/p/cipin/git ssh:git@git.coding.n ...

  3. 补第二周四人小组WBS/NABCD

    四人小组项目<东北师范大学论坛> 要求: 1.给出需求概述.功能列表.痛点或亮点.NABCD及WBS模型在此项目中的应用. 2.不熟悉的名词,自行搜索资料并参考教材第393页开始的术语索引 ...

  4. WPF:ListView数据绑定及Style

    DrugRecordsWin.xaml <ListView Grid.Row="4" Grid.Column="1" Name="list_Dr ...

  5. 字符串匹配的sunday算法

    sunday算法核心思想:启发式移动搜索步长! SUNDAY 算法描述: 字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).这里介 ...

  6. SQL 创建索引的作用以及如何创建索引

    SQL 创建索引的作用以及如何创建索引 SQL 创建索引的作用 一.使用索引的优点: 1.通过唯一性索引(unique)可确保数据的唯一性 2.加快数据的检索速度 3.加快表之间的连接 4.减少分组和 ...

  7. webapi方式

    随笔 - 112  文章 - 0  评论 - 334 ASP.NET MVC学习系列(二)-WebAPI请求   继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前 ...

  8. 识别低效率的SQL语句

    1.返回行与逻辑读的比率 CREATE TABLE t as select * from dba_objects; --CREATE INDEX idx ON t (object_id); ---例1 ...

  9. subline快捷键

    折叠所有代码:  按ctrl+k,再按ctrl+1 展开所有代码: 按ctrl+k,再按ctrl+j 折叠此处代码:  ctrl+shift+[ 展开此处代码: ctrl+shift+]

  10. iis7.5 设置伪静态

    1)首先新建一个应用程序池,名称任意,比如:nettest,托管管道模式先暂时设置为集成模式,等下面的一系列设置完成之后再设置成经典模式: 2)部署好站点,并将此站点的应用程序池设置为nettest; ...