Poj2488【深搜】

来源:互联网 发布:异步网络框架 编辑:程序博客网 时间:2024/06/06 00:33
/*A Knight's JourneyTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 31802Accepted: 10839DescriptionBackground 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.InputThe 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, . . .OutputThe 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.Sample Input31 12 34 3Sample OutputScenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4SourceTUD Programming Contest 2005, Darmstadt, Germany*/ #include<stdio.h>#include<string.h>int path[1000][2], vis[26][26], n, m, dx[8] = {-1, 1,-2,2,-2,2,-1,1},dy[8]  = {-2,-2,-1,-1,1,1,2,2}, k, ok;  // 横向字母竖向数字 void dfs(int x,int y){int i, j;if(k == n*m){ok = 1;return;}for(i = 0; i < 8; i++){int nx = x+dx[i], ny = y+dy[i];//八个方向搜索 if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && !ok)//注意ok标记一定要放到循环内也作为判断标记 {                                                             //否则有可能某个方向找到答案,却还在搜索其他方向覆盖了路径 vis[nx][ny] = 1;k++;path[k][0] = nx;path[k][1] = ny;dfs(nx,ny);k--;          //backtrackvis[nx][ny] = 0;}}}int main(){int T, cas = 1;scanf("%d", &T);while(T--){scanf("%d%d", &n,&m);memset(vis,0,sizeof(vis));k = 1;ok = 0;vis[0][0] = 1;path[1][0] = 0;path[1][1] = 0;//初始化从A1开始出发 dfs(0,0);printf("Scenario #%d:\n",cas++);if(ok){for(int i = 1; i <= n*m; i++)printf("%c%d", path[i][1]  + 'A', path[i][0]+1);printf("\n\n");}elseprintf("impossible\n\n");}return 0;}

题意 :给出一个棋盘,横向和纵向分别是由字母和数字排序的,都是从小到大,起点就是从字典序最小的那个点,问是否能将整个棋盘的每个点都走一次,若可以则将其路径按照字典序从小到大输出。

思路:采用深搜的方法,其次需要注意的是因为输出路径要是最小的,所以遍历的八个方向必须也是按照字典序从小到大哦的方向开始遍历。

难点:个人感觉此题的难点就在于读题上面,很难理解透。

0 0
原创粉丝点击