BFS——Poj 3669 Meteor Shower

来源:互联网 发布:人工智能评价 编辑:程序博客网 时间:2024/05/16 01:50

题目:http://poj.org/problem?id=3669

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18796 Accepted: 4879

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: 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

Source
USACO 2008 February Silver

AC代码:

#include <iostream>#include <cstdio>#include <queue>#include <algorithm>#include <cstring>using namespace std;struct Node {    int i;    int j;    int t;};int a[310][310];int vis [310][310];int b[4][2] = {0,1,0,-1,1,0,-1,0};void fun1 (int u, int v, int t){   for (int i = 0; i < 4; i++)   {       int u1, v1;       u1 = u + b[i][0];       v1 = v + b[i][1];       if (v1 >= 0 && u1 >= 0)         {          if (a[u1][v1] == -1)           a[u1][v1] = t;          else if(a[u1][v1] > t )            a[u1][v1] = t;         }   }}int bfs(){    int ans = -1;    Node st;    st.i = 0;    st.j = 0;    st.t = 0;    queue <Node> Q;    vis[st.i][st.j] = 1;    Q.push(st);    while (Q.size() != 0)    {        Node t = Q.front();        Q.pop();        if (a[t.i][t.j] == -1)  {ans = t.t;break;}        for (int i = 0; i < 4; i++)     {        Node temp;        temp.i = t.i + b[i][0];        temp.j = t.j + b[i][1];        temp.t = t.t + 1;        if (temp.i >=0 && temp.j >= 0 && vis[temp.i][temp.j] == 0)        {            if (a[temp.i][temp.j] == -1)  {ans = temp.t; return ans;}            if (temp.t < a[temp.i][temp.j])  {Q.push(temp);vis[temp.i][temp.j] = 1; continue;}        }     }    }return ans;}int main(){    int M,ans = -1;    memset (a,-1,sizeof(a));    memset (vis,0,sizeof(vis));   scanf("%d",&M);    for (int i =0; i < M; i++)    {        int u,v,t;        scanf("%d%d%d",&u,&v,&t);        if (a[u][v] == -1)           a[u][v] = t;        else if(a[u][v] > t )            a[u][v] = t;        fun1 (u,v,t);    }    ans = bfs();     cout << ans << endl;    return 0;}

题解:
1.BFS(注意判重)
2.一个方块和相邻四个方块会t时毁灭(注意比较每一格最小的t)
3.输入的t可能=0;数组a初值初始化为-1或极大值 。//我原先设的处置为0,error掉无数发!!!
4.大数据输入输出用scanf 和printf, 不要用cin/cout,用cin/cout , g++会tle,C++可过,不过不建议用!!

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 号码忘记交话费变成空号怎么办 多屏互动没办法隔空播放怎么办? 一个人长期受一件事的打击怎么办 物流信息显示快递被别人签收怎么办 现在打工的人被领导骂怎么办 加密狗丢了打不开软件了怎么办 手机微信可以打开网页打不开怎么办 手机中国网打开网速慢该怎么办 在韩国用中国软件网速特别慢怎么办 淘宝退货快递一直没显示到货怎么办 高二美术生集训时文化课怎么办 唯品会不支持7天无理由退货怎么办 sy来4am了孤存怎么办 淘宝网买的电器坏了怎么办 在闲鱼上买的电器是坏的怎么办 打开时全屏不知变成小屏怎么办 苹果6s原装数据线不充电怎么办 vivo手机安卓系统耗电快怎么办 电视机机顶盒插了电源开不了怎么办 苹果手机更新系统后老是闪退怎么办 16g苹果6s手机内存不足怎么办 苹果以前浏览器页面忽然没了怎么办 白衬衣被别的衣服染了怎么办 把宝贝标题改了没访客了怎么办 微信公众号看不到评论时间了怎么办 微店退款退货买家发空快递怎么办 微信二维码收款顾客少付款了怎么办 江西高考二本差5分上线怎么办 湖南文科考生二本上线差两分怎么办 在商场试鞋自己鞋子被偷怎么办 网易账号忘记密码更换手机了怎么办 一人在外地钱花光了怎么办 银行卡被公安冻结卡里的钱怎么办 在店铺不上班了押工资了怎么办? 在银座的押金拿不出来怎么办 唯品会退货时快递单号填错了怎么办 天猫店铺快递已发到不了怎么办 中邮消费贷没审核通过怎么办 房贷扣款日忘存钱了怎么办 农业银行卡输密码次数超限载怎么办 农业银行卡丢了不知道卡号怎么办