HDU-1010-Tempter of the bone

Source: Internet
Author: User

Question Link

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1010

The question is that a dog eats bones, and it enters a maze trap. Every time a floor passes through the maze, the floor will collapse in the next second, so you cannot stay on the floor. There is a door in the maze that can only be opened at a specific second to let the dog escape. Now the question tells you the size of the maze and the time when the door is opened. If you ask your dog if you can escape, you can output yes; otherwise, no.

 



Pruning used for search:

1. If the current time is the number of steps (STEP)> = T and no dpoint is found, cut it.

2. set the shortest distance from the current location (x, y) to the dpoint (dx, Dy) to S. It takes time (Steps) to reach the current location (x, y, if the time required by the question is T-step <s, it is cut off.

3. For the current position (x, y), if (t-step-S) is an odd number, it is cut (parity pruning ).

4. If the number of points that can be taken in the map (xnum) <required time t, then cut (path pruning)

 

# Include <stdio. h>

# Include <stdlib. h>

Int n, m, ex, ey, T;

Int success;

Char maze [10] [10];

Void DFS (int stx, int sty, int DT)

{

If (Stx <= 0 | STX> N | sty <= 0 | sty> m)

Return;

If (Stx = ex & sty = ey & dt = T)

{

Success = 1;

Return;

}

Int temp = (t-Dt)-ABS (ex-STX)-ABS (ey-STY );

If (temp <0 | temp & 1)

Return;

If (maze [STX] [sty + 1]! = 'X ')

{

Maze [STX] [sty + 1] = 'X ';

DFS (Stx, sty + 1, DT + 1 );

Maze [STX] [sty + 1] = '.';

}

If (maze [STX] [sty-1]! = 'X ')

{

Maze [STX] [sty-1] = 'X ';

DFS (Stx, Sty-1, DT + 1 );

Maze [STX] [sty-1] = '.';

}

If (maze [STX + 1] [sty]! = 'X ')

{

Maze [STX + 1] [sty] = 'X ';

DFS (Stx + 1, sty, DT + 1 );

Maze [STX + 1] [sty] = '.';

}

If (maze [stx-1] [sty]! = 'X ')

{

Maze [stx-1] [sty] = 'X ';

DFS (stx-1, sty, DT + 1 );

Maze [stx-1] [sty] = '.';

}

}

 

Int main (void)

{

Int STX, sty, wall, I, J;

While (scanf ("% d", & N, & M, & T) = 3 & (n + M + T ))

{

Getchar ();

Wall = 0;

For (I = 1; I <= N; I ++)

{

For (j = 1; j <= m; j ++)

{

Scanf ("% C", & maze [I] [J]);

If (maze [I] [J] ='s ')

{

STX = I;

Sty = J;

}

Else if (maze [I] [J] = 'D ')

{

Ex = I;

Ey = J;

}

Else if (maze [I] [J] = 'X ')

Wall ++;

}

Getchar ();

}

Success = 0;

Maze [STX] [sty] = 'X ';

If (N * m-wall <= T)

Printf ("NO \ n ");

Else

{

DFS (Stx, sty, 0 );

If (SUCCESS)

Printf ("Yes \ n ");

Else

Printf ("NO \ n ");

}

}

Return 0;

}

 

 



 
What is parity pruning?

Take the matrix as follows:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
Take a step from a grid with 0 to a grid with 1.
A step from a grid with 1 is bound to a grid with 0.
That is:
Moving from 0 to 1 must be an odd step, and moving from 0 to 0 must be an even step.

Therefore, when the request is from 0 to 0 but requires an odd number of times or from 1 to 0 but requires an even number of times, you can directly determine that the request is not reachable!

 

For example, there is a map:

 

[C-sharp]View plaincopy
  1. S...
  2. ....
  3. ....
  4. ....
  5. ... D

 

The shortest distance from S to D is S = ABS (DX-SX) + ABS (dy-sy ).

If a map contains obstacles that cannot pass through:

 

[C-sharp]View plaincopy
  1. S.. x
  2. XX. x
  3. ... X
  4. . Xxx
  5. ... D

 

At this time, the shortest distance s '= S + 4. to bypass the obstacle, no matter the offset points, the offset distance is the shortest distance s plus an even distance.

Just like the matrix mentioned above, it requires you to go from 0 to 0. No matter how you go around, it will always be the shortest distance (even step) plus an even step. It requires you to go from 1 to 0, you can only add an even number to the shortest distance (odd step.

 

 

You can also search for parity pruning.

 

HDU-1010-Tempter of the bone

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.