http://codeforces.com/problemset/problem/1214/D
题目大意:给出一个
n
∗
m
n*m
n∗m的字符矩阵,
′
.
′
'.'
′.′表示能通过,
′
#
′
'\#'
′#′表示不能通过。每步可以往下或往右走。问至少把多少个
′
.
′
'.'
′.′变成
′
#
′
'\#'
′#′,才能让从
(
1
,
1
)
(1,1)
(1,1)出发不能到达
(
n
,
m
)
(n,m)
(n,m)。
思路:仔细思考,不难发现答案只有 0 、 1 、 2 0、1、2 0、1、2三种情况,考虑从起点 ( 1 , 1 ) (1,1) (1,1)做一次 d f s dfs dfs,若到不了终点,答案一定是 0 0 0,否则我们再从起点做一次 d f s dfs dfs,且这次 d f s dfs dfs不能经过上一次所走过的点,若仍能到达终点,则答案是 2 2 2,否则答案是 1 1 1。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=1e6+5;
int n,m;
char s[maxn];
vector<char> vec[maxn];
vector<bool> vis[maxn];
bool flag=0;
void dfs(int x,int y)
{
if(x<0||x>=n||y<0||y>=m||vis[x][y]||vec[x][y]=='#')
return ;
if(x==n-1&&y==m-1)
flag=1;
if(flag)
return ;
vis[x][y]=1;
dfs(x+1,y);
dfs(x,y+1);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
vec[i].resize(m);
vis[i].resize(m);
scanf("%s",s);
for(int j=0;j<m;j++)
vec[i][j]=s[j];
}
dfs(0,0);
if(!flag)
printf("0\n");
else
{
vis[0][0]=flag=0;
dfs(0,0);
printf("%d\n",flag==1?2:1);
}
return 0;
}