zoukankan      html  css  js  c++  java
  • [USACO08FEB]Meteor Shower S

    题目:Meteor Shower S

    网址:https://www.luogu.com.cn/problem/P2895

    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.

    输入
    • Line 1: A single integer: M

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

    输出
    • Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
    输入输出样例
    输入
    4
    0 0 2
    2 1 2
    1 1 2
    0 3 5
    
    输出
    5
    

    记录每一个格子被砸的时间(如果没有就赋值为-1)。

    广搜,如果越界或者当前时间>=该格子砸落的时间就不能扩展。

    需要判重。

    不过要注意这道题有很多值得深思回味的坑: 1.如果一个格子多次被砸,则记录最早一次被砸的时间。
    2.题目指的安全位置可以大于n。

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #define pii pair <int, int>
    using namespace std;
    const int maxn = 300 + 5;
    const int dx[4] = {-1, 1, 0, 0};
    const int dy[4] = {0, 0, -1, 1};
    int m;
    int map[maxn][maxn], d[maxn][maxn];
    bool valid(int x, int y)
    {
    	if(x < 0 || y < 0 || d[x][y] > -1) return false;
    	return true;
    }
    int bfs()
    {
    	queue <pii> Q;
    	while(!Q.empty()) Q.pop();
    	memset(d, -1, sizeof(d));
    	int time;
    	d[0][0] = 0;
    	Q.push(make_pair(0, 0));
    
    	while(!Q.empty())
    	{
    		pii now = Q.front(), next;
    		Q.pop();
    		time = d[now.first][now.second] + 1;
    		for(int i = 0; i < 4; ++ i)
    		{
    			next = make_pair(now.first + dx[i], now.second + dy[i]);
    			if(!valid(next.first, next.second)) continue;
    			if(map[next.first][next.second] == -1) return time;
    			if(map[next.first][next.second] <= time) continue;
    			
    			d[next.first][next.second] = time;
    			Q.push(next);
    		}
    	}
    	return -1;
    }
    int main()
    {
    	memset(map, -1, sizeof(map));
    	scanf("%d", &m);
    	for(int i = 0; i < m; ++ i)
    	{
    		int x, y, t;
    		scanf("%d %d %d", &x, &y, &t);
    		if(map[x][y] == -1) map[x][y] = t;
    		else map[x][y] = min(t, map[x][y]);
    		if(x) {
    			if(map[x - 1][y] == -1) map[x - 1][y] = t;
    			else map[x - 1][y] = min(map[x - 1][y], t);
    		}
    		if(x < 300) {
    			if(map[x + 1][y] == -1) map[x + 1][y] = t;
    			else map[x + 1][y] = min(map[x + 1][y], t);
    		}
    		if(y) {
    			if(map[x][y - 1] == -1) map[x][y - 1] = t;
    			else map[x][y - 1] = min(map[x][y - 1], t);
    		}
    		if(y < 300) {
    			if(map[x][y + 1] == -1) map[x][y + 1] = t;
    			else map[x][y + 1] = min(map[x][y + 1], t);
    		}
    	}
    	printf("%d
    ", bfs());
    	return 0;
    }
    
  • 相关阅读:
    ubuntu server编译安装nginx
    XPath具体解释
    windows下安装,配置gcc编译器
    给字符数组赋值的方法
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    MiinCMP1.0 SAE 新浪云版公布, 开源企业站点系统
    Mac下cocos2dx-3.0打包Android时,提示&quot;SimpleAudioEngine.h&quot;not found的解决方法
    GG同步到sqlserver报错一例 Invalid date format
    分布式文件系统
    动画clip仅仅读的解决的方法,以及动画关键帧回调的办法
  • 原文地址:https://www.cnblogs.com/zach20040914/p/12814283.html
Copyright © 2011-2022 走看看