Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises)D. Treasure Island(1214)

题目来源:http://codeforces.com/contest/1214/problem/D

跑两遍dfs,因为题目要求只能往右或者往下走,所以不要返回,直接一直边走边标记,第一遍跑完,如果到不了的话,那么就为0;否则跑第二遍,如果第二遍到不了,就为1,否则就为2,因为只有0,1,2这三种情况。

压缩成一维数组去储存。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
vector<char>s[1000100];
map<ll,int>book;
int ne[2][2]={{0,1},{1,0}};
bool flag=false;
int n,m;
void dfs(int x,int y)
{
	if(flag)
	return ;
	if(x==n-1&&y==m-1)
	{
		flag=true;
		return ;
	}
	for(int i=0;i<2;i++)
	{
		if(flag)
		return ;
		int xx=x+ne[i][0],yy=y+ne[i][1];
		ll uu=xx*1000000+yy;
		if(xx<0||yy<0||xx>=n||yy>=m||book[uu])
		{
			continue;
		}
		book[uu]=1;
		dfs(xx,yy);
	}
	return ;
}
int main()
{
	int i,j;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			char temp;
			cin>>temp;
			s[i].push_back(temp);
			if(temp=='#')
			{
				book[i*1000000+j]=1;
			}
		}
	}
	dfs(0,0);
	if(!flag)
	{
		printf("0");
		return 0; 
	}
	flag=false;
	book[0]=0;
	book[(n-1)*1000000+m-1]=0;
	dfs(0,0);
	if(!flag)
	{
		printf("1");
		return 0;
	}
	printf("2");
	return 0;
} 
发布了56 篇原创文章 · 获赞 17 · 访问量 2331

猜你喜欢

转载自blog.csdn.net/weixin_43958964/article/details/100810413