POJ3669Meteor Shower(bfs)

来源:互联网 发布:天龙八部星河源码 编辑:程序博客网 时间:2024/05/22 13:41
Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19078 Accepted: 4951

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

40 0 22 1 21 1 20 3 5

Sample Output

5

Source

USACO 2008 February Silver

题目大意:在一个二维矩阵中,会在不同的t时刻在某一个位置上掉下一颗陨石,然后上下左右中都会受到影响,陨石损坏后就不能通过,问从(0,0)开始走一个最短路径到达一个安全地方,求最短路径,否则输出-1

解题思路:这个题是一个bfs,主要是利用一个图把每个点被破坏的最小时间存下来,然后判断一下走到一个安全地方所需要的时间,这道题的数据范围虽然到300,其实感觉有些点已经过了300,所以要注意一下,长时间没写bfs忘记判断是否该点走过了,结果t了无数次

#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map>  using namespace std;struct point{int x, y, sum;}p, pp;queue<point> qua;int dx[5] = { 0,0,0,1,-1 };int dy[5] = { 0,1,-1,0,0 };int i, n, x, y, t, j, ddx, ddy;bool a[305][305];int tu[305][305];int main(){memset(tu, -1, sizeof(tu));memset(a, false, sizeof(a));cin >> n;for (i = 1; i <= n; i++){//scanf("%d %d %d", &ddx, &ddy, &t);cin >> ddx >> ddy >> t;for (j = 0; j < 5; j++){x = ddx+dx[j];y = ddy+dy[j];if (x >= 0 && y >= 0&&x<=300&&y<=300){if (tu[x][y] == -1){tu[x][y] = t;}else{tu[x][y] = min(t, tu[x][y]);}}}}if (tu[0][0] == -1){//printf("0\n");cout << 0 << endl;return 0;}if (tu[0][0] == 0){//printf("-1\n");cout << -1 << endl;return 0;}p.x = 0, p.y = 0, p.sum = 0;qua.push(p);a[0][0] = true;while (!qua.empty()){pp = qua.front();qua.pop();if (tu[pp.x][pp.y] == -1){//printf("%d\n", p.sum);cout << pp.sum << endl;return 0;}for (i = 1; i < 5; i++){p.x = pp.x + dx[i];p.y = pp.y + dy[i];p.sum = pp.sum + 1;if ((p.sum< tu[p.x][p.y]||tu[p.x][p.y]==-1) && p.x >= 0 && p.y >= 0){if (a[p.x][p.y] == false){a[p.x][p.y] = true;qua.push(p);}else{continue;}}}}//printf("-1\n");cout << -1 << endl;}


0 0
原创粉丝点击