A Knight's Journey
来源:互联网 发布:webpackconfig.js 编辑:程序博客网 时间:2024/05/16 13:02
A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 43095 Accepted: 14619
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.
If no such path exist, you should output impossible on a single line.
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
Source
TUD Programming Contest 2005, Darmstadt, Germany
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<stdlib.h>#include<time.h>#include<math.h>#include<queue>;#include<stack>;#include <iomanip>;using namespace std;int t,b[100][2],v[10][10],n,m,p;int dir[8][2]= {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};int go(int x,int y){ if(1<=x&&x<=n&&1<=y&&y<=m&&!v[x][y]&&!p) return 1; return 0;}void dfs(int x,int y,int c){ b[c][0]=y; b[c][1]=x; if(c==n*m) { p=1; return; } for(int i=0; i<8; i++) { int ex=x+dir[i][0]; int ey=y+dir[i][1]; if(go(ex,ey)) { v[ex][ey]=1; dfs(ex,ey,c+1); v[ex][ey]=0; } }}int main(){ scanf("%d",&t); for(int i=1; i<=t; i++) { p=0; scanf("%d%d",&n,&m); memset(v,0,sizeof(v)); printf("Scenario #%d:\n",i); v[1][1]=1; dfs(1,1,1); if(p==1) { for(int i=1; i<=n*m; i++) { printf("%c%d",b[i][0]+'A'-1,b[i][1]); } } else printf("impossible"); printf("\n"); if(i!=t) printf("\n"); }}
0 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
- angular js 17年1月小结 (包括简单的指令和模块 服务等)
- 从web.xml入手分析jeecms配置文件
- 小数点位数的处理
- Linux Shell脚本去掉几类常见文件中的注释
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming! 哈希
- A Knight's Journey
- ios image not found
- Php基础语法
- Linu学习笔记之——ubuntu安装、卸载JDK1.7
- Spark的一些配置总结
- elasticsearch入门教程二(基于restful 针对5.1版本)
- 对不起,给不了你太多
- Android 使用CoordinatorLayout实现滚动标题栏效果
- JavaWeb总结十五、JSP基础用法