(Hdu step 4.3.1) Tempter of the Bone (with specific time constraints, determine whether it can reach the end point from the start point), hdutempter

Source: Internet
Author: User

(Hdu step 4.3.1) Tempter of the Bone (with specific time constraints, determine whether it can reach the end point from the start point), hdutempter

Question:

Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1134 Accepted Submission (s): 379
 
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;
\\\\\\\\\\\\\\\ ': The Door; or
\\\\\\\ '. \\\\\\': An empty block.

The input is terminated with three 0 \\\\\\'s. This test case is not to be processed.
Output
For each test case, print in one line \ "YES \" if the doggie can keep ve, or \ "NO \" otherwise.
Sample Input
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 
Sample Output
NOYES
 
AuthorZHANG, Zheng
SourceZJCPC2004
RecommendJGShining


Question Analysis:

"Determining whether the problem can reach the end point from the starting point" can be easily solved using BFS. Then, this question is characterized by "time constraints ". In this case, we can use DFS. I wanted to write my own ideas. However, I found that someone on the Internet wrote more details than I was about to write. So let's take a look at others' conclusions.


  1. 1010 temp of the bone
  2. Sample input:
  3. 4 4 5
  4. S.X.
  5. . X.
  6. .. XD
  7. ....
  8. Problem:
  9. (1 ):
  10. When the current node fails to arrive, the stack pops up and refresh the mark '.'
  11. (2 ):
  12. In dfs, how does one ensure that the arrival time is correct ?? In the function, this form is implemented:
  13. Dfs (int si, int sj, int cnt) records the time with cnt, and
  14. If (si = di & sj = dj & cnt = t)
  15. {
  16. Escape = 1;
  17. Return;
  18. }
  19. When the current point reaches the end point and the time exactly equals the time limit given by the question
  20. And the escape is marked as true.
  21. (3 ):
  22. How can we make a point Traverse the points it can arrive around sequentially ??
  23. The smart and short method is to set a dir [4] [2] array to control the direction.
  24. Set its value to dir [4] [2] = {0,-1 }};
  25. It is very convenient to use for (I: 0-> 4) During traversal.
  26. (4 ):
  27. Note that when the node is out of bounds, when dfs (int si, int sj, int cnt), the si must be, in the matrix given to you by the sj control, a column is mentioned later because the position [0,-1] is accessed.
  28. His data is changed
  29. (5 ):
  30. When reading a matrix, you can use for (I = 1; I <= N; I ++)
  31. For (j = 1; j <= M; j ++)
  32. Scanf ("% c", & map [I] [j]);
  33. The advantage of this method is that it can control and compute each read data. The disadvantage is that it is not convenient to observe the matrix during debugging, and it seems that there are still errors, when 2102 "plan a" reads data using this method, it seems that wa will,
  34. Another method is for (I = 0; I <N; I ++) gets (map [I]);
  35. The data read in this way is very convenient for gets () to be read by default as a string during debugging and observation, during vc debugging, you can directly observe the matrix. The disadvantage is that the calculation and control of each data in the Matrix cannot be achieved. You need to read the data and traverse it again.
  36. (6)
  37. Using bfs or using bfs as much as possible I won't use bfs... dfs recursion in debugging is not very convenient, and bfs is faster than dfs, debugging is also convenient, because it does not have Recursion
  38. (7)
  39. It is impossible to search for pruning without pruning. In this case, Liu talked about two pruning methods during his class. One is parity pruning and the other is path pruning.
  40. Parity pruning:
  41. Mark the matrix as follows:
  42. 0, 1, 0, 0
  43. 1, 0, 1, 0, 1
  44. 0, 1, 0, 0
  45. 1, 0, 1, 0, 1
  46. Obviously, if the start point is 0 and the end point is 1, it takes an odd step to start from the start point to the end point, and so on. Even steps with the same parity and odd-even steps have different odd-even steps.
  47. When reading the data, you can judge and perform pruning. Of course, you do not need to refresh the entire matrix 0, 1. When reading the data, the start point is (Si, Sj) the end point is recorded as (Di, Dj) to judge the parity of (Si + Sj) and (Di + Dj ).
  48. Path pruning:
  49. The matrix size is the number of N * M walls. It is recorded as wall. If the number of walk routes N * M-wall is less than the time T, it means that the total time cannot be reached, this is obviously wrong.
  50. The courseware has provided the benchmark for this question. In the dfs process, there is a pruning not mentioned, that is, the shortest path from the current point to the end. If it is less than the remaining time, it will jump out.
  51. I think this pruning is more scientific. After all, is it Dynamic? It is written in the standard:
  52. Temp = (t-cnt)-abs (si-di)-abs (sj-dj );
  53. If (temp <0 | temp & 1) return;
  54. The shortest path from the current point to the end point is abs (si-di)-abs (sj-dj), which is rough, obviously not considering the situation of turning the wall.
  55. Is there any good way to find the shortest path?
  56. I used to think of the shortest short circuit using pair straq, which is obviously very small. I saw a method in the forum that can be used here.
  57. For example:
  58. S.X.
  59. . X.
  60. .. XD
  61. ....
  62. Is the shortest path from each point to the end:
  63. S6X2
  64. 65X1
  65. 54XD
  66. 4321
  67. How can this be solved ?? Traverse the entire array from the end point. The end point is 0, and the surrounding points are all + 1. The wall does not count, and so on. Then, a shortest time matrix of this matrix can be obtained, compare the shortest path from the current point to the end point in dfs.
  68. The Preprocessing of this method is still very fast. I have never used it, but it feels very useful.
  69. (8)
  70. When I was doing this, I encountered a magic thing.
  71. If (map [si + dir [I] [0] [sj + dir [I] [1]! = 'X ')
  72. Map [si + dir [I] [0] [sj + dir [I] [1] = 'X ';
  73. T changed !! This has nothing to do with T. How can we change the value of T ??
  74. When map [0] [0] started, I didn't notice that map [si + dir [I] [0] [sj + dir [I] [1] actually does map [0] [-1] =' x '; it's a dangerous assignment. I 've been in this place for a long time, so I think it's necessary to list it separately to remind myself.


The Code is as follows:

/**. Cpp ** Created on: February 24, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 9; char map [maxn] [maxn]; // map matrix. int n; // number of rows int m; // Number of columns int t; // destination time int x1, y1; // start point int x2, y2; // end point int dir [4] [2] = {// direction matrix {}, {-}, {}, {0,-1 }}; /*** determine whether the next step is valid */bool check (int x, int y) {if (x <0 | x> = n | y <0 | y> = m) {// return false if the coordinates are out of bounds; // It indicates that the next step is illegal} ret Urn true; // otherwise, the next step is valid}/*** DFS. * Si: number of rows of the current node * sj: Number of columns of the current node * cnt: Arrival (si, sj) is the time used */bool dfs (int si, int sj, int cnt) {if (si = x2 & sj = y2 & cnt = t) {// return true if the target point is reached within the specified time; // return true}/*** if the current node is out of bounds. This judgment is not required. * If this parameter is added, it may also time out. Because the cross-border judgment has been performed when the next node is generated * // if (check (si, sj) = false) {// return false; //}/*** t-cnt: indicates the remaining time * abs (x2-si) + abs (y2-sj ): time required from the current node path destination node */int temp = (t-cnt)-(abs (x2-si) + abs (y2-sj )); // Shortest Path pruning if (temp <0) {// return false if the remaining time is not enough to reach the target node; // return false, this path is not successful} // if (temp & 1) {return false ;}int I; for (I = 0; I <4; ++ I) {// traverse the adjacent node int tempi = si + dir [I] [0] of the current node; // calculate the coordinate int tempj = sj + dir [I] [1] of the next node; if (che Ck (tempi, tempj) = false) {// If the coordinates of the next vertex are not valid, continue; // skip this node, calculate next node} if (map [tempi] [tempj]! = 'X') {// if the current node is not a wall map [tempi] [tempj] = 'X'; // set the current node to a wall bool flag = dfs (tempi, tempj, cnt + 1); // search down this node if (flag = true) {// return true if this path is feasible; // return true. indicates that the target node can arrive} // when the following code is notified, the target node cannot be reached through this node map [tempi] [tempj] = '. '; // reset the current node '. 'availability status} return false; // If none of the above paths can be found. now, the target node cannot be reached.} int main () {while (scanf ("% d", & n, & m, & t )! = EOF, n) {int I; int j; for (I = 0; I <n; ++ I) {cin> map [I];} int wall = 0; // Number of walls in the village for (I = 0; I <n; ++ I) {for (j = 0; j <m; ++ j) {if (map [I] [j] = 's') {// record start position x1 = I; y1 = j ;} else if (map [I] [j] = 'D') {// record end position x2 = I; y2 = j ;} else if (map [I] [j] = 'X') {// count the number of walls. wall ++ ;}}} // path pruning int nums = n * m-wall; // calculates the number of accessible steps if (nums <t) {// if the number of walking steps <specified time printf ("NO \ n"); // It indicates that the path has been completed and has not reached the specified time. name returns false. this indicates that there is no such path continue;} map [x1] [y1] = 'X'; // The prize start point is set to 'X' bool result = dfs (x1, y1, 0); // traverse from the starting point if (result = true) {// if resultweighttrue, it indicates that there is such a path printf ("YES \ n ");} else {printf ("NO \ n"); // otherwise, this path does not exist.} return 0 ;}







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.