poj 3669 bfs

题意:

流星雨,给每次在x,y,t时间点落下来的流星。

求从0,0点开始,最快跑到哪个地方可以安全避难。


解析:

bfs,开始图建的有点问题。

然后是300那个限制要去掉。

不该仔细的地方。。。太仔细了。。。。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int maxn = 300 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int g[maxn][maxn];
int step[maxn][maxn];

bool ok(int x, int y)
{
    if (0 <= x && 0 <= y)///debug  300不要
        return true;
    return false;
}

struct Node
{
    int x, y;
    Node() {}
    Node(int _x, int _y)
    {
        x = _x;
        y = _y;
    }
};

int bfs()
{
    memset(step, -1, sizeof(step));
    step[0][0] = 0;
    queue<Node> q;
    q.push(Node(0, 0));

    while (!q.empty())
    {
        Node now = q.front();
        q.pop();
        int x = now.x;
        int y = now.y;
//        cout << x << " " << y << endl;
        if (g[x][y] == -1)
        {
            return step[x][y];
        }
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            if (ok(nx, ny))
            {
                if (step[nx][ny] == -1 && (step[x][y] + 1 < g[nx][ny] || g[nx][ny] == -1))
                {
                    step[nx][ny] = step[x][y] + 1;
                    q.push(Node(nx, ny));
                }
            }
        }
    }
    return -1;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int m;
    while (~scanf("%d", &m))
    {
        memset(g, -1, sizeof(g));
        for (int i = 0; i < m; i++)
        {
            int x, y, t;
            scanf("%d%d%d", &x, &y, &t);
//            cout << x << " " << y << " " << t << endl;
            if (g[x][y] > t || g[x][y] == -1)
            {
                g[x][y] = t;
            }
            for (int d = 0; d < 4; d++)
            {
                int nx = x + dir[d][0];
                int ny = y + dir[d][1];
                if (ok(nx, ny))
                {
                    if (g[nx][ny] == -1 || g[nx][ny] > t)
                        g[nx][ny] = t;
                }
            }
        }
//        for (int i = 0; i < 4; i++)
//        {
//            for (int j = 0; j < 4; j++)
//            {
//                printf("%3d ", g[i][j]);
//            }
//            cout << endl;
//        }
        printf("%d\n", bfs());
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值