傳送門:http://codeforces.com/problemset/problem/1214/D
題意:給你一張圖 ,從 (1,1)走到( n , m )問增加幾個障礙使得不連通
思路:最多像兩個點 dfs尋找是否存在兩個不相同的路徑到達(n,m)
ACcode:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+10;
bool dp[maxn];
bool vis[maxn];
char s[maxn];
int n,m;
int node(int x,int y)
{
return x*m+y;
}
bool dfs(int x,int y)
{
if(x == n-1 && y == m - 1) return true;
vis[node(x,y)] = true;
if(x+1 < n && !vis[node(x+1,y)] && !dp[node(x+1,y)] && dfs(x+1,y)) return true;
if(y+1 < m && !vis[node(x,y+1)] && !dp[node(x,y+1)] && dfs(x,y+1)) return true;
return false;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%s",&s);
for(int j=0; j<m; j++)
if(s[j] == '#') dp[node(i,j)]= true;
}
int ans=0;;
if(dfs(0,0)) ans = 1;
if(dfs(0,0)) ans = 2;
printf("%d\n",ans);
return 0;
}