cf#301-C. Ice Cave-BFS

49 篇文章 1 订阅

http://codeforces.com/contest/540/problem/C


题意 给n*m的地图

只能走.不能走X,并且点.被走了之后会变成X,问能否从点1走到点2,并且要求最后点2要是X(为了掉到下一level)   


因此直接bfs搜,每次走过 点 后把 点变成X ,最后判断能否到达点2 并且点2状态为 X即可


另一种是直接判断能不能到点2,然后如果点2本身是X则输yes,

如果是点的话,只需要看能否往外走一步然后退回来即可

即看四周有没一个合法的点,

合法的点首先要是【点】,其次没被访问过,如果被访问过 【访问的步数必须==到达点2的步数-1】 


1:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001; 
__int64 min(__int64 a,__int64 b)
{return a<b? a:b ;} 
__int64 max(__int64 a,__int64 b) 
{return a>b?a:b;}

int n,m;
char mp[505][505];
int vis[505][505];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
	int x,y;
	int step;
	node(){}
	node(int a,int b,int c)
	{x=a,y=b;step=c;}
};
queue<node> q;
node st,ed;
int main()
{ 
	int i;
	cin>>n>>m;
	int x,y;
	for (i=1;i<=n;i++)
	{
		scanf("%s",mp[i]+1);
	}
	scanf("%d%d",&st.x,&st.y);
	scanf("%d%d",&ed.x,&ed.y);
	int flag=0;
	st.step=0;
	q.push(st);
	while(!q.empty())
	{
		node tp=q.front();
		q.pop();
		for (i=0;i<4;i++)
		{
			int x=tp.x+dx[i];
			int y=tp.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
		//	if (vis[x][y])continue;
			if (x==ed.x&&y==ed.y&&mp[x][y]=='X')
			{
				flag=tp.step+1;break;
			}
			if (mp[x][y]=='X')continue;
			mp[x][y]='X';
		//	vis[x][y]=tp.step+1;
			q.push(node(x,y,tp.step+1));
		}
		if (flag)break; 
	}
	if(flag)
	{
	  printf("YES\n"); 
	}
	else
	{
		printf("NO\n");
	}
	
	
	
	return 0; 
}


2:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001; 
__int64 min(__int64 a,__int64 b)
{return a<b? a:b ;} 
__int64 max(__int64 a,__int64 b) 
{return a>b?a:b;}

int n,m;
char mp[505][505];
int vis[505][505];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
	int x,y;
	int step;
	node(){}
	node(int a,int b,int c)
	{x=a,y=b;step=c;}
};
queue<node> q;
node st,ed;
int main()
{ 
	int i;
	cin>>n>>m;
	int x,y;
	for (i=1;i<=n;i++)
	{
		scanf("%s",mp[i]+1);
	}
	scanf("%d%d",&st.x,&st.y);
	scanf("%d%d",&ed.x,&ed.y);
	int flag=0;
	st.step=0;
	q.push(st);
	int lastx,lasty;
	while(!q.empty())
	{
		node tp=q.front();
		q.pop();
		for (i=0;i<4;i++)
		{
			int x=tp.x+dx[i];
			int y=tp.y+dy[i];
			if (x<1||x>n||y<1||y>m) continue;
			if (vis[x][y])continue;
			if (x==ed.x&&y==ed.y)
			{
				flag=tp.step+1;
				lastx=tp.x,lasty=tp.y;break;
			}
			if (mp[x][y]=='X')continue;
			vis[x][y]=tp.step+1;
			q.push(node(x,y,tp.step+1));
		}
		if (flag)break; 
	}
	if(flag)
	{
		if (mp[ed.x][ed.y]=='X') printf("YES\n");
		else
		{
			int cun=0;
			for (i=0;i<4;i++)
			{
				int x=ed.x+dx[i];
				int y=ed.y+dy[i];
				if (x<1||x>n||y<1||y>m) continue; 
				if (mp[x][y]=='X')continue; 
				if ((vis[x][y]&&vis[x][y]<flag-1))continue; 
				if (x==lastx&&y==lasty) continue;
				cun++;
			}
			if (cun>=1) printf("YES\n");
			else
				printf("NO\n");
		}
		
	}
	else
	{
		printf("NO\n");
	}
	
	
	
	return 0; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值