poj2488 A Knight's Journey
来源:互联网 发布:淘宝大学鹰图学院 编辑:程序博客网 时间:2024/05/29 05:57
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.
dfs即可。
因为要字典序输出,所以每一步要按字典序枚举。
#include<cstdio>#include<cstring>int pa_x[30],pa_y[30],m,n,xx[8]={-2,-2,-1,-1,1,1,2,2},yy[8]={-1,1,-2,2,-2,2,-1,1};bool vis[30][30],ok;void dfs(int x,int y,int num){ if (x<1||x>m||y<1||y>n||vis[x][y]) return; pa_x[num]=x; pa_y[num]=y; vis[x][y]=1; if (num==m*n) { ok=1; return; } for (int i=0;i<8;i++) { dfs(x+xx[i],y+yy[i],num+1); if (ok) return; } vis[x][y]=0;}int main(){ int i,j,k,T; scanf("%d",&T); for (k=1;k<=T;k++) { memset(vis,0,sizeof(vis)); ok=0; scanf("%d%d",&n,&m); for (i=1;i<=m;i++) for (j=1;j<=n;j++) { dfs(i,j,1); if (ok) break; } printf("Scenario #%d:\n",k); if (ok) for (i=1;i<=m*n;i++) printf("%c%d",'A'+pa_x[i]-1,pa_y[i]); else printf("impossible"); printf("\n"); if (k<T) printf("\n"); }}
- poj2488 A Knight's Journey
- poj2488 - A Knight's Journey
- poj2488 A Knight's Journey
- POJ2488--A Knight's Journey
- POJ2488:A Knight's Journey
- poj2488 A Knight's Journey
- poj2488 A Knight's Journey
- poj2488---A Knight's Journey
- POJ2488---A Knight’s Journey
- poj2488 A Knight's Journey
- Poj2488 A Knight's Journey
- POJ2488 A Knight's Journey
- poj2488 A Knight's Journey
- poj2488 A Knight's Journey
- poj2488-A Knight's Journey
- POJ2488-A Knight's Journey
- poj2488 A Knight's Journey
- poj2488——A Knight's Journey
- 栈的演示1.
- Make 命令教程
- vectorLine画线
- android studio 如何打包成jar包
- AFNetworking 的核心 AFURLSessionManager(二)
- poj2488 A Knight's Journey
- sphinx 配置文件全解析
- C#编写的获取天气信息
- 一步一步解析集合框架ArrayList源码(2)
- 程序猿男票为何特别想要那个?
- Hive安装与使用
- 交流直流测试
- Hibernate和Spring整合时,获取Session失败
- 处理请求和响应 AFURLSerialization(三)