POJ3009-Curling 2.0

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1303548481

 

General question:

Haha, those who have played "GBA pocket monster" should be very easy to understand when they see this question. "The game is just written like this." O (strong _ strong) o ha ~

 

It is hard to understand the meaning of the question:

It is required to move an curling pot from the start point "2" to the end point "3" with the minimum number of steps"

Where 0 is the moving area and 1 is the stone area. Once you think about a direction, the curling will not stop or change the direction (think about sliding the curling on the ice ), unless the curling hits stone 1 or ends at 3

 

Note:

After the curling hits the rock, the curling will stop in front of the rock. At this time (static state), the direction of the curling can be changed. The rock will break down and the area where the rock is located will change from 1 to 0. that is to say, after the curling hits the stone, it will not replace the stone position.

The endpoint is an area with a large friction. If the curling reaches the endpoint 3, it stops moving at the endpoint.

 

Solution:

In fact, this is not so much a deep search question, I think it is more like a simulation question...

Make it clear:

0 indicates the sliding area.

1 is the stone area

2 is the starting point and the sliding Area

3 is the end point and cannot be moved.

 

(1) The starting point is "2", which is also a slide area. Therefore, after marking the starting point, you can regard the starting point as 0.

(2) Pay attention to distinguish whether the curling is moving or static. If it is still, the stone next to it cannot go.

(3) the shortest path from 2 to 3 is output. If the shortest path is greater than 10 (not including 10), it is deemed that the shortest path cannot reach the end point.(In fact, this is used for pruning)

(4) The curling cannot exit during sliding

Based on the above principles, it is not difficult to find:

(1) the so-called"Step by step", RefersThe curling starts from a static state to the next static state.That is to say, the "number of cells" passed by the curling during exercise is not regarded as "number of steps", that is to say, the distance between the curling every movement is not fixed.

(2) The stone disappears because of the collision of the curling, so the environment of the curling will change once every "Step.

(3) Based on (2), it can be found that although this question is to find the "most short circuit", but BFS is almost impossible, because every "Step", the site status will change once; if this step does not meet the requirements, you need to restore the site status to the previous step, which can only be done by DFS.

(4) based on (3), DFS is not BFS and cannot be used simply to find the shortest path. All possible paths must be found one by one, compare the steps one by one to determine the shortest. However, if the question value is 1000 ms, a timeout problem occurs. Therefore, the question gives the condition that "if the number of steps exceeds 10, it is regarded as a failure". This is usedPruningOf

 

With the above analysis, we can finally determine the solution to this question:

There are two methods:

DFS + vector + pruning

DFS + backtracking + pruning

 

However, due to the poor compatibility of poj with STL, I have never met the topic "tle" with STL... This is also the same, so it is not recommended. In fact, vector is very useful. With its features, it can automatically restore the site status when DFS returns to the previous step, the backtracking rule needs to manually restore the site status (in fact, 8 lines of code)

 

DFS is used to find the path, backtracking (or vector) is used to restore the Board state, and pruning is used to optimize

 

In addition, if you define the curling as a "Dynamic and Static" state, you can search for it in the unit of "Grid". For details, see my program, I think this is relatively simple. I feel very annoying when I use for to find the number of cells in each step.

Below I will post both types of code for your reference.

 

