深搜+回溯 poj3009 Curling 2.0

来源:互联网 发布:网络推广方式方法有哪些 编辑:程序博客网 时间:2024/06/10 06:04

On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead 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 may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have 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 stone obeys 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 like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.

With the initial configuration shown 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, the board 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 of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.

Each dataset is formatted as follows.

the width(=w) and the height(=h) of the board
First row of the board

...
h-th row of the board

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

Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.

0vacant square1block2start position3goal position

The dataset for Fig. D-1 is as follows:

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 line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.

Sample Input

2 13 26 61 0 0 2 1 01 1 0 0 0 00 0 0 0 0 30 0 0 0 0 01 0 0 0 0 10 1 1 1 1 16 11 1 2 1 1 36 11 0 2 1 1 312 12 0 1 1 1 1 1 1 1 1 1 313 12 0 1 1 1 1 1 1 1 1 1 1 30 0

Sample Output

14-1410-1


人生第一道回溯题,做的确实有点累。
什么是回溯?就是在dfs()结束后,把该状态恢复。
为什么要恢复?因为状态转移会改变整体状态,而这种改变对后续状态有影响。

另外这道题有点特别,因为他的dfs一次可以走多步,所以要特判第一步能不能走,走的话停不停的下来。我是在判断函数中传递了(如果停的下来就是这一步可以走的话)障碍物位置和此时所在位置。
需要特别注意的是,地图需要初始化,我因此而WA了。为什么在程序里我用红字标明了。总之,以后做图的多组数据的题都初始化一下总是没错的。
下面是我的又臭又长的代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<limits.h>#include<math.h>using namespace std;const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};int m,n,start_x,start_y,end_x,end_y,xx,yy,minn,xxx,yyy;int a[25][25];bool panduan(int aa,int bb){    if((aa>=1)&&(aa<=n)&&(bb>=1)&&(bb<=m))        return true;    else        return false;}bool panduan2(int i,int x,int y)//判断能不能走并给出走到哪里,i是方向,xy是起始位置坐标{    xx=x+dx[i];    yy=y+dy[i];    if(panduan(xx,yy)&&(a[xx][yy]==3))//如果下一步就是结束点,直接return        return true;    if((!panduan(xx,yy))||(a[xx][yy]==1))//如果下一步越界或者下一步是障碍不能走        return false;    while(panduan(xx,yy)&&(a[xx][yy]!=1))//走到这里,说明下一步既不是终点,而且能走,找出最后的的停止点    {        xx=xx+dx[i];        yy=yy+dy[i];        if((a[xx][yy]==3))            return true;    }    if(a[xx][yy]!=1)//假如xx,yy此时越界,而且这一格子正好是1,那就完蛋了。所以要赋初值        return false;    xxx=xx;//保存这时候障碍物的位置xxx,yyy    yyy=yy;    xx-=dx[i];    yy-=dy[i];    return true;}void dfs(int step,int x,int y){    int temp_xxx,temp_yyy;    //printf("(%d,%d),step=%d\n",x,y,step);    int temp_x,temp_y;    if((step>=10)||((x==end_x)&&(y==end_y)))        return ;    for(int i=0;i<4;i++)    {        if(panduan2(i,x,y))        {            temp_x=xx;            temp_y=yy;            if((xx==end_x)&&(yy==end_y))            {                minn=min(step+1,minn);                //printf("++\n");            }            a[xxx][yyy]=0;//更改状态            temp_xxx=xxx;            temp_yyy=yyy;//将这次dfs对应的block位置保存在这层递归中,防止被破坏            dfs(step+1,xx,yy);            a[temp_xxx][temp_yyy]=1;//回溯        }    }}void duru(void){    for(int i=1;i<=n;i++)    for(int j=1;j<=m;j++)    {        scanf("%d",&a[i][j]);        if(a[i][j]==2)        {            start_x=i;            start_y=j;            a[i][j]=0;        }        if(a[i][j]==3)        {            end_x=i;            end_y=j;        }    }}int main(void){    while(1)    {        scanf("%d%d",&m,&n);        memset(a,-1,sizeof(a));        if(m*n==0)            goto myk;        duru();        minn=INT_MAX;        dfs(0,start_x,start_y);        if(minn==INT_MAX)//走不到的情况            minn=-1;        printf("%d\n",minn);    }    myk:return 0;}

0 0