首页 > 代码库 > poj 2488 -- A Knight's Journey
poj 2488 -- A Knight's Journey
A Knight‘s Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30388 | Accepted: 10404 |
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
题目大意: 让骑士以字典序遍历整个图,每个点只能到一次。
思路:数据不大,直接搜索即可。注意每个测试数据后面都有一个空行。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : AKnightsJourney.cpp 4 * Creat time : 2014-07-31 16:22 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio>10 #include <cstring>11 #include <queue>12 #include <cmath>13 #define clr(a,b) memset(a,b,sizeof(a))14 #define M 3015 using namespace std;16 struct Node17 {18 int x,y;19 }steps[M*M];20 bool vis[M][M];21 int p,q;22 int dir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};23 int DFS(int x,int y,int cnt)24 {25 if(vis[x][y]){26 return -1;27 }28 vis[x][y] = true;29 steps[cnt].x = x; steps[cnt].y = y;30 if(cnt == p*q-1) return 1;31 for(int i = 0; i < 8; i++){32 int xx = x+dir[i][0];33 int yy = y+dir[i][1];34 if(xx >= 1 && xx <= p && yy >= 1 && yy <= q){35 int ans = DFS(xx,yy,cnt+1);36 if(ans == 1){37 return true;38 }39 else if(ans == 0){40 vis[xx][yy] = false;41 }42 }43 }44 return 0;45 }46 int main(int argc,char *argv[])47 {48 int n,t = 1;49 scanf("%d",&n);50 while(n--){51 clr(steps,0);52 scanf("%d%d",&p,&q);53 int cnt = 0;54 printf("Scenario #%d:\n",t++);55 int flag = 0;56 for(int i = 1; i <= q; i++){57 for(int j = 1; j <= p; j++){58 clr(vis,0);59 if(DFS(j,i,cnt) == 1){ //因为i与j位置反了,贡献了几次wr60 flag = 1;61 break;62 }63 }64 if(flag) break;65 }66 if(!flag){67 printf("impossible");68 }69 else{70 for(int i = 0; i < q*p; i++){71 printf("%c%d",steps[i].y-1+‘A‘,steps[i].x);72 }73 }74 printf("\n\n");75 }76 return 0;77 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。