poj 3669(BFS)

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25105 Accepted: 6487

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

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5

题目大意:输入N组数据,前两个代表(x,y)后面代表什么时间会落下流星,求最少的移动步数

类似迷宫问题,肯定是广搜,我用了深搜之后TLE了,这道题最主要的是处理每次流星落下的时间,这里就用最开始的时间表示,假设在(1,1)的时候3s 的时候落一次5s 的时候落一次,肯定保留3,如果你在3s前已经经过了,那么以后不管有没有流星落下,都不会砸中。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    int x;
    int y;
    int step;
}num;
queue<node>q;
int dp[410][410];//起码要是300以上,第一次交的时候开了302,结果MLE了
int yy[]={0,0,1,-1,0};
int xx[]={1,-1,0,0,0};
bool v[400][400];
int sum=0;
int MAX=0x3f3f3f3f;
int bfs()
{
    node next;
    while(q.size())
    {
        num=q.front();
        q.pop();
        if(dp[num.x][num.y]==-1)
            return num.step;
        for(int i=0;i<4;i++)
        {
            next.x=num.x+xx[i];
            next.y=num.y+yy[i];
            next.step=num.step+1;
            if(next.x>=0&&next.y>=0&&(next.step<dp[next.x][next.y]||dp[next.x][next.y]==-1)&&!v[next.x][next.y])//这里一开始写了是<=300结果WA
            {
                v[next.x][next.y]=1;
                
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int n;
    cin>>n;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++)
        {
            int x,y,t;//要用需要加上ios::sync_with_stdio(0);cin.tie(0);就可以,具体为什么   BaiDu
            cin>>x>>y>>t;//这里如果是scanf的话用g++交就可以了,若用cin就用c++交,否则会TLE
            for(int j=0;j<5;j++)
            {
                if((x+xx[j])>=0&&(y+yy[j])>=0){
                        if(dp[xx[j]+x][y+yy[j]]==-1)
                        dp[xx[j]+x][y+yy[j]]=t;
                      else
                    dp[xx[j]+x][y+yy[j]]=min(dp[xx[j]+x][y+yy[j]],t);
                }
            }
        }
        num.x=0;
        num.y=0;
        num.step=0;
        v[num.x][num.y]=1;
        q.push(num);
        cout<<bfs()<<endl;
//        for(int i=0;i<=10;i++)
//        {
//            for(int j=0;j<=10;j++)
//            cout<<dp[i][j]<<"     ";
//            cout<<endl;
//        }
    return 0;
}

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    int x;
    int y;
    int step;
}num;
queue<node>q;
int dp[410][410];
int yy[]={0,0,1,-1,0};
int xx[]={1,-1,0,0,0};
bool v[400][400];
int sum=0;
int MAX=0x3f3f3f3f;
int bfs()
{
    node next;
    while(q.size())
    {
        num=q.front();
        q.pop();
        if(dp[num.x][num.y]==MAX)
            return num.step;
        for(int i=0;i<4;i++)
        {
            next.x=num.x+xx[i];
            next.y=num.y+yy[i];
            if(next.x>=0&&next.y>=0&&(num.step+1)<dp[next.x][next.y]&&v[next.x][next.y]==0)
            {
                v[next.x][next.y]=1;
                next.step=num.step+1;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int n;
    cin>>n;
    memset(dp,MAX,sizeof(dp));
    for(int i=0;i<n;i++)//10^4就要用scanf了
        {
            int x,y,t;
            scanf("%d%d%d",&x,&y,&t);//scanf比cin快很多

                dp[x][y]=min(dp[x][y],t);
            dp[x+1][y]=min(dp[x+1][y],t);
            dp[x][y+1]=min(dp[x][y+1],t);
            if(x-1>=0)
                dp[x-1][y]=min(dp[x-1][y],t);
            if(y-1>=0)
                dp[x][y-1]=min(dp[x][y-1],t);
        }
        num.x=0;
        num.y=0;
        num.step=0;
        v[num.x][num.y]=1;
        q.push(num);
        cout<<bfs()<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动控制节水灌溉技术的高低代表着农业现代化的发展状况,灌溉系统自动化水平较低是制约我国高效农业发展的主要原因。本文就此问题研究了单片机控制的滴灌节水灌溉系统,该系统可对不同土壤的湿度进行监控,并按照作物对土壤湿度的要求进行适时、适量灌水,其核心是单片机和PC机构成的控制部分,主要对土壤湿度与灌水量之间的关系、灌溉控制技术及设备系统的硬件、软件编程各个部分进行了深入的研究。 单片机控制部分采用上下位机的形式。下位机硬件部分选用AT89C51单片机为核心,主要由土壤湿度传感器,信号处理电路,显示电路,输出控制电路,故障报警电路等组成,软件选用汇编语言编程。上位机选用586型以上PC机,通过MAX232芯片实现同下位机的电平转换功能,上下位机之间通过串行通信方式进行数据的双向传输,软件选用VB高级编程语言以建立友好的人机界面。系统主要具有以下功能:可在PC机提供的人机对话界面上设置作物要求的土壤湿度相关参数;单片机可将土壤湿度传感器检测到的土壤湿度模拟量转换成数字量,显示于LED显示器上,同时单片机可采用串行通信方式将此湿度值传输到PC机上;PC机通过其内设程序计算出所需的灌水量和灌水时间,且显示于界面上,并将有关的灌水信息反馈给单片机,若需灌水,则单片机系统启动鸣音报警,发出灌水信号,并经放大驱动设备,开启电磁阀进行倒计时定时灌水,若不需灌水,即PC机上显示的灌水量和灌水时间均为0,系统不进行灌水。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值