国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ3669-Meteor Shower-BFS

2019-11-08 18:28:46
字體:
來源:轉載
供稿:網友

原題鏈接 Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18098 Accepted: 4704 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: MLines 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 Source

USACO 2008 February Silver

#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int INF = 0x3f3f3f3f;const int dx[4]={0,0,-1,1};const int dy[4]={-1,1,0,0};const int maxn=400 + 10;int a[maxn][maxn],d[maxn][maxn];//隕石最早到達的時間和到達該點最短的時間int n;typedef struct node{ int x,y;}node;int bfs(){ if(a[0][0]==0) return -1; queue<node> que; que.push((node){0,0}); d[0][0]=0; while(!que.empty()){ node now=que.front(); que.pop(); int nowx=now.x,nowy=now.y; if(a[nowx][nowy]==INF) return d[nowx][nowy]; for(int i=0;i<4;i++){ int nx=nowx+dx[i],ny=nowy+dy[i]; if(nx>=0 && ny>=0 && d[nx][ny]==INF && d[nowx][nowy] + 1 < a[nx][ny]){ d[nx][ny]=d[nowx][nowy]+1; que.push((node){nx,ny}); } } } return -1;}int main(){ while(scanf("%d",&n)==1){ for(int i=0;i<maxn;i++){ fill(a[i],a[i]+maxn,INF); fill(d[i],d[i]+maxn,INF); } int x,y,t; while(n--){ scanf("%d%d%d",&x,&y,&t); a[x][y]=min(a[x][y],t); for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=0 && ny>=0) a[nx][ny]=min(a[nx][ny],t); } } int res=bfs(); cout << res << endl; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙胜| 无为县| 邯郸市| 连山| 南溪县| 北川| 桃江县| 顺平县| 乐平市| 丁青县| 科技| 大洼县| 房产| 特克斯县| 浮山县| 钟山县| 临汾市| 板桥市| 武定县| 渝中区| 镇巴县| 丹东市| 正阳县| 易门县| 启东市| 高青县| 登封市| 乐安县| 绵竹市| 和硕县| 平阳县| 平果县| 大新县| 龙胜| 和硕县| 萨迦县| 晋中市| 柘城县| 奉节县| 岑巩县| 宣化县|