poj 3669 Meteor Shower(BFS)

题意:一个矩阵,流星下落在某个时刻会砸毁某个点及其上下左右四个点,问某人能否不死?

思路:

记录某点最早爆炸时间(因为爆炸之后不能再走那个点),然后bfs,具体看题解。

题解:

/*
 * mai.cpp
 *
 *  Created on: 2015年9月15日
 *      Author: chen
 */

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<memory.h>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)                          //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;

template<class T>
T Mint(T a, T b, T c) {
	if (a>b) {
		if (c>b)
			return b;
		return c;
	}
	if (c>a)
		return a;
	return c;
}

template<class T>
T Maxt(T a, T b, T c) {
	if (a>b) {
		if (c>a)
			return c;
		return a;
	}
	else if (c > b)
		return c;
	return b;
}

#define maxn 500
int M,x,y,t,maxt;
int map[maxn][maxn];
bool vis[maxn][maxn];
int dx[]={-1,1,0,0,0};//x方向
int dy[]={0,0,-1,1,0};//y方向

bool judge(int x,int y){//判断条件
	if(x<0||x>=M||y<0||y>=M)	return false;
	return true;
}

struct node{
	int x,y,t;//t记录移动次数
};

int bfs(){
	MEM1(vis);
	queue<node>q;
	node s,e;
	s.x=s.y=s.t=0;//初始化
	q.push(s);
	while(!q.empty()){
		s=q.front();
		q.pop();
		for1(i,0,4){//遍历上下左右方向
			e.x=s.x+dx[i];
			e.y=s.y+dy[i];
			e.t=s.t+1;
			if(!judge(e.x,e.y))
				continue;
			if(!vis[e.x][e.y]&&map[e.x][e.y]>e.t){
				if(map[e.x][e.y]>maxt)//如果某点爆炸时间大于流星最后下落的时间,则一点不会受伤,直接返回
					return e.t;
				q.push(e);
				vis[e.x][e.y]=1;
			}
		}
	}
	return -1;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	while(~sf(M)){
		memset(map,0x7F,sizeof(map));//0x7F是int最大化,而不是0xFF,一开始纠结了好久都没发现错误!!!
		maxt=0;//记录最后流星下落的时间
		for1(i,0,M){
			sft(x,y,t);
			maxt=max(maxt,t);
			for1(j,0,5){
				int tx=x+dx[j];
				int ty=y+dy[j];
				if(judge(tx,ty)&&map[tx][ty]>t)
					map[tx][ty]=t;//记录某个点最早的流星下落时间
			}
		}
		if(map[0][0]==0)//如果只有1个点,一定会被砸死
			printf("-1\n");
		else
			pf(bfs());
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值