[POJ3009]Curling2.0

[POJ3009]Curling 2.0
 
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<limits.h>
 5 using namespace std;
 6 const int way[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
 7 int n,m,startx,starty,MIN_N=INT_MAX,f[25][25];
 8 void dfs(int x,int y,int step)
 9 {
10     if(step>=10 || step>=MIN_N)return;
11     for(int i=0;i<4;++i)
12     {
13         if(f[x+way[i][0]][y+way[i][1]]==1)continue;
14         int setx=x,sety=y;
15         while(setx<=n && sety<=m && setx>0 && sety>0)
16         {
17             setx+=way[i][0];
18             sety+=way[i][1];
19             if(f[setx][sety]==3 && MIN_N>step+1)
20             {
21                 MIN_N=step+1;
22                 return;
23             }
24             if(f[setx+way[i][0]][sety+way[i][1]]==1)
25             {
26                 f[setx+way[i][0]][sety+way[i][1]]=0;
27                 dfs(setx,sety,step+1);
28                 f[setx+way[i][0]][sety+way[i][1]]=1;
29                 break;
30             }
31         }
32     }
33 }
34 int main()
35 {
36     while(~scanf("%d%d",&m,&n))
37     {
38         if(n==0 && m==0)return 0;
39         MIN_N=INT_MAX;
40         memset(f,NULL,sizeof(f));
41         for(int i=1;i<=n;++i)
42             for(int j=1;j<=m;++j)
43             {
44                 scanf("%d",&f[i][j]);
45                 if(f[i][j]==2)
46                     startx=i,starty=j;
47             }
48         dfs(startx,starty,0);
49         if(MIN_N==INT_MAX)printf("-1
");
50         else printf("%d
",MIN_N);
51     }
52 }
原文地址:https://www.cnblogs.com/__Kgds/p/9436457.html