A Knight's Journey
来源:互联网 发布:java执行sql语句 编辑:程序博客网 时间:2024/06/09 15:34
A Knight's Journey
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 188 Accepted Submission(s) : 40
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.
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. <br>If no such path exist, you should output impossible on a single line.
Sample Input
31 12 34 3
Sample Output
Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4
题目大意:给出一个棋盘的规格,问你能否在不重复的情况下吧每一个方格都走一遍,按照国际象棋的走法,东南西北只走日,按字典序输出
思路:广搜记录路径
注意横坐标是字母,纵坐标数字,按照字典序(ABC),以及搜索及时停止,输出,基本问题不大
- Source Code
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;struct chess{ int x; int y; };int dx[]={-1,1,-2,2,-2,2,-1,1};int dy[]={-2,-2,-1,-1,1,1,2,2};bool vis[101][101];chess ans[101];char zimubiao[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};int flag;int n,m; int judge(int x,int y) { if(x>0&&x<=n&&y>0&&y<=m) return 1; return 0; }void dfs(int x,int y,int num){ int i,j,nextx,nexty; ans[num].x=x; ans[num].y=y; if(flag)return ; if(num==n*m) { for(i=1;i<=n*m;i++) { cout<<zimubiao[ans[i].y-1]<<ans[i].x; } cout<<endl; flag=1; return ; } if(flag)return ; for(i=0;i<8;i++) { nextx=x+dx[i]; nexty=y+dy[i]; if(judge(nextx,nexty)==1&&vis[nextx][nexty]==0) { vis[nextx][nexty]=1; dfs(nextx,nexty,num+1); vis[nextx][nexty]=0; } } }int main(){ int t; int c=1,i,j; cin>>t; for(;c<=t;c++) { cin>>n>>m; memset(vis,0,sizeof(vis)); flag=0; vis[1][1]=1; cout<<"Scenario #"<<c<<":"<<endl; dfs(1,1,1); if(flag==0)cout<<"impossible"<<endl; if(c<=t)cout<<endl; } return 0; }
阅读全文
1 0
- A Knight's Journey
- A Knight's Journey
- A knight's journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- A Knight's Journey
- pku2488 A Knight's Journey
- 贪心,递归,动态规划,及分治算法之间的区别和联系(二)
- spark之5:配置文件
- 练习 2-5 编写函数 any(s1, s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。
- ubuntu16 jdk7 install&config
- BZOJ 1951 lucas定理 中国剩余定理
- A Knight's Journey
- 继承和多态
- GAWK 学习:AWK 语言基础
- 使用回调函数实现一个简单的计算器;
- Java8中内置的四大核心函数式接口
- [挖坑][uoj]多项式乘法 FFT
- JZOJ2017.08.05 C组
- 【凸包 Graham法 极角排序】poj 2007 Scrambled Polygon
- 1.在win10中搭建Java环境