poj 2488 A Knight's Journey 【dfs】【字典序】【刷題計劃】
阿新 • • 發佈:2017-11-27
different step center latin repr 順序 void 水題 eof A Knight‘s Journey
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 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.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 47516 | Accepted: 16161 |
Description
BackgroundThe 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
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
題意:在一個n*m的國際棋盤上,象棋以馬走日的方式走,行數為1-n的數字,列數為A-A+m的字母,按照字典序輸出一條合適的路徑。
思路:先構造一個棋盤數組和一個標記數組,按照字典序的方式就是先走字典序優先的位置,所以在定義方向數組的時候需要留意方位順序,最後,每個樣例後都一個空行。
喵?喵?喵?這道題的字典序操作真是毀我青春,我一直以為字典序就是先按照字母表順序輸出一遍再從頭開始,然後就躺屍在這道題上,今天師父一看我屏幕,感嘆這種水題居然還沒有寫過(畢竟上個周我就纏著師父給我講為啥錯了),最後師父才發現,原來我徒弟連字典序都不會(嗚嗚嗚,誰知道是這樣的字典序啊,樣例誤導人~),師父講完字典序以後我就A過啦~
#include<stdio.h> #include<string.h> #define N 100 int map[N][N],m,n,flag; char str[N][N]; struct node1{ int x; char y; }l[N*N]; struct node2{ int x; char y; }f[N*N]; void dfs(int x,int y,int step) { int k[8][2] = {-1,-2,1,-2,-2,-1,2,-1,-2,1,2,1,-1,2,1,2}; int nx,ny,i; if(step > n*m)//在找到合適路徑的情況下 { flag = 1; for(i = 1; i <= n*m; i ++) { f[i].x = l[i].x ;//將臨時數組的值存入最終數組 f[i].y = l[i].y ; } return; } else { for(i = 0; i < 8; i ++) { if(!flag)//在未找到合適路徑的情況下 { nx = x + k[i][0]; ny = y + k[i][1]; if(nx < 1||ny < 1||nx > n||ny > m||map[nx][ny] == 1) continue; map[nx][ny] = 1;//標記為已經訪問過 l[step].x = nx;//用臨時數組存儲下標 l[step].y = str[nx][ny]; dfs(nx,ny,step +1);//往下搜索 map[nx][ny] = 0;//回溯 } } } } int main() { int i,j; int t,t2=0; scanf("%d",&t); while(t --) { scanf("%d%d",&n,&m); flag = 0; memset(map,0,sizeof(map));// memset(l,0,sizeof(l));// memset(f,0,sizeof(f));//初始化 for(i = 1; i <= n; i ++) { char ch = ‘A‘; for(j = 1; j <= m; j ++) str[i][j] = ch ++; } l[1].x = 1; l[1].y = ‘A‘; map[1][1] = 1;//將初識下標初始化為訪問過 dfs(1,1,2); printf("Scenario #%d:\n",++t2); if(!flag) printf("impossible\n\n"); else { for(i = 1; i <= n*m; i ++) printf("%c%d",f[i].y ,f[i].x ); printf("\n\n"); } } return 0; }
poj 2488 A Knight's Journey 【dfs】【字典序】【刷題計劃】