poj_3669_Meteor Shower(BFS+预处理)


http://poj.org/problem?id=3669
/*思路:BFS+预处理
先预处理会爆炸的区域,BFS,遇到-1则结束*/
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int visit[1001][1001];
int dx[5] = {0,0,1,0,-1};
int dy[5] = {0,1,0,-1,0};
typedef struct
{
	int x,y;
	int step; 
}point;
queue<point> que;
int bfs(int x,int y)
{
	int cnt;
	if(visit[0][0] == 0)  return -1;//无法逃离 
	if(visit[0][0] == -1) return 0;  //安全,无需逃离 
	point s,cur,next;
	s.x = x;
	s.y = y;
	s.step = 0;
	que.push(s);
	while(!que.empty())
	{
		cur = que.front();
		que.pop();
		for(int i = 0;i < 5;i++)
		{
			next.x = cur.x + dx[i];
			next.y = cur.y + dy[i];
			next.step = cur.step + 1;
			
			if(next.x >= 0 && next.y >= 0 && next.y < 1001 && next.x < 1001)
			{
				if(visit[next.x][next.y] == -1)
				{
					return next.step;
				}
				if(next.step < visit[next.x][next.y])
				{
					visit[next.x][next.y] = next.step;//记录最小的步数 
					que.push(next);	
				}
			} 
		} 
	} 
	return -1;
}
int main()
{

	int m;
    while(cin >> m)
	{
        int x,y,t,xx,yy;
	    memset(visit,-1,sizeof(visit));
	    for(int i = 0;i < m;i++)
	    {
	        cin >> x >> y >> t;
	        
	  
	        for(int i = 0;i <= 4;i++)
	        {
	            xx = x + dx[i];
	            yy = y + dy[i];
	            if(xx >= 0 && yy >= 0 && xx < 1001 && yy < 1001)//保证在有限区域内 
				{
		            if(visit[xx][yy] == -1)
		            	 visit[xx][yy] = t;
		            else
		            	visit[xx][yy] = min(visit[xx][yy],t);
				}
	        }
	    }
	    cout << bfs(0,0) << "\n"; 
	}
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值