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;
}