POJ3669(meteor shower, BFS)TLE

16 篇文章 0 订阅

when initial, try to convenient for later operation, e.g: if step[][] = -1, will hard to compare mini with other.
However, my code still TLE for unknown reason.

#define LOCAl

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

#define SIZE 310
#define INF 10000000

using namespace std;

bool is_legal(int newx, int newy){
    if(0 <= newx &&  newx < SIZE && 0 <= newy && newy < SIZE)
        return true;
    return false;
}

int main(void){
#ifdef LOCAl
    freopen("data.in", "r", stdin);
#endif
    int dir[][2] = {0, 0, 1, 0, -1, 0, 0, 1, 0, -1};
    int m;
    int x, y, t;
    int map[SIZE][SIZE];
    int step[SIZE][SIZE];

    for(int i = 0; i < SIZE; i++){
        fill(map[i], map[i] + SIZE, INF);
    }

    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d%d%d", &x, &y, &t);

        for(int i = 0; i < 5; i++){
            int newx = x + dir[i][0];
            int newy = y + dir[i][1];

            if(is_legal(newx, newy) && map[newx][newy] > t)
                map[newx][newy] = t;
        }
    }
    step[0][0] = 0;

    int flag = false;
    queue<pair<int, int> > q;
    q.push(make_pair(0, 0));

    while(!q.empty()){
        pair<int, int> temp = q.front();
        q.pop();

        //printf("x:%d y:%d map:%d step:%d\n", temp.first, temp.second,
        //   map[temp.first][temp.second], step[temp.first][temp.second]);

        if(map[temp.first][temp.second] == INF){
            printf("%d\n", step[temp.first][temp.second]);
            flag = true;
            break;
        }

        for(int i = 1; i < 5; i++){
            int newx = temp.first + dir[i][0];
            int newy = temp.second + dir[i][1];

            if(is_legal(newx, newy) && 
                step[temp.first][temp.second] + 1 < map[newx][newy])
            {
                q.push(make_pair(newx, newy));
                step[newx][newy] = step[temp.first][temp.second] + 1;
            }
        }
    }

    if(!flag)
        printf("-1\n");

    return 0;
}

答案地址

#include<cstdio>
#include<algorithm>
#include<queue>
#define MAX_M 50005
#define MAX_SIZE 303
#define MAX_TIME 9999
using namespace std;

int M;
int X, Y, T;

int map[MAX_SIZE][MAX_SIZE];

int dist[MAX_SIZE][MAX_SIZE];

int dirction[5][2] = {0, -1, -1, 0, 0, 1, 1, 0, 0, 0};

typedef pair<int, int> P;

struct Meteor {
    int X;
    int Y;
    int T;
} meteors[MAX_M];

bool cmp (Meteor a, Meteor b) {
    if (a.T < b.T) return true;
    else return false;
}

bool isLegal(int x, int y) {
    if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
    else return false;
}

int bfs() {
    queue<P> que;
    if (map[0][0] == 0) return -1;
    else if (map[0][0] == MAX_TIME) return 0;
    que.push(P(0, 0));
    while (que.size()) {
        P p = que.front();
        que.pop();
        int px = p.first, py = p.second;
        if (px >= MAX_SIZE || py >= MAX_SIZE) continue;
        for(int i = 0; i < 4; i ++) {
            int tx = px + dirction[i][0], ty = py + dirction[i][1];
            if (isLegal(tx, ty)) {
                if (map[tx][ty]  == MAX_TIME) {
                    return ++ dist[px][py];
                }
                if (dist[px][py] + 1 < map[tx][ty] && !dist[tx][ty]) {
                    que.push(P(tx, ty));
                    dist[tx][ty] = dist[px][py] + 1;
                }
            }
        }
    }
    return -1;
}

void solve() {
    for (int i = 0; i < MAX_SIZE; i ++) {
        for (int j = 0; j < MAX_SIZE; j ++) {
            map[i][j] = MAX_TIME;
        }
    } 
    for (int i = 0; i < M; i ++) {
        scanf("%d %d %d", &meteors[i].X, &meteors[i].Y, &meteors[i].T);
    }
    sort(meteors, meteors + M, cmp);
    for (int i = 0; i < M; i ++) {
        for (int j = 0; j < 5; j ++) {
            int tx = meteors[i].X, ty = meteors[i].Y;
            tx += dirction[j][0];
            ty += dirction[j][1];
            if (isLegal(tx, ty) && map[tx][ty] > meteors[i].T) {
                map[tx][ty] = meteors[i].T;
            }
        }
    }
    printf("%d\n", bfs());
}

int main() {
    while (~scanf("%d", &M)) {
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值