poj-3669

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

广搜


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>
#include<stdexcept>

using namespace std;

int n;

struct ss
{
    int x;
    int y;
    int tm;
}p[50010];

int pp[333][333];//最小被砸时间 初始无穷大
int vis[333][333];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

bool cmp (ss a ,ss b)
{
    return a.tm < b.tm;
}

int bfs ()
{
    memset(vis,0,sizeof(vis));
    queue<ss> q ;
    ss qq ;
    ss qqq ;
    qq.x = 0;
    qq.y = 0;
    qq.tm = 0;
    q.push(qq);

    while (!q.empty())
    {
        qqq = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            qq = qqq;
            qq.x = qq.x + dir[i][0];
            qq.y = qq.y + dir[i][1];
            qq.tm = qq.tm+1;
            if(qq.x>=0 && qq.y>=0 && qq.tm < pp[qq.x][qq.y] && !vis[qq.x][qq.y] )
            {
                vis[qq.x][qq.y] = 1;
                if (pp[qq.x][qq.y] > p[n-1].tm)
                {
                    return qq.tm;
                }
                q.push(qq);
            }
        }
    }
    return -1;
}

int main ()
{
    while (scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<=322;i++)
            for(int j=0;j<=322;j++)
            {
                pp[i][j] = 10000;
            }

        for(int i=0;i<n;i++)
        {
            scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].tm );
        }
        //对陨石降落时间排序
        sort(p,p+n,cmp);

        for(int i=0;i<n;i++)
            for(int j=0;j<4;j++)
            {
                if (pp[p[i].x][p[i].y] > p[i].tm)
                    pp[p[i].x][p[i].y] = p[i].tm;

                int xx = p[i].x + dir[j][0];
                int yy = p[i].y + dir[j][1];

                if (xx >= 0 && yy >= 0 && pp[xx][yy] > p[i].tm)
                {
                    pp[xx][yy] = p[i].tm;
                }
            }


        if(pp[0][0] == 0)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n",bfs());
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值