Poj2488-a Knight ' s Journey (dfs+ backtracking)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2488

A Knight ' s Journey
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36695 Accepted: 12462

Description

Background
The knight is getting bored of seeing, the same black and white squares again and again and have decided to make a journey
Around the world. Whenever a knight moves, it is the squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he was living on. Our knight lives in a chessboard that have a smaller area than a regular 8 * 8 board, but it's still rectangular. Can 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 on the first line. The following lines contain n test cases. Each of the test case consists of a single line with the 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 Ho W many different square letters exist. These is the first Q letters of the Latin alphabet:a, ...

Output

The output for every scenario begins with a line containing "scenario #i:", where I am the number of the scenario starting at 1. Then print a single line containing the lexicographically first path this visits all squares of the chessboard with Knight Moves followed by a empty line. The path should is given on a, the names of the visited squares by concatenating. Each square name is consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a.

Sample Input

31 12 34 3

Sample Output

Scenario #1: A1scenario #2: Impossiblescenario #3: A1B3C1A2B4C2A3B1C3A4B2C4

Main topic : Choose A starting point, according to the Chess Horse jumping method, do not repeat the entire board, if there are multiple routes, select the shortest Path of the dictionary (the alignment is a set of horizontal ordinate points, note the horizontal axis of the board in uppercase letters, ordinate is the number)

Topic Analysis:

1. You should see this topic can be thought of using DFS, when the first to understand the meaning of the question is whether to go only once ( do not look back and do not repeat ) the entire map to complete, and the ordinary depth of the first search is to go, and go back to somewhere along the road to continue deep search. So this question to use the backtracking thought , if do not repeat walk over, do a mark, algorithm stop; otherwise in some kind of Dfs go to a certain step, according to the rules of horse jumping no way to go and the board is also for the point to go, so we need to undo This step, Then try other routes (of course, other routes may also lead to revocation), and the so-called undo step is to reset the point when the recursive deep search returns, so that the state of the point is not accessed when the current route is not available for another route, and not as if the normal DFS has been accessed.

2. If there are many ways to walk through without repeating, you need to output the path of the smallest dictionary order, and notice that the chess board is listed as the letter, the number of acts, if you can not go back and walk through the walk, will certainly after A1 points, we should start the search from A1 to ensure that the resulting path dictionary is minimal ( This means that if the path does not start with A1, the path must not be the dictionary-ordered least path, and we should ensure that the preferred direction is the smallest direction of the dictionary order, so that the first path we get is the smallest dictionary order.

Reference code:

#include <cstdio>#include<cstring>using namespacestd;Const intMax_n = -;//the smallest walking direction of dictionary orderConst intdx[8] = {-1,1, -2,2, -2,2, -1,1}; Const intdy[8] = {-2, -2, -1, -1,1,1,2,2};BOOLVisited[max_n][max_n];structstep{Charx, y;} Path[max_n];BOOLSuccess//whether the tag was successfully traversedintcases, p, q;voidDasointXintYintnum);intMain () {scanf ("%d", &cases);  for(intc =1; c <= cases; C++) {Success=false; scanf ("%d%d", &p, &q); Memset (visited,false,sizeof(visited)); visited[1][1] =true;//starting pointDFS (1,1,1); printf ("Scenario #%d:\n", c); if(Success) { for(inti =1; I <= p * q; i++) printf ("%c%c", Path[i].y, path[i].x); printf ("\ n"); }        Elseprintf ("impossible\n"); if(c! =cases) printf ("\ n");//note the line break for the problem    }    return 0;}voidDFS (intXintYintnum) {PATH[NUM].Y= y +'A'-1;//Int converted to charpath[num].x = x +'0'; if(num = = p *q) {success=true; return; }     for(inti =0; I <8; i++)    {        intNX = x +Dx[i]; intNY = y +Dy[i]; if(0< NX && NX <= p &&0< NY && NY <=Q&&!visited[nx][ny] &&!success) {Visited[nx][ny]=true; DFS (NX, NY, Num+1); Visited[nx][ny]=false;//Undo This step        }    }}

Poj2488-a Knight ' s Journey (dfs+ backtracking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.