/* DFS + backtracking + pruning * // memory time // 188 K 329 MS # include <iostream> using namespace STD; const int INF = 11; typedef class {public: int R, C; // the current position of the curling bool status; // status the current status of the curling: True, false} se; se S, E; // record int W, h at the start and end of the curling; // sizeint minstep at the site; // The shortest short-circuit int Board [30] [30]; // The site void DFS (int I, int J, bool status, int direction, int step, bool flag) {// direction: curling current motion direction north: 0 West: 1 South: 2 East: 3 // flag: whether to eliminate the next position in the direction of direction If (Step> 10) // pruning, return is not taken into account for any walk in more than 10 steps. If (Board [I] [J] = 3) // end {If (minstep> step) minstep = step; return;} If (FLAG) // remove stone {Switch (Direction) {Case 0: {board [I-1] [J] = 0; break;} Case 1: {board [I] [J-1] = 0; break;} Case 2: {board [I + 1] [J] = 0; break;} Case 3: {board [I] [J + 1] = 0; break ;}}} if (! Status) // static {If (I-1> = 1 & (Board [I-1] [J] = 0 | Board [I-1] [J] = 3 )) // northdfs (I-1, J, true, 0, step + 1, false ); if (J-1> = 1 & (Board [I] [J-1] = 0 | Board [I] [J-1] = 3) // westdfs (I, j-1, true, 1, step + 1, false ); if (I + 1 <= H & (Board [I + 1] [J] = 0 | Board [I + 1] [J] = 3 )) // southdfs (I + 1, J, true, 2, step + 1, false ); if (J + 1 <= W & (Board [I] [J + 1] = 0 | Board [I] [J + 1] = 3 )) // eastdfs (I, J-1, true, 3, step + 1, false);} else if (Status) // motion {swit CH (Direction) {Case 0: {If (I-1 <1) // pre-determine whether the next step exceeded return; else {If (Board [I-1] [J] = 0) // The next position is 0 and does not cross the border, continue to exercise DFS (I-1, J, true, 0, step, false ); else if (Board [I-1] [J] = 1) // The next position is 1 and does not cross the border, stop the movement, and remove the next position of the stone DFS (I, j, false, 0, step, true); else if (Board [I-1] [J] = 3) // The next position is 3 and does not cross the border, stop Motion After movement to position 3, game end DFS (I-1, J, false, 0, step, false);} break;} Case 1: {If (J-1 <1) // pre-determine whether the next step is out of bounds return; else {If (Board [I] [J-1] = 0) // The next position is 0 and not out of bounds, continue to motion DFS (I, j-1, true, 1, step, Fa LSE); else if (Board [I] [J-1] = 1) // The next position is 1 and does not cross the border, stop the motion, and remove the next position of the stone DFS (I, j, false, 1, step, true); else if (Board [I] [J-1] = 3) // The next position is 3 and does not cross the border, the movement to the position 3 after the stop motion, the game ends DFS (I, J-1, false, 1, step, false);} break ;} case 2: {if (I + 1> H) // pre-determine whether the next step has crossed the return; else {If (Board [I + 1] [J] = 0) // if the next position is 0 and does not cross the border, continue to exercise DFS (I + 1, J, true, 2, step, false ); else if (Board [I + 1] [J] = 1) // if the next position is 1 and does not cross the border, stop the motion, and remove the next position of the stone DFS (I, j, false, 2, step, true); else if (Board [I + 1] [J] = 3) // next location It is 3 and does not cross the border. After the motion reaches the position 3, the motion stops. The game ends DFS (I + 1, J, false, 2, step, false);} break;} Case 3: {If (J + 1> W) // pre-determine whether the next step is out of bounds return; else {If (Board [I] [J + 1] = 0) // if the next position is 0 and does not cross the border, continue to exercise DFS (I, j + 1, true, 3, step, false ); else if (Board [I] [J + 1] = 1) // if the next position is 1 and does not cross the border, stop the motion, and remove the next position of the stone DFS (I, j, false, 3, step, true); else if (Board [I] [J + 1] = 3) // The next position is 3 and does not cross the border. After the motion reaches the position 3, the game stops running DFS (I, j + 1, false, 3, step, false );} break ;}}if (FLAG) // restore the stone before backtracking, that is, restore the checker status in the previous step {Switch (Direction) {Case 0: {board [I-1] [J] = 1; break;} Case 1: {board [I] [J-1] = 1; break;} Case 2: {board [I + 1] [J] = 1; break;} Case 3: {board [I] [J + 1] = 1; break ;}}} return ;} int main (void) {While (CIN> W> H) {If (! W &&! H) break;/* structure the Board */minstep = inf; For (INT I = 1; I <= H; I ++) for (Int J = 1; j <= W; j ++) {CIN> Board [I] [J]; If (Board [I] [J] = 2) {S. R = I; S. C = J; S. status = false; Board [I] [J] = 0; // after recording the start position, use it as 0 for processing.} If (Board [I] [J] = 3) // The end point is a special position. The curling will stop when it passes through or reaches the grid {e. R = I; E. C = J ;}/ * search the min path */DFS (S. r, S. c, S. status, 0, 0, false); If (minstep <= 10) cout <minstep <Endl; // although the DFS contains pruning, however, all the methods may have been cut. Therefore, elsecout <-1 <Endl;} return 0;} must be judged ;}

 

============== Gorgeous split line ==========================

 

/* DFS + vector + pruning * // TLE // The Evil poj seems to be incompatible with STL, and the advantage of pruning or timeout // vector cannot be realized... # Include <iostream> # include <vector> using namespace STD; const int INF = 11; typedef class {public: int R, C; // the current position of the curling bool status; // status curling Current status: Motion true, static false} se; se S, E; // records the inner point int W, H; // sizeint minstep of the curling; // The shortest void DFS (vector <int> board, int I, Int J, bool status, int direction, int step, bool flag) {// direction: curling current motion direction north: 0 West: 1 South: 2 East: 3 // flag: whether to remove the stones at the bottom of the direction of direction if (Step> 10) // pruning, If (Board [I] [J] = 3) // The end point {If (minstep> step) minstep = step; return;} If (FLAG) // remove stone {Switch (Direction) {Case 0: {board [I-1] [J] = 0; break ;} // The purpose of the board expression in vector is to delete a rock position in the current step Case 1: {board [I] [J-1] = 0; break ;} // The previous step does not delete the stone Case 2: {board [I + 1] [J] = 0; break;} Case 3: {board [I] [J + 1] = 0; break ;}}} if (! Status) // static {If (I-1> = 1 & (Board [I-1] [J] = 0 | Board [I-1] [J] = 3 )) // northdfs (board, I-1, J, true, 0, step + 1, false ); if (J-1> = 1 & (Board [I] [J-1] = 0 | Board [I] [J-1] = 3) // westdfs (board, i, J-1, true, 1, step + 1, false ); if (I + 1 <= H & (Board [I + 1] [J] = 0 | Board [I + 1] [J] = 3 )) // southdfs (board, I + 1, J, true, 2, step + 1, false ); if (J + 1 <= W & (Board [I] [J + 1] = 0 | Board [I] [J + 1] = 3 )) // eastdfs (board, I, J-1, true, 3, step + 1, false);} e LSE if (Status) // motion {Switch (Direction) {Case 0: {If (I-1 <1) // pre-determine whether the next step crosses the return; else {If (Board [I-1] [J] = 0) // The next position is 0 and does not cross the border, continue motion DFS (board, I-1, J, true, 0, step, false); else if (Board [I-1] [J] = 1) // The next position is 1 and does not cross the border, stop the motion, and eliminates the next position of the stone DFS (board, I, j, false, 0, step, true); else if (Board [I-1] [J] = 3) // The next position is 3 and does not cross the border, after the movement to the position 3 stop the movement, the game ends DFS (board, I-1, J, false, 0, step, false );} break;} Case 1: {If (J-1 <1) // pre-determine whether the next step exceeded return; else {If (Board [I] [J-1] = 0) // The next position is 0 and does not cross the border, continue to exercise DFS (board, I, J-1, true, 1, step, false ); else if (Board [I] [J-1] = 1) // The next position is 1 and does not cross the border, stop the movement, and remove the next position of the stone DFS (board, i, j, false, 1, step, true); else if (Board [I] [J-1] = 3) // The next position is 3 and not out of bounds, stop the game after moving to position 3 (board, I, J-1, false, 1, step, false);} break;} Case 2: {if (I + 1> H) // pre-determine whether the next step is out of bounds return; else {If (Board [I + 1] [J] = 0) // if the next position is 0 and does not cross the border, continue to exercise DFS (board, I + 1, J, true, 2, step, false ); else if (Board [I + 1] [J] = 1) // if the next position is 1 and does not cross the border, stop the motion and remove Stone DFS (board, I, j, false, 2, step, true) in the next position; else if (Board [I + 1] [J] = 3) // The next position is 3 and does not cross the border. After the motion reaches the position 3, stop the motion. The game ends DFS (board, I + 1, J, false, 2, step, false);} break;} Case 3: {If (J + 1> W) // determines whether the next step is to cross-border return; else {If (Board [I] [J + 1] = 0) // if the next position is 0 and does not cross the border, continue to exercise DFS (board, I, j + 1, true, 3, step, false); else if (Board [I] [J + 1] = 1) // if the next position is 1 and does not cross the border, stop the motion, and remove the stone DFS (board, I, j, false, 3, step, true) in the next position; else if (Board [I] [J + 1] = 3) // The next position is 3 and does not cross the border. After the motion reaches the position 3, stop the motion and the game ends DFS (B Oard, I, j + 1, false, 3, step, false) ;}break ;}}return ;} int main (void) {While (CIN> W> H) {If (! W &&! H) break;/* structure the Board */minstep = inf; vector <int> Board (H + 1, vector <int> (W + 1 )); for (INT I = 1; I <= H; I ++) for (Int J = 1; j <= W; j ++) {CIN> Board [I] [J]; If (Board [I] [J] = 2) {S. R = I; S. C = J; S. status = false; Board [I] [J] = 0; // after recording the start position, use it as 0 for processing.} If (Board [I] [J] = 3) // The end point is a special position. The curling will stop when it passes through or reaches the grid {e. R = I; E. C = J ;}/ * search the min path */DFS (board, S. r, S. c, S. status, 0, 0, false); If (minstep <= 10) cout <minstep <Endl; // although the DFS contains pruning, however, you may have cut all the methods. Therefore, you must judge elsecout <-1 <Endl;/* Relax * // board. swap (vector <int> (); // poj seems to disable swap ()} 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.