Meteor Shower (bfs)
来源:互联网 发布:软件接口安全设计 编辑:程序博客网 时间:2024/05/22 17:25
Meteor Shower
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 2
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.
40 0 22 1 21 1 20 3 5
5
#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>#include <queue>using namespace std;int n;const int maxn = 310;const int INF = 0x3fffffff;int map[maxn][maxn];int visited[maxn][maxn];int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};struct node { int x, y, step; void set(int x, int y, int s) { this->x = x; this->y = y; step = s; } friend bool operator < (const node& n1,const node& n2) { return n1.step > n2.step; }};int bfs() { memset(visited, 0, sizeof(visited)); priority_queue<node> que; node cur, nex; cur.set(0, 0, 0); que.push(cur); visited[0][0] = 1; while (!que.empty()) { cur = que.top(); que.pop(); if (map[cur.x][cur.y] == INF) { //cout << cur.x << cur.y << endl; return cur.step; } for (int i = 0; i < 4; ++i) { nex.set(cur.x + dir[i][0], cur.y + dir[i][1], cur.step + 1); if (nex.x >= 0 && nex.y >= 0 && map[nex.x][nex.y] > nex.step && !visited[nex.x][nex.y]) { que.push(nex); visited[nex.x][nex.y] = 1; } } } return -1;}int main() { //freopen("in.txt", "r", stdin); while (scanf("%d", &n) != EOF){ int x, y, t; for (int i = 0; i < maxn; ++i) { for (int j = 0; j < maxn; ++j) { map[i][j] = INF; } } for (int i = 0; i < n; ++i) { scanf("%d%d%d", &x, &y, &t); map[x][y] = min(map[x][y], t); for (int j = 0; j < 4; ++j) { int nx = x + dir[j][0]; int ny = y + dir[j][1]; if (nx >= 0 && ny >= 0) { map[nx][ny] = min(map[nx][ny], t); } } } printf("%d\n", bfs()); }}
- Meteor Shower (bfs)
- Meteor Shower (bfs)
- POJ3669 Meteor Shower(bfs)
- POJ3669-Meteor Shower(BFS)
- POJ Meteor Shower(BFS)
- PKU 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower (BFS)
- POJ 3669 Meteor Shower (BFS + 预处理)
- POJ 3669 Meteor Shower (bfs)
- POJ 3669 Meteor Shower(bfs)
- poj 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669-Meteor Shower(BFS)
- POJ - 3669 - Meteor Shower(bfs)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(bfs)
- 关于C#的一些几个关键的概念:out,ref,const,readonly
- VC++2010开发权威指南--笔记一
- C 库函数 – memset()
- 《TP5.0学习笔记---视图和模板篇 》
- SpringCloud微服务架构搭建(一):注册与发现
- Meteor Shower (bfs)
- matchTemplate opencv3.0
- Hibernate小记
- c语言通讯录
- Mysql(四)
- Android设置透明、半透明等效果
- 名词以及解释
- 520. Detect Capital+substring字符串截取
- Jsp对于缓存的处理