再一次被POJ输入输出坑死的题目 Meteor Shower

题目:https://vjudge.net/problem/POJ-3669

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: M
* Lines 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




题意:巨大流星雨即将袭来。每个流星会对击中的地方以及周围(上下左右四格)造成破坏。
Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内)。
已知每移动一格需要1个时间单位,被流星破坏后的地方不能再进入。
给出M个流星在T时刻击中的地方(X, Y),问Bessie能否逃到安全的地方,若能输出最短时间,否则输出-1


题解:bfs搜索(可以优先队列优化)


代码:
//优先队列
//
#include<iostream> //#include<cstdio> //#include<cstring> //#include<cmath> //#include<queue> //#include<set> //#include<algorithm> //#include<map> //#define maxn 200005 //using namespace std; //struct node{ // int ans,x,y; //}; //bool operator < (const node &a,const node &b) //{ // return a.ans>b.ans; //} //int mp[305][305]; //int vis[305][305]; //node s1,s2,s3; //int nx[5]={0,0,1,-1,0}; //int ny[5]={-1,1,0,0,0}; //int bfs(int a,int b) //{ // priority_queue<node>que; // if(mp[0][0]==0)return -1; // if(mp[0][0]==-1)return 0; // s1.x=a; // s1.y=b; // s1.ans=0; // vis[0][0]=1; // que.push(s1); // //cout<<"a"<<endl; // while(!que.empty()) // { // //cout<<"a"<<endl; // s2=que.top(); // que.pop(); // if(mp[s2.x][s2.y]==-1)return s2.ans; // for(int i=0;i<4;i++) // { // s3.x=s2.x+nx[i]; // s3.y=s2.y+ny[i]; // s3.ans=s2.ans+1; // if(s3.x>=0&&s3.y>=0&&(mp[s3.x][s3.y]>s3.ans||mp[s3.x][s3.y]==-1)) // { //// if(mp[s3.x][s3.y]>s3.ans){ //// vis[s3.x][s3.y]=1; //// que.push(s3); //// } //// if(mp[s3.x][s3.y]==-1) //// { //// //cout<<"a"<<endl; //// return s3.ans; //// } // if(!vis[s3.x][s3.y])que.push(s3); // vis[s3.x][s3.y]=1; // // } // } // } // return -1; //} //int main() //{ // int m,xx,yy,tt,xxx,yyy; // while(scanf("%d",&m)!=EOF){ // memset(mp,-1,sizeof(mp)); // for(int i=0;i<m;i++) // { // scanf("%d%d%d",&xx,&yy,&tt); // for(int j=0;j<5;j++) // { // xxx=xx+nx[j]; // yyy=yy+ny[j]; // if(xxx>=0&&yyy>=0){ // if(mp[xxx][yyy]==-1)mp[xxx][yyy]=tt; // else mp[xxx][yyy]=min(mp[xxx][yyy],tt); // } // } // } // memset(vis,0,sizeof(vis)); // int flag=bfs(0,0); // printf("%d ",flag); // } // return 0; //} #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 200005 using namespace std; struct node{ int ans,x,y; }; queue<node>que; int mp[1005][1005]; int vis[1005][1005]; node s1,s2,s3; int nx[5]={0,0,1,-1,0}; int ny[5]={-1,1,0,0,0}; int bfs(int a,int b) { if(mp[0][0]==0)return -1; if(mp[0][0]==-1)return 0; s1.x=a; s1.y=b; s1.ans=0; que.push(s1); //cout<<"a"<<endl; while(!que.empty()) { //cout<<"a"<<endl; s2=que.front(); que.pop(); if(mp[s2.x][s2.y]==-1)return s2.ans; for(int i=0;i<4;i++) { s3.x=s2.x+nx[i]; s3.y=s2.y+ny[i]; s3.ans=s2.ans+1; if(!vis[s3.x][s3.y]&&s3.x>=0&&s3.y>=0) { if(mp[s3.x][s3.y]>s3.ans){ vis[s3.x][s3.y]=1; que.push(s3); } if(mp[s3.x][s3.y]==-1) { //cout<<"a"<<endl; return s3.ans; } } } } return -1; } int main() { int m,xx,yy,tt,xxx,yyy; while(scanf("%d",&m)!=EOF){ memset(mp,-1,sizeof(mp)); for(int i=0;i<m;i++) { scanf("%d%d%d",&xx,&yy,&tt); for(int j=0;j<5;j++) { xxx=xx+nx[j]; yyy=yy+ny[j]; if(xxx>=0&&yyy>=0){ if(mp[xxx][yyy]==-1)mp[xxx][yyy]=tt; else mp[xxx][yyy]=min(mp[xxx][yyy],tt); } } } memset(vis,0,sizeof(vis)); int flag=bfs(0,0); printf("%d ",flag); } return 0; }

可恶的POJ,,,第一遍就该过了,,又卡我scanf输入输出


原文地址:https://www.cnblogs.com/huangzzz/p/8612871.html