A Knight’s Journey

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 35564 Accepted: 12119

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

TUD Programming Contest 2005, Darmstadt, Germany

一段时间没有做搜索,果然手生了很多,犯了各种各样的错误,要赶紧补回来.

  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <vector>
  8. #include <string>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. #define eps 1e-9
  16. #define LL long long
  17. #define PI acos(-1.0)
  18. #define INF 0x3f3f3f3f
  19. #define CRR fclose(stdin)
  20. #define CWW fclose(stdout)
  21. #define RR freopen("input.txt","r",stdin)
  22. #pragma comment(linker,"/STACK:102400000")
  23. #define WW freopen("output.txt","w",stdout)
  24. struct node
  25. {
  26. int x;
  27. int y;
  28. } a[100];
  29. int Dir[][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
  30. bool vis[9][9];
  31. int n,m;
  32. int flag;
  33. bool DFS(int x,int y,int site)
  34. {
  35. vis[x][y]=true;
  36. a[site].x=x;
  37. a[site].y=y;
  38. if(site==n*m)
  39. {
  40. flag=true;
  41. return true;
  42. }
  43. int Fx,Fy;
  44. for(int i=0; i<8; i++)
  45. {
  46. Fx=x+Dir[i][0];
  47. Fy=y+Dir[i][1];
  48. if(Fx>=0&&Fx<n&&Fy>=0&&Fy<m&&!vis[Fx][Fy])
  49. {
  50. if(DFS(Fx,Fy,site+1))
  51. {
  52. return true;
  53. }
  54. }
  55. }
  56. vis[x][y]=false;
  57. return false;
  58. }
  59. int main()
  60. {
  61. int T,w=1;
  62. scanf("%d",&T);
  63. while(T--)
  64. {
  65. scanf("%d %d",&n,&m);
  66. flag=false;
  67. memset(vis,false,sizeof(vis));
  68. for(int i=0;i<n;i++)
  69. {
  70. for(int j=0;j<m;j++)
  71. {
  72. if(DFS(i,j,1))
  73. {
  74. flag=true;
  75. break;
  76. }
  77. }
  78. if(flag)
  79. {
  80. break;
  81. }
  82. }
  83. printf("Scenario #%d:\n",w++);
  84. if(flag)
  85. {
  86. for(int i=1; i<=n*m; i++)
  87. {
  88. printf("%c%c",a[i].y+'A',a[i].x+1+'0');
  89. }
  90. printf("\n\n");
  91. }
  92. else
  93. {
  94. printf("impossible\n\n");
  95. }
  96. }
  97. return 0;
  98. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏的更多相关文章

  1. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  2. 选择排序 分类: 算法 c/c++ 2014-10-10 20:32 509人阅读 评论(0) 收藏

    选择排序(假设递增排序) 每次选取从当前结点到末尾结点中最小的一个与当前结点交换,每一轮固定一个元素位置. 时间复杂度O(n^2),空间复杂度O(1).下面的示例代码以带头结点的链表为存储结构: #i ...

  3. 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  4. Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏

    Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...

  5. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

  6. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  7. 利用OpenMP实现埃拉托斯特尼(Eratosthenes)素数筛法并行化 分类: 算法与数据结构 2015-05-09 12:24 157人阅读 评论(0) 收藏

    1.算法简介 1.1筛法起源 筛法是一种简单检定素数的算法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-194年)发明的,又称埃拉托斯特尼筛法(sieve of Eratos ...

  8. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  9. Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏

    Sequence Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8277 Accepted: 2708 Description ...

随机推荐

  1. decimal类型保留两位小数

    oj.PriceTop =Math.Round(Convert.ToDecimal(reader["PriceTop"]),2);

  2. PostgreSQL Replication之第十一章 使用Skytools(3)

    11.3 管理 pgq-queues Skytools 的一个核心组件是pgq.它提供了一个通用排队接口,它可以让您把消息从一个消息提供者传送到一个任意数目的接收者. 现在的问题是:一般来说,一个队列 ...

  3. Leetcode: Insert Delete GetRandom O(1)

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  4. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  5. poj 1731 Orders

    http://poj.org/problem?id=1731 Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9 ...

  6. [转]-Dmaven.multiModuleProjectDirectory system propery is not set. 解决方案 适用于myeclipes 和 eclipes

    eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...

  7. Array.prototype.each

    Array.prototype.each = function(closure){ //递归合并 return this.length ? [closure(this.slice(0,1))].con ...

  8. paper 72 :高动态范围(HDR)图像 HDR (High Dynamic Range)

    In standard rendering, the red, green and blue values for a pixel are each represented by a fraction ...

  9. 关于mybatis的参数2个使用经验(类似于struts2的通配所有页面的action配置,xmlsq语句参数类型为基本类型时的快捷指定办法)

    1.我们都知道在struts2中为防止浏览器绕过struts过滤器直接请求页面,所以我们都会配置一个拦截所有页面的action,如下: <action name="*"> ...

  10. 夺命雷公狗ThinkPHP项目之----企业网站12之文章添加的实现

    我们现在就开始写文章添加了,居然是添加当然布列外,我们还是要先讲模版搞定再说被: <!doctype html> <html> <head> <meta ch ...