D 迷宫

链接:https://ac.nowcoder.com/acm/contest/4090/D

 

 标程如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define inf 0x3f3f3f3f
 4 const int N=1005;
 5 int n,m,dp[N][N];
 6 char s[N][N];
 7 int main()
 8 {
 9     scanf("%d%d",&n,&m);
10     for(int i=1;i<=n;i++)
11         scanf("%s",s[i]+1);
12     memset(dp,inf,sizeof(dp));
13     dp[1][1]=0;
14     for(int i=1;i<=n;i++)
15         for(int j=1;j<=m;j++){
16         if(s[i][j]=='0'){
17             if(j<m&&s[i][j+1]=='0')
18                 dp[i][j+1]=min(dp[i][j+1],dp[i][j]);
19             if(i<n&&s[i+1][j]=='0')
20             {
21                 if(j==m||s[i][j+1]=='1')
22                     dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
23                 else dp[i+1][j]=min(dp[i+1][j],dp[i][j]+1);
24             }
25         }
26     }
27     if(dp[n][m]==inf) printf("-1\n");
28     else printf("%d\n",dp[n][m]);
29 }

 

posted @ 2020-05-22 21:01  古比  阅读(179)  评论(0编辑  收藏  举报