Part1 题目大意
给你一个地图,其中有一些点不能走,问你最少要堵上几个点才能使点 无法走到 ,只能向右或向下行走。
。
如图需要堵住两个位置。
Part2 做法介绍
答案不超过 (最次堵住出来的两个点),这就很好做,分讨一下即可。
如果本来就搜不到 ,明显是 。
如果搜得到 ,就说明至少要堵住 个点了,进行第一遍搜索,将第一遍走过的路径上的点变成障碍,再从 出发,再搜一遍,如果还能搜到 那么答案就为 ,否则为 。
搜索时为了方便处理,将第一遍走过的路径上的点都变成了障碍。实际上可以只堵住一个点,就是这条路径上的最关键的点,也能达到全变成障碍的效果,所以这样做是对的。
Part3 完整代码
#include<bits/stdc++.h>
#define LL long long
#define faster ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e6+5;
struct node
{
int x,y;
};
int n,m;
char t,bf;
vector <char> mp[N];
vector <bool> vis[N];
queue <node> q;
bool check(int x,int y)
{
if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]=='.'&&vis[x][y]==0)return 1;
else return 0;
}
bool dfs(int x,int y)
{
if(x==n&&m==y)
{
mp[x][y]='#';
return 1;
}
if(check(x+1,y))
{
if(dfs(x+1,y))
{
mp[x][y]='#';
return 1;
}
}
if(check(x,y+1))
{
if(dfs(x,y+1))
{
mp[x][y]='#';
return 1;
}
}
return 0;
}
int main()
{
faster;
cin >> n >> m;
for(int i=1;i<=n;i++)
{
mp[i].push_back(0);
vis[i].push_back(0);
for(int j=1;j<=m;j++)
{
cin >> t;
mp[i].push_back(t);
vis[i].push_back(0);
}
}
if(dfs(1,1))
{
cout << 0;
return 0;
}
else
{
if(dfs(1,1))
{
cout << 2;
return 0;
}
else
{
cout << 1;
return 0;
}
}
return 0;
}