POJ 3009 Curling 2.0 {breadth First search}

Source: Internet
Author: User

Original question

$On Planet MM-21, after their Olympic, the year, and curling is getting popular. But the rules is somewhat different from ours. The game is played on the Ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to leads the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of a game board. Some squares is occupied with blocks. There is special squares namely the start and the goal, which is not occupied with blocks. (These squares is distinct.) Once The stone begins to move, it'll proceed until it hits a block. In order to bring the stone to the goal, your may has to stop the stone by hitting it against a block, and throw again.

The movement of the stone obeys the following rules:

The

At the beginning, the stone stands still at the start square.
The movements of the stone is restricted to X and y directions. Diagonal moves is prohibited.
When the stone stands still, you can make it moving by throwing it. Direction unless it is blocked immediately (Fig. 2 (a)).
Once thrown, the stone keeps moving to the same direction until one of the following occurs:
The stone hits a bloc K (Fig. 2 (b), (c)).
The stone stops at the square next to the block it hits.
The block disappears.
The stone gets out of the board.
The game ends in failure.
The stone reaches the goal square.
The stone stops there and the game ends in success.
You cannot throw the stone more than the times in a game. If The stone does not reach the goal in moves, the game ends in failure.

Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number O F moves required.

With the initial configuration shown in Fig. 1, 4 moves is required to bring the stone from the start to the goal. The route is shown in Fig. 3 (a). Notice when the stone reaches the goal, the board configuration had changed as in Fig. 3 (b). $

This time I will not translate, simply say, your ball in the box to scroll, note that the scroll is not a block to move, only to meet the obstacles will stop.

There are 4 adjacent directions that can be moved, not oblique diagonal direction.

If the ball is out of bounds, it will fail if it has been scrolled more than 10 times.

This is the main meaning, see figure 3 to understand.

Ideas

This problem is the same as before, are all about breadth-first search, you can look at this: POJ 1979 red and Black (red and dark)

Here the direction of moving in a two-dimensional array is the same as the previous definition, as shown here:

You first need to enter a two-dimensional array:

        for (int0; row < H; ++row) {            for (int0; col < W; ++col) {                cin >> room[row][col];            }        }

Locate the starting point where 2 is the start, copy the current row and Col to the corresponding start (start), and then end the loop.

        // 为2的点为起始点        for (int0; row < H; ++row) {            for (int0; col < W; ++col) {                if2) {                    sRow = row;                    sCol = col;                    break;                }            }        }

Since this starting point is not needed at this time, set it back to 0 to indicate that you can go. Because there are steps required to complete, you need to add a judgment that exceeds 10 of this output-1. Where the DFS function is the core.

        0;        11;        0);        if10) {            minStep = -1;        }        // 输出结果        cout << minStep << endl;

You need to make a decision at the beginning of the DFS function.

    if (step10step > minStep) {        return;    }

The d here has 4 values representing 4 directions (top, right, bottom, left), and the current row and Col are all at the beginning of the For loop because if you go where you can't go, you can return immediately and regain the current (original) position.

    for (int04; ++d) {        int cRow = row;        int cCol = col;    }

In any case, the point must be in the range, followed by the meaning of judging the point.

while (cRow >= 0 && cRow < H && cCol >= 0 && cCol < W) {    switch (room[cRow][cCol]) {    }}

0 is empty, so continue to the (d) direction.

case0: {    cRow += direc[d][1];    cCol += direc[d][0];    break;}

3 is indicated as the end point, and if step is smaller than the minimum number of steps before the current step, assign it to the minimum number of steps.

case3: {    if (step1 < minStep) {        step1;    }    cRow = -1;    break;}

1 represents the barrier, which restores the walk-through step and then the next recursion, the number of steps plus 1, the location of the restore.

case1: {    if (!(cRow - direc[d][1] == row && cCol - direc[d][0] == col)) {        0;        dfs(cRow - direc[d][1], cCol - direc[d][0step1);        1;    }    cRow = -1;    break;}

The default condition.

default: {    break;}
Code
#include <iostream>using namespace Std;//The maximum width and height given in the topic#define MAX_W#define MAX_H//width and height to be entered and number of steps takenintW, H;int Step=0;intMinstep;intSRow, SCol;//Two-dimensional array to writeintRoom[max_w][max_h];//Clockwise direction to goConst intdirec[4][2] = {    {0, -1},    {1,0},    {0,1},    { -1,0},};voidDfsConst int& Row,Const int& Col,int Step) {if(Step>=Ten||Step> Minstep) {return; } for(intD =0; D <4; ++d) {intCRow = row;intCcol = col; while(CRow >=0&& CRow < H && Ccol >=0&& Ccol < W) {Switch(Room[crow][ccol]) { Case 0: {CRow + = direc[d][1]; Ccol + = direc[d][0]; Break; } Case 3: {if(Step+1< Minstep) {Minstep =Step+1; } CRow =-1; Break; } Case 1: {if(! (crow-direc[d][1] = = Row && ccol-direc[d][0] = = col)) {Room[crow][ccol] =0; DFS (crow-direc[d][1], ccol-direc[d][0],Step+1); Room[crow][ccol] =1; } CRow =-1; Break; }default: { Break; }            }        }    }}intMain () { while(Cin >> W >> H, W >0) {//Input         for(introw =0; Row < H; ++row) { for(intCol =0; Col < W;            ++col) {cin >> room[row][col]; }        }//point for 2 is the starting point         for(introw =0; Row < H; ++row) { for(intCol =0; Col < W; ++col) {if(Room[row][col] = =2{sRow = row; SCol = col; Break; }}} Room[srow][scol] =0; Minstep = One; DFS (SRow, SCol,0);if(Minstep >Ten) {Minstep =-1; }//Output Resultscout << minstep << Endl; }}

POJ 3009 Curling 2.0 {breadth First search}

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.