CF1214D Treasure Island

Aimee

因为可以堵出口和入口,所以只有 $ {1,2,3} $

那么先dfs一遍并且封锁路径,然后再dfs一遍

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdio>
#include<vector> 
using namespace std;
int n,m;
char x;
const int maxn=1000005;
string ma[maxn];
int mx[3]={0,1};
int my[3]={1,0};
int f;
void bfs(int x,int y){
	if(x>=n||y>=m)
	return ;
	if(x==n-1&&y==m-1){
		f=1;
		return ;
	}
	if(f){
		return ; 
	}
	if(ma[x][y]=='#'){
		return ;
	}
	ma[x][y]='#';
	for(int i=0;i<=1;++i){
		bfs(x+mx[i],y+my[i]);
		if(f)
		break;
	}
	return ;
}
int cnt;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;++i){
			cin>>ma[i];
	}
	while(1){
		f=0;
		ma[0][0]='.';
		bfs(0,0);
		if(!f)
		break;
		cnt++;
		if(cnt==2)
		break;
		f=0;
	}
	cout<<cnt;
	return 0;
} 
posted @ 2021-03-07 21:41  Simex  阅读(51)  评论(0编辑  收藏  举报