POJ3009 Curling2.0 (DFS求最短路(已知最长的情况下))

来源:互联网 发布:大气层消失 知乎 编辑:程序博客网 时间:2024/04/28 10:33

On Planet MM-21, after theirOlympic games this year, curling is getting popular. But the rules are somewhatdifferent from ours. The game is played on an ice game board on which a squaremesh is marked. They use only a single stone. The purpose of the game is tolead the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of agame board. Some squares may be occupied with blocks. There are two specialsquares namely the start and the goal, which are not occupied with blocks.(These two squares are distinct.) Once the stone begins to move, it willproceed until it hits a block. In order to bring the stone to the goal, you mayhave to stop the stone by hitting it against a block, and throw again.


Fig. 1: Example of board (S: start, G: goal)

The movement of the stoneobeys the following rules:

  • At the beginning, the stone stands still at the start square.
  • The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
  • When the stone stands still, you can make it moving by throwing it. You may throw it to any 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 block (Fig. 2(b), (c)).
      • The stone stops at the square next to the block it hit.
      • 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 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.


Fig. 2: Stone movements

Under the rules, we would liketo know whether the stone at the start can reach the goal and, if yes, theminimum number of moves required.

With the initial configurationshown in Fig. 1, 4 moves are 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, theboard configuration has changed as in Fig. 3(b).


Fig. 3: The solution for Fig. D-1 and the final board configuration

Input

The input is a sequence ofdatasets. The end of the input is indicated by a line containing two zerosseparated by a space. The number of datasets never exceeds 100.

Each dataset is formatted asfollows.

the width(=w) and theheight(=h) of the board
First row of the board
...
h-th row of the board

The width and the height ofthe board satisfy: 2 <=w <= 20, 1 <=h <= 20.

Each line consists ofwdecimal numbers delimited by a space. The number describes the status of thecorresponding square.

0

vacant square

1

block

2

start position

3

goal position

The dataset for Fig. D-1 is asfollows:

6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1

Output

For each dataset, print a linehaving a decimal integer indicating the minimum number of moves along a routefrom the start to the goal. If there are no such routes, print -1 instead. Eachline should not have any character other than this number.

Sample Input

2 1

3 2

6 6

1 0 0 2 1 0

1 1 0 0 0 0

0 0 0 0 0 3

0 0 0 0 0 0

1 0 0 0 0 1

0 1 1 1 1 1

6 1

1 1 2 1 1 3

6 1

1 0 2 1 1 3

12 1

2 0 1 1 1 1 1 1 1 1 1 3

13 1

2 0 1 1 1 1 1 1 1 1 1 1 3

0 0

Sample Output

1

4

-1

4

10

-1

 

 

 

题目大意:输入一张图,0表示路,1表示障碍物,2表示开始位置,3表示目标位置,只能往上下左右四个方向运动,而且一旦开始移动就只能滑出场地,滑进洞,或者撞上障碍物。撞上障碍物后障碍物会消失,求至少需要多少步(如果步数大于10则失败)

分析:一般来说DFS不适合用来求最短路,因为这样的话就要把所有可能路径都搜一遍,搜索树会很深,很费时间。但此题已知步数要小于10才输出,意思就是搜索树最多有10层,多余10层的就可以不搜了,所以可以用DFS



注意:如果球的左边就是障碍物的话,不能直接往左打来击碎障碍物



我的代码(不知道我的为什么那么长。。。。。。):


#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int w,h,grid[22][22],sy,ey,sx,ex,step,minstep;int ok(int x0,int y0)//再打一杆可以进洞{    int i;    if (x0==ex&&y0<ey)    {        for (i=y0;i<=ey;i++)        {            if (grid[x0][i]==1)            {                return 0;            }        }        return 1;    }    else if (x0==ex&&y0>ey)    {        for (i=y0;i>=ey;i--)        {            if (grid[x0][i]==1)            {                return 0;            }        }        return 1;    }    else if (y0==ey&&x0<ex)    {        for (i=x0;i<=ex;i++)        {            if (grid[i][y0]==1)            {                return 0;            }        }        return 1;    }    else if (y0==ey&&x0>ex)    {        for (i=x0;i>=ex;i--)        {            if (grid[i][y0]==1)            {                return 0;            }        }        return 1;    }    return 0;}int checkleft(int x0,int y0)//是否可以向左走{    int i;    if (grid[x0][y0-1]==1)    {        return 0;    }    for (i=y0-2;i>=0;i--)    {        if (grid[x0][i]==1)        {            return 1;        }    }    return 0;}int checkright(int x0,int y0)//同上{    int i;    if (grid[x0][y0+1]==1)    {        return 0;    }    for (i=y0+2;i<=w-1;i++)    {        if (grid[x0][i]==1)        {            return 1;        }    }    return 0;}int checkup(int x0,int y0)//同上{    int i;    if (grid[x0-1][y0]==1)    {        return 0;    }    for (i=x0-2;i>=0;i--)    {        if (grid[i][y0]==1)        {            return 1;        }    }    return 0;}int checkdown(int x0,int y0)//同上{    int i;    if (grid[x0+1][y0]==1)    {        return 0;    }    for (i=x0+2;i<=h-1;i++)    {        if (grid[i][y0]==1)        {            return 1;        }    }    return 0;}void dfs(int x0,int y0)//深搜{    int x,y;    grid[ex][ey]=3;//不要这句无法AC,不知是何原理    if (ok(x0,y0)&&step<minstep)    {        minstep=step+1;        return;    }    else if (step>10)    {        return;    }    else    {        x=x0;        y=y0;        if (checkleft(x,y))        {            step++;            do            {                y--;            }while (grid[x][y]==0);            grid[x][y]=0;            y++;            dfs(x,y);            grid[x][y-1]=1;            x=x0;            y=y0;            step--;        }        if (checkright(x,y))        {            step++;            do            {                y++;            }while (grid[x][y]==0);            grid[x][y]=0;            y--;            dfs(x,y);            grid[x][y+1]=1;            x=x0;            y=y0;            step--;        }        if (checkup(x,y))        {            step++;            do            {                x--;            }while (grid[x][y]==0);            grid[x][y]=0;            x++;            dfs(x,y);            grid[x-1][y]=1;            x=x0;            y=y0;            step--;        }        if (checkdown(x,y))        {            step++;            do            {                x++;            }while (grid[x][y]==0);            grid[x][y]=0;            x--;            dfs(x,y);            grid[x+1][y]=1;            x=x0;            y=y0;            step--;        }    }    return;}int main(){    int i,j;    do    {        minstep=11;        step=0;        memset(grid,0,sizeof(grid));        scanf("%d%d",&w,&h);        if (w==0&&h==0)        {            break;        }        for (i=0;i<=h-1;i++)        {            for (j=0;j<=w-1;j++)            {                scanf("%d",&grid[i][j]);                if (grid[i][j]==2)                {                    sx=i;                    sy=j;                    grid[i][j]=0;                }                else if (grid[i][j]==3)                {                    ex=i;                    ey=j;                }            }        }        dfs(sx,sy);        if (minstep<=10)        {            printf("%d\n",minstep);        }        else        {            printf("-1\n");        }    }while (1);    return 0;}

0 0
原创粉丝点击