poj 3669

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20056Accepted: 5210

Description

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti  ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

Input

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5
下面放上一个错误代码。。我写的,就是过不了,5555
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

typedef pair<int,int> P;
const int MAX_M = 50000;
const int MAX_N = 401;
const int INF = 100000000;

int M;
int X[MAX_M], Y[MAX_M], T[MAX_N];
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};

int maze[MAX_N][MAX_N];	//存储地图
int d[MAX_N][MAX_N];	//保存步数

int bfs()
{
	if(maze[0][0]==0)	return -1;	//时刻0在(0,0)有危险 
	
	queue<P> que;
	que.push(P(0,0));	//记住这个P(0,0) 
	d[0][0]=0;	//保存步数 和 时间 
	
	while(!que.empty()){		//队列存坐标 
		P p=que.front();
		que.pop();	//要记得的弹出啊。。。 
		int x=p.first, y=p.second;
		if(maze[x][y]==INF)	return d[x][y];
		
		for(int i=0; i<4; i++){
			int nx, ny;
			nx=x+dir[i][0];
			nx=y+dir[i][1];
			
			if(0<=nx && 0<=ny && d[nx][ny] == INF && d[x][y]+1 < maze[nx][ny]){
				que.push(P(nx,ny));
				d[nx][ny] = d[x][y]+1;
			}
			
		}
	}
	return -1;
}

void solve(){
	//初始化地图
	for(int i=0; i<MAX_N; i++)
	{
		fill(maze[i], maze[i]+MAX_N, INF);	//初始化地图 
		fill(d[i], d[i]+MAX_N, INF);	//初始化地图最小步数 
	}
		
	
	for(int i=0; i<M; i++){
		maze[X[i]][Y[i]] = min(maze[X[i]][Y[i]],T[i]);
		for(int j=0; j<4; j++)
		{
			int nx=X[i]+dir[j][0], ny=Y[i]+dir[j][1];
			if(0<=nx && 0<=ny && nx<=300 && ny<=300)
				maze[nx][ny] = min(maze[nx][ny], T[i]);
		} 
	}
	
	int ans = bfs();
	printf("%d\n", ans);
	return ; 
}

int main()
{
	scanf("%d", &M);
	for(int i=0; i<M; i++){
		scanf("%d%d%d", &X[i], &Y[i], &T[i]);
	}
	solve();
	return 0;
} 
 
唉,下面是一个正确的代码
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int MAX_M = 50000;
const int MAX_N = 400 + 1;
const int INF = 100000000;
//输入
int M;
int X[MAX_M], Y[MAX_M], T[MAX_M];
int maze[MAX_N][MAX_N];                        //保存地图
int d[MAX_N][MAX_N];                        //保存最短步数
//4个方向
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int bfs(){
    //一开始就被炸
    if(maze[0][0] == 0) return -1;
    
    queue<P> que;
    que.push(P(0, 0));
    d[0][0] = 0;
    while(!que.empty()){
        P p = que.front();
        que.pop();
        //已到达安全位置
        int x = p.first, y = p.second;
        if(maze[x][y] == INF) return d[x][y];
        //4个方向走
        for(int i = 0; i < 4; i ++){
            int nx = x + dx[i], ny = y + dy[i];
            //判断是否可移动,是否访问过,以及下一个时刻是否安全
            if(0 <= nx && 0 <= ny && d[nx][ny] == INF && d[x][y] + 1 < maze[nx][ny]){
                que.push(P(nx, ny));
                d[nx][ny] = d[x][y] + 1;
            }
        }
    }
    return -1;
}
void solve(){
    //初始化地图
    for(int i = 0; i < MAX_N; i ++)
        fill(maze[i], maze[i] + MAX_N, INF);
    //模拟轰炸场景
    for(int i = 0; i < M; i ++){
        maze[X[i]][Y[i]] = min(maze[X[i]][Y[i]], T[i]);
        for(int j = 0; j < 4; j ++){
            int nx = X[i] + dx[j], ny = Y[i] + dy[j];
            if(0 <= nx && 0 <= ny)
                maze[nx][ny] = min(maze[nx][ny], T[i]);
        }
    }
    //初始化地图最小步数
    for(int i = 0; i < MAX_N; i ++)
        fill(d[i], d[i] + MAX_N, INF);
    //宽度优先搜索
    int ans = bfs();
    printf("%d\n", ans);
}
int main(int argc, char const *argv[]){
    scanf("%d", &M);
    for(int i = 0; i < M; i ++){
        scanf("%d %d %d", &X[i], &Y[i], &T[i]);
    }
    solve();
    return 0;
}
其实就是一道bfs题目,先自己模拟好地图,在地图maze[][]上的数字表示什么时候开始不能走了,然后用一个数组d[][]来标记到达某个坐标的最少步数。记得maze和d都要初始话为INF好操作哦!之后就是bfs了,将pair元素来代表二维坐标,放入队列,最重要的条件是d[x][y] + 1 < maze[x][y]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值