CF-1214D. Treasure Island(dfs,最小割)

CF传送门

洛谷传送门


解题思路

首先容易想到的是个最小割板子,注意点要分成入点和出点。

其次就是简单的dfs,很显然,答案是0或1或2。

第一遍如果没有路,答案就是0,并且把所有经过的点都标记成障碍。

第二遍再进行一次dfs,若没路,答案就是1,否则就是2。

AC代码

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<iomanip>
 5 #include<cmath>
 6 #include<vector>
 7 using namespace std;
 8 const int maxn=1000005;
 9 int n,m;
10 vector<int> ma[maxn];
11 bool dfs(int x,int y){
12     if(x==n&&y==m) return 1;
13     if(x<n&&ma[x+1][y]){
14         if(dfs(x+1,y)){
15             ma[x][y]=0;
16             return 1;
17         }
18     }
19     if(y<m&&ma[x][y+1]){
20         if(dfs(x,y+1)){
21             ma[x][y]=0;
22             return 1;
23         }
24     }
25     ma[x][y]=0;
26     return 0;
27 }
28 int main(){
29     scanf("%d %d",&n,&m);
30     for(int i=1;i<=n;i++){
31         ma[i].push_back(0);
32         for(int j=1;j<=m;j++){
33             char c;
34             c=getchar();
35             while(c!='.'&&c!='#')c=getchar();
36             if(c=='.') ma[i].push_back(1);
37             else ma[i].push_back(0);
38         }
39     }
40     if(dfs(1,1)){
41         if(dfs(1,1)) cout<<2;
42         else cout<<1<<endl;
43     }
44     else cout<<0<<endl;
45     return 0;
46 }

 

posted @ 2021-03-07 21:49  尹昱钦  阅读(71)  评论(0编辑  收藏  举报