Tempter of the bone
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 23769 accepted submission (s): 6538
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 the 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 coshould not stay at one block for more than one second, nor coshould 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 the 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 input4 4 5
S.x.
. X.
.. XD
....
3 4 5
S.x.
. X.
... D
0 0 0
Sample outputno
Yes
Authorzhang, Zheng
Sourcezjcpc2004
Recommendjgshining DFS note branch reduction
# Include < Iostream >
# Include < Queue >
# Include < String . H >
# Include < Math. h >
Using Namespace STD;
Struct Node
{
Int X, Y, T;
};
Int DX [] = { 1 , 0 , - 1 , 0 };
Int Dy [] = { 0 , 1 , 0 , - 1 };
Int SX, Sy;
Int N, m, T;
Char Map [ 10 ] [ 10 ];
Bool CNT;
Int Num;
Void DFS (node P)
{
If (CNT) Return ;
If (P. x = SX && P. Y = Sy && P.t = T) {CNT = True ; Return ;}
If (P. t > = T) Return ;
Int Min = ( Int ) (FABS (( Double ) (P. x - SX )) + FABS (( Double ) (P. Y - Sy )));
If (Min > T - P. t) Return ;
If (Min % 2 ! = (T - P. t) % 2 ) Return ;
// If (Map [SX + 1] [sy]! = '.' & Map [SX-1] [sy]! = '.' & Amp; Map [SX] [SY + 1]! = '.' & Map [SX] [SY-1]! = '.') Return;
If (P. x = SX && P. Y = Sy) Return ;
Node TMP;
For ( Int I = 0 ; I < 4 ; I ++ )
{
TMP. x = P.x + DX [I];
TMP. Y = P. Y + Dy [I];
TMP. t = P.t + 1 ;
If (CNT | TMP. x < 1 | TMP. x > N | TMP. Y < 1 | TMP. Y > M | Map [TMP. x] [TMP. Y] = ' X ' ) Continue ;
If (TMP. x = SX && TMP. Y = Sy && TMP. t = T) {CNT = True ; Return ;}
Char Ch = Map [TMP. x] [TMP. Y];
Map [TMP. x] [TMP. Y] = ' X ' ;
DFS (TMP );
Map [TMP. x] [TMP. Y] = Ch;
}
}
Int Main ()
{
Node P;
While (Scanf ( " % D " , & N, & M, & T) ! = EOF)
{
If (N = 0 && M = 0 && T = 0 ) Break ;
Memset (map, 0 , Sizeof (MAP ));
Num = 0 ;
For ( Int I = 1 ; I <= N; I ++ )
For ( Int J = 1 ; J <= M; j ++ )
{
CIN > Map [I] [J];
If (Map [I] [J] = ' S ' )
{P. x = I; p. Y = J; P. t = 0 ;}
Else If (Map [I] [J] = ' D ' )
{SX = I; SY = J ;}
Else Num ++ ;
}
If (Num < T - 1 ) {Cout < " No " < Endl; Continue ;}
Map [p. x] [P. Y] = ' X ' ;
CNT = False ;
DFS (P );
If (CNT) cout < " Yes " < Endl;
Else Cout < " No " < Endl;
}
Return 0 ;
}