题目链接
题意:现在有一个n*m的图,起点(1,1),终点(n,m),里面有些点能走,一些点是障碍物,现在让你把一些能走的点变成障碍物,现在让你变最少的点,使起点和终点不连通。
题解:这个题最多变两个点(跟起点连接的两个点),现在我们就要考虑变更少的点的情况。现在我们直接暴力dfs,我们把走过的点全部变成障碍物,要是找到一条走到终点的路(找不到输出0),又从起点开始找,要是还能找到一条路到终点输出2,否则就只要一条路到终点,我们只需要变一个点。
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int mod = 998244353;
const int maxn = 1e6 + 5;
const int inf = 1e9;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
string maps[maxn];
int n, m;
bool dfs(int x, int y) {
if (x == n - 1 && y == m - 1)
return true;
if (x >= n || y >= m || maps[x][y] == '#')
return false;
if (x || y)
maps[x][y] = '#';
return dfs(x + 1, y) || dfs(x, y + 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
cin >> maps[i];
if (!dfs(0, 0))
puts("0");
else {
if (!dfs(0, 0))因为在上面那个if判断就走出了一条路。
puts("1");
else
puts("2");
}
return 0;
}