1. 程式人生 > >poj 3009 Curling 2.0(DFS)

poj 3009 Curling 2.0(DFS)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f

using namespace std;
int a[105][105];
int n,m;
int x1,y1;
int dx[]= {1,0,-1,0};
int dy[]= {0,-1,0,1};
int mi;
int vis[105][105];
void dfs(int x,int y,int s)   
{
    if(a[y][x]==3)
    {
        mi=min(mi,s);<span style="font-family: 'Times New Roman', Times, serif;">//</span><span style="color:#ff0000;font-family: 'Times New Roman', Times, serif;">DFS只有在全部搜尋完後才可以得到最小的步數,每次搜尋成功後取當前的最小值</span>

        return ;
    }
    if(s>=10)//       <span style="color:#ff6666;">步數大於大於10,題目中說按沒有搜尋到,等於10,如果滿足的話會在上面返回。</span>
    {
        return ;
    }
    if(x+1<n&&(a[y][x+1]!=1||vis[y][x+1]))          <span style="color:#ff0000;">//判斷在此行是否可以移動,可移動的條件是在這一行找到0(障礙物)或3(目標)否則一</span>
<span style="color:#ff0000;">                                                                             //  旦移動將不會停下來或者是yuejie</span>
    {
        int i;
        if(a[y][x+1]==3)                                         
        {
           dfs(x+1,y,s+1);<span style="font-family: 'Times New Roman', Times, serif;">                  </span><span style="color:#ff0000;font-family: 'Times New Roman', Times, serif;">//如果當前的下一個位置是目標則進入dfs</span>

        }
        if(vis[y][x+1])                 
        {                                      
            i=x+1;
        }
        else i=x+2;
        for(; i<n; i++)
        {
            if((a[y][i]==1||a[y][i]==3)&&!vis[y][i])
            {
                if(a[y][i]==1)
                {
                    vis[y][i]=1;
                    dfs(i-1,y,s+1);
                    vis[y][i]=0;
                }
                else
                {
                    vis[y][i]=1;
                    dfs(i,y,s+1);
                    vis[y][i]=0;
                }
                break;
            }
        }
    }
    if(x-1>=0&&(a[y][x-1]!=1||vis[y][x-1]))
    {
        int i;
        if(a[y][x-1]==3)
        {
            dfs(x-1,y,s+1);
        }
        if(vis[y][x-1])i=x-1;
        else i=x-2;
        for(; i>=0; i--)
        {
            if((a[y][i]==1||a[y][i]==3)&&!vis[y][i])
            {
                if(a[y][i]==1)
                {
                    vis[y][i]=1;
                    dfs(i+1,y,s+1);
                    vis[y][i]=0;
                }
                else
                {
                    vis[y][i]=1;
                    dfs(i,y,s+1);
                    vis[y][i]=0;
                }
                break;
            }
        }
    }
    if(y-1>=0&&(a[y-1][x]!=1||vis[y-1][x]))
    {
        int j;
        if(a[y-1][x]==3)
        {
            dfs(x,y-1,s+1);
        }
        if(vis[y-1][x])j=y-1;
        else j=y-2;
        for(; j>=0; j--)
        {
            if((a[j][x]==1||a[j][x]==3)&&!vis[j][x])
            {
                if(a[j][x]==1)
                {
                    vis[j][x]=1;
                    dfs(x,j+1,s+1);
                    vis[j][x]=0;
                }
                else
                {
                    vis[j][x]=1;
                    dfs(x,j,s+1);
                    vis[j][x]=0;
                }
                break;
            }
        }
    }
    if(y+1<m&&(a[y+1][x]!=1||vis[y+1][x]))
    {
         int j;
         if(a[y+1][x]==3)
         {
			dfs(x,y+1,s+1);
         }
         if(vis[y+1][x])j=y+1;
         else j=y+2;
        for(; j<m; j++)
        {
            if((a[j][x]==1||a[j][x]==3)&&!vis[j][x])
            {
                if(a[j][x]==1)
                {
                    vis[j][x]=1;
                    dfs(x,j-1,s+1);
                    vis[j][x]=0;
                }
                else
                {
                    vis[j][x]=1;
                    dfs(x,j,s+1);
                    vis[j][x]=0;
                }
                break;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        mi=inf;
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
            {
                scanf("%d",&a[i][j]);
                if(a[i][j]==2)
                {
                    x1=j;
                    y1=i;
                }
            }
        int t=0;
        int tt=0;
       // printf("%d %d\n",x1,y1);
        for(int i=0; i<4; i++)
        {
            int xi=x1+dx[i];
            int yi=y1+dy[i];
            if(xi>=0&&yi>=0&&xi<n&&yi<m)
            {
                if(a[yi][xi]==0)
                {
                    t=1;
                }
                if(a[yi][xi]==3)
                {
                    tt=1;
                }
            }

        }
        if(tt)printf("1\n");
        else
        if(!t)printf("-1\n");
        else
        {
            memset(vis,0,sizeof(vis));
            dfs(x1,y1,0);
            if(mi==inf)printf("-1\n");
            else printf("%d\n",mi);
        }

    }
}