Hdu1010 search + pruning

Source: Internet
Author: User
Tempter of the bone

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 42078 accepted submission (s): 11382


Problem descriptionthe doggie found a bone in an alert ent maze, which fascinated him a lot. however, when he picked it up, the maze began to shake, and the doggie cocould feel the ground sinking. he realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. there was a door in the maze. at the beginning, the door was closed and it wocould open at the T-th second for a short period of time (less than 1 second ). therefore the doggie had to arrive at the door on exactly
T-th second. in every second, he cocould move one block to one of the upper, lower, left and right neighboring blocks. once he entered a block, the ground of this block wocould start to sink and disappear in the next second. he cocould not stay at one block
More than one second, nor cocould he move into a visited Block. Can the Poor doggie keep ve? Please help him.


Inputthe input consists of multiple test cases. the first line of each test case contains three integers n, m, and T (1 <n, m <7; 0 <t <50 ), which denote the sizes of the maze and the time at which the door will open, respectively. the next n lines give
Maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;
'S ': the start point of the doggie;
'D': the door; or
'.': An empty block.

The input is terminated with three 0's. This test case is not to be processed.


Outputfor each test case, print in one line "yes" if the doggie can have ve, or "no" otherwise.


Sample Input

4 4 5 s.x... X... XD... 3 4 5 s.x... X... d 0 0 0 


Sample output

No Yes 

This question needs to be explained. I have never heard of parity pruning before. All kinds of attention,

DFS uses the Backtracking framework for # include <iostream>
# Include <cmath>
Using namespace STD;
Int n, m, T;

Int time_used;

Char map [7] [7];
Int dirx [4] = {-1, 0, 1, 0 };
Int diry [4] = {0, 0,-1 };
Int Sx, Sy;
Int ex, ey;
Bool flag;
Int count;

Void DFS (int x, int y)
{
Int XX, YY;
If (FLAG)
Return;
If (x = ex & Y = ey) & (time_used = t) // exit when conditions are met
{
Flag = true;
Return;
}
If (T-time_used) % 2! = (ABS (X-Ex) + ABS (Y-ey) % 2) // parity Reduction Method
Return;
If (ABS (X-Ex) + ABS (Y-ey)> T-time_used) // the shortest time from the current point to the end is longer than the remaining time
Return;


For (INT I = 0; I <4; I ++) // expands to four directions
{
Xx = x + dirx [I];
YY = Y + diry [I];
If (XX> = 0) & (XX <n) & (yy> = 0) & (yy <m) & map [XX] [YY]! = 'X ')
{

Map [x] [Y] = 'X ';
Time_used ++;
DFS (XX, YY );
Time_used --;
Map [x] [Y] = '.';

}
}
}

Int main ()
{

While (CIN> N> m> T)
{
If (! N &&! M &&! T)
Break;
Count = 0;
For (INT I = 0; I <n; I ++)
{
For (Int J = 0; j <m; j ++)
{
Cin> map [I] [J];
If (Map [I] [J] ='s ')
{
SX = I;
Sy = J;
}
If (Map [I] [J] = 'D ')
{
Ex = I;
Ey = J;
}
Else if (Map [I] [J] = '.')
{
Count ++;
}
}

}
If (count + 1 <t) // if the total number of available steps is less than the given time
{
Cout <"no" <Endl;
Continue;
}
Time_used = 0;
Flag = false;
DFS (sx, Sy );
If (flag = true)
Cout <"yes" <Endl;
Else
Cout <"no" <Endl;

}
Return 0;
}

Extension: parity pruning is a special trick in Data Structure search. Assume that the starting point is (sx, Sy) and the ending point is (ex, ey). The given t step is just the end point,

S
|
|
|
+ - - - E

("|" Vertical, "-" horizontal, "+" turns), E-certificate ABS (ex-SX) + ABS (ey-sy) in any situation for this question, the minimum number of steps from the start point to the end point. Record the step. Step 1 = 8;

S - - -
- - +
| +
|
+ - - - E

For example, if the path is not the shortest path, step2 = 14; step2-step1 = 6, the offset path is 6, even (easy to prove, if T-[ABS (ex-SX) + ABS (ey-sy)] is not an even number (odd number), it cannot arrive exactly in step t; return, false; the opposite is true.

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.