POJ 3669 - Meteor Shower

 

题目地址: http://poj.org/problem?id=3669

 

BFS。

 

简单搜索,记录每个炸弹落地的时间,在落地之前能走,之后不能走。

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define bug puts("here");

using namespace std;

int vis[330][330];
int fx[]={0,0,0,1,-1};
int fy[]={0,1,-1,0,0};

struct Node{
	int x,y,t;
};
queue<Node>que;

int bfs(){
	int k;
	Node tmp,now,nx;
	if(vis[0][0]==0) return -1; 
	else if(vis[0][0]==-1) return 0;
	tmp.x=tmp.y=tmp.t=0;
	que.push(tmp);
	while(!que.empty()){
		now=que.front();
		que.pop();
		for(k=1;k<5;k++){
			nx.x=now.x+fx[k];
			nx.y=now.y+fy[k];
			nx.t=now.t+1;
			if(nx.x<0 || nx.y<0) continue;
			if(vis[nx.x][nx.y]==-1)  return nx.t;
			if(nx.t>=vis[nx.x][nx.y]) continue;
			vis[nx.x][nx.y]=0;
			que.push(nx);
		}
	}
	return -1;
} 

int main(){
	int m,k,t,x,y,xx,yy;
	scanf("%d",&m); 
	memset(vis,-1,sizeof(vis));
	while(m--){
		scanf("%d%d%d",&x,&y,&t);
		for(k=0;k<5;k++){
			xx=x+fx[k];
			yy=y+fy[k];
			if(xx<0 || yy<0) continue;
			if(vis[xx][yy]==-1) vis[xx][yy]=t;
			else vis[xx][yy]=min(vis[xx][yy],t);
		}
	}
	printf("%d\n",bfs());
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值