Pog.org 2488

Source: Internet
Author: User

/*
* Poj 2488
* It is good to traverse DFS, record the path that has passed, as long as you walk through the p * q square without repeating (end condition)
*/

# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace STD;
Const int max = 30;
Int Kase;
Int p, q;
Int vis [Max] [Max]; // mark the Array
// Direction array, just in the Lexicographic Order
Int dir [8] [2] = {-1,-2}, {1,-2}, {-2,-1}, {2,-1 }, {-2, 1}, {2, 1}, {-1, 2}, {1, 2}; // eight directions
Int path [Max * max] [2]; // used to record the path passed by DFS
Int flag;

Void DFS (int x, int y, int step)
{

If (step = p * q) // The p * q is finished.
{
Cout <"Scenario #" <++ Kase <":" <Endl;
For (INT I = 0; I <p * q; I ++)
{
Printf ("% C % d", path [I] [1] + 'A', path [I] [0] + 1 ); // note that the subscript of the recorded path starts from 0. According to my design, output the Y value first.
}
Cout <Endl;
Flag = 1;
Return;
}


For (int d = 0; D <8; D ++)
{
Int NX, NY; // only local variables can be made.
Nx = x + dir [d] [0];
NY = Y + dir [d] [1];
If (! Vis [NX] [NY] & NX> = 0 & NX <P & ny> = 0 & ny <q)
{
Vis [NX] [NY] = 1;
Path [STEP] [0] = NX;
Path [STEP] [1] = NY;
DFS (NX, NY, step + 1 );
Vis [NX] [NY] = 0; // unmark
If (FLAG)
Return;
}
}
}


Int main ()
{
Int T;
While (scanf ("% d", & T )! = EOF)
{
Kase = 0;
While (t --)
{
Flag = 0;
Memset (VIS, 0, sizeof (VIS ));
Scanf ("% d", & P, & Q );
Path [0] [0] = 0;
Path [0] [1] = 0;
Vis [0] [0] = 1;
DFS (0, 0, 1 );

If (! Flag)
{
Cout <"Scenario #" <++ Kase <":" <Endl;
Cout <"impossible" <Endl;
}
}
}
Return 0;
}


/*
Pay attention to the following issues:
1. Chess stands for letters and numbers.
2. It is output in Lexicographic Order, so pay attention to the search direction! This is a pitfall.
3. Ignore the phrase "the knight can start and end on any square of the board .".
It's a pitfall. In fact, you only need to start searching from Point A1. As long as you can start searching from point A1.
From any point on the Board as the starting point, you can find a path to traverse the board, and the question
It is required to output data in Lexicographic Order, so you only need to start searching from the start point!
*/


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.