POJ 2488 A Knight's Journey
2016-04-20 21:26
288 查看
D - A Knight's Journey
Time Limit:1000MS Memory Limit:65536KB 64bit
IO Format:%I64d & %I64u
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
Sample Output
题意:给出一个规格为8 * 8的棋盘,一个骑士可以从任意一个格子开始走,并且只能走“日”,问骑士能否不重复的走完整个棋盘。如果能,输出按字典序排列最小的路径,若不能,输出“impossible"。
Time Limit:1000MS Memory Limit:65536KB 64bit
IO Format:%I64d & %I64u
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
题意:给出一个规格为8 * 8的棋盘,一个骑士可以从任意一个格子开始走,并且只能走“日”,问骑士能否不重复的走完整个棋盘。如果能,输出按字典序排列最小的路径,若不能,输出“impossible"。
分析:假设骑士可以不重复的走完整个棋盘,那么他一定可以走到A1这个格子,所以我们只需从A1开始搜索,并且控制好骑士移动的方向,得到的一定是字典序最小的路径。控制好条件后,就直接从A1进行深搜就可以了,记得将搜过的地方进行标记。<pre name="code" class="cpp">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int flag,m,n; int visit[30][30]; int v[30][2]; //搜索方向 int go[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; //打印 void print(int sum) { for(int i = 0;i < sum;i++) { printf("%c%d",v[i][0] - 1 + 'A',v[i][1]); } printf("\n"); } void dfs(int x,int y,int sum) { if(flag) //已经搜到答案,结束搜索 return ; if(sum == n * m) //搜到答案 { flag = 1; print(sum); return ; } else { //对8个方向进行搜素 for(int i = 0;i < 8;i++) { //移动之后的位置记为(nx,ny) int nx = x + go[i][0]; int ny = y + go[i][1]; //判断是否可以移动以及是否访问过 if(1 <= nx && nx <= n && 1 <= ny && ny <= m && visit[nx][ny] == 0) { visit[nx][ny] = 1;//标记为1 v[sum][0] = nx; //记录位置 v[sum][1] = ny; dfs(nx,ny,sum + 1); //搜索下一个位置 visit[nx][ny] = 0;//取消标记 } } } } int main() { int t; int cont = 0; scanf("%d",&t); while(t--) { memset(visit,0,sizeof(visit));//初始化 memset(v,0,sizeof(v)); scanf("%d %d",&m,&n); flag = 0; visit[1][1] = 1; v[0][0] = 1; v[0][1] = 1; if(cont++ > 0) printf("\n"); printf("Scenario #%d:\n",cont); dfs(1,1,1); if(flag == 0) printf("impossible\n"); } return 0; }
相关文章推荐
- 数据库的事务四大特性
- Reverse Linked List II
- ASP.NET连接数据库并获取数据
- 清华EMBA课程系列思考之八 -- 营销管理
- 勤奋
- 最小生成树---Prim算法和Kruskal算法
- Linux make与cmake的区别
- python中字符串的ljust、rjust、center方法讲解
- 枚举类型的使用方法
- 为什么一款优秀的企业协同办公软件一定要有windows客户端
- Android 图片转化工具类BitmapUtil
- LeetCode_9_Palindrome Number
- C++ 11(一)
- python中字符串的ljust、rjust、center方法讲解
- Map的4种遍历方法
- 为什么一款优秀的企业协同办公软件一定要有windows客户端
- 颓废
- 冲刺阶段第二天
- ACM:数论专题(3)——约瑟夫问题
- Hrb1924