POJ - 3669 - Meteor Shower(bfs)

来源:互联网 发布:淘宝客服电话是多少 编辑:程序博客网 时间:2024/05/17 02:53

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15006 Accepted: 3955

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


题意:在第一象限内,有一个人躲避流星雨的攻击,一个流星雨在第t秒砸在(x, y)的位置,并砸到周围四个格子。问这个人能不能在被流星砸到之前躲在安全的地方,如果能最少需要走多少步,不能的话输出-1

感觉自己最近智商是负数,刚刚把自己锁在门外进不去,手机也锁进去了,最近几天要失联orz

先预处理出格子是否会被砸到,在何时被砸到,然后bfs

总觉得搜索很神奇


#include <stdio.h>#include <math.h>#include <string.h>#include <algorithm>#include <queue>#include <iostream>#include <assert.h>#define INF 0x3f3f3f3fusing namespace std;int M;int maps[350][350];int dir[5][2] = {0, 0, 1, 0, -1, 0, 0, 1, 0, -1};struct node{  int x, y;  int time;};bool judge(int x, int y){  if (x < 0 || y < 0 || x > 400 || y > 400)    return false;  return true;}int bfs(){  if (maps[0][0] == -1) return 0;  //如果是-1,说明此格子安全  if (maps[0][0] == 0) return -1;  //初始格子在一开始就被砸到  node now, next;  next.x = next.y = next.time = 0;  queue<node>q;  q.push(next);  while (!q.empty()) {  //bfs    now = q.front();    q.pop();    for (int i = 1; i < 5; i++) {      next.x = now.x + dir[i][0];      next.y = now.y + dir[i][1];      next.time = now.time + 1;      if (!judge(next.x, next.y)) continue;      if (maps[next.x][next.y] == -1) return next.time;      if (next.time >= maps[next.x][next.y]) continue;      maps[next.x][next.y] = next.time;      q.push(next);    }  }  return -1;}int main(){  while (cin >> M) {    int xi, yi, ti;    memset(maps, -1, sizeof(maps));    for (int i = 0; i < M; i++) {      scanf("%d%d%d", &xi, &yi, &ti);      for (int j = 0; j < 5; j++) {        int tx = xi + dir[j][0];        int ty = yi + dir[j][1];        if (!judge(tx, ty)) continue;        if (maps[tx][ty] == -1)          maps[tx][ty] = ti;        else          maps[tx][ty] = min(maps[tx][ty], ti);      }    }    int ans = bfs();    printf("%d\n", ans);  }  return 0;}






0 0
原创粉丝点击