题目
坑点 1:流星撞击范围在0~300,人可以走到300之外,保存地图的数组要开大一点。
坑点2:人在起点也会被 T = 0 的流星砸死。
#include <iostream>
#include <cstring>
#include <queue>
#include <fstream>
using namespace std;
struct Coordinate{
int row;
int col;
};
Coordinate mov_dir[5] = {{0, 0}, {0, -1}, {-1, 0}, {0, 1}, {1, 0}};//五个方向,包含原点
Coordinate start = {0, 0};
int map[500][500], now_time[500][500];//分别保存地图的状态和到达改点所需的时间
queue<Coordinate> que;
int M, row, col, t;
void UpdateStrikenPoint(int row, int col, int t)//标记流星可以撞击到的位置,
//保存该位置最早被撞击到的时间
{
for (int i = 0; i < 5; i++) {
int now_row = row + mov_dir[i].row;
int now_col = col + mov_dir[i].col;
if (now_row < 0 || now_col < 0)
continue;
if (map[now_row][now_col] == -1) {
map[now_row][now_col] = t;
}
else if (map[now_row][now_col] > t) {
map[now_row][now_col] = t;
}
}
}
int bfs()
{
while (que.size()) {
Coordinate head = que.front();
if (map[head.row][head.col] == -1) {
return now_time[head.row][head.col];
}
que.pop();
Coordinate now;
for (int i = 0; i < 5; i++) {
now.row = head.row + mov_dir[i].row;
now.col = head.col + mov_dir[i].col;
if (now.row < 0 || now.col < 0) {
continue;
}
if (map[now.row][now.col] != -1 && (now_time[head.row][head.col] + 1
>= map[now.row][now.col])) {
continue;
}
if (now_time[now.row][now.col] == -1){
que.push(now);
now_time[now.row][now.col] = now_time[head.row][head.col] + 1;
}
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// ifstream IN("IN.txt", ios::in);
memset(map, -1, sizeof(map));//地图初始值为-1.
memset(now_time, -1, sizeof(now_time));
cin >> M;
for (int i = 0; i < M; i++) {
cin >> row >> col >> t;
UpdateStrikenPoint(row, col, t);
}
que.push(start);
now_time[0][0] = 0;
int ans = bfs();
cout << ans << endl;
}