zoukankan      html  css  js  c++  java
  • poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009

    参考博客:http://www.cnblogs.com/LK1994/

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<stack>
     6 #include<queue>
     7 #include<iomanip>
     8 #include<cmath>
     9 #include<map>
    10 #include<vector>
    11 #include<algorithm>
    12 using namespace std;
    13 
    14 int w,h,G[30][30];
    15 int d[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
    16 int minstep;
    17 
    18 int dfs(int sx,int sy,int step)
    19 {
    20     if(step>10)
    21     return step;
    22     for(int i=0; i<4; i++)
    23     for(int k=1; k<20; k++)
    24     {
    25         int tx=sx+d[i][0]*k;
    26         int ty=sy+d[i][1]*k;
    27         if(tx>=1&&tx<=h&&ty>=1&&ty<=w)
    28         {
    29             if(G[tx][ty]==3) return step+1;
    30             else if(G[tx][ty]==1)
    31             {
    32                 if(k==1) break;
    33                 int x=sx+d[i][0]*(k-1);
    34                 int y=sy+d[i][1]*(k-1);
    35                 G[tx][ty]=0;
    36                 minstep=min(minstep,dfs(x,y,step+1));
    37                 G[tx][ty]=1;
    38                 break;
    39             }
    40         }
    41         else break;
    42     }
    43     return minstep;
    44 }
    45 int main()
    46 {
    47     int sx,sy,ans;
    48     while(cin>>w>>h&&(w!=0||h!=0))
    49     {
    50         for(int i=1; i<=h; i++)
    51         for(int j=1; j<=w; j++)
    52         {
    53             cin>>G[i][j];
    54             if(G[i][j]==2)
    55             {
    56                 sx=i; sy=j;
    57                 G[i][j]=0;
    58             }
    59         }
    60         minstep=11;
    61         ans=dfs(sx,sy,0);
    62         if(ans>10) ans=-1;
    63         cout<<ans<<endl;
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    Beta版使用说明
    【每日scrum】NO.7
    【每日scrum】NO.6
    【每日scrum】NO.5
    【每日scrum】NO.4
    【每日scrum】NO.3
    【每日scrum】NO.2
    【每日scrum】NO.1
    运行及总结
    测试与调试
  • 原文地址:https://www.cnblogs.com/bfshm/p/3286089.html
Copyright © 2011-2022 走看看