GG_and_go_to_died @ 2024-03-08 18:58:42
#include <iostream>
#include <queue>
using namespace std;
int dx[5]={0, 0, 0, -1, 1};
int dy[5]={0, 1, -1, 0, 0};
int times[505][505];
bool vis[505][505];
struct node
{
int time;
int x, y;
};
void bfs()
{
queue<node> q;
q.push((node){0, 0, 0});
vis[0][0] = true;
while (! q.empty())
{
int nx = q.front().x;
int ny = q.front().y;
int nt = q.front().time;
q.pop();
for (int i = 1;i <= 4; ++ i)
{
int tx = nx + dx[i];
int ty = ny + dy[i];
int tt = nt + 1;
if (! vis[tx][ty] && (times[tx][ty] == 0x7fffffff || tt < times[tx][ty]) && tx >= 0 && ty >= 0)
{
if (times[tx][ty] == 0x7fffffff)
{
cout << tt << endl;
return;
}
vis[tx][ty] = true;
q.push((node){tt, tx, ty});
}
}
}
cout << -1 << endl;
return;
}
int main()
{
int m;
cin >> m;
for (int i = 0;i < 505; ++ i)
{
for (int j = 0;j < 505; ++ j)
{
times[i][j] = 0x7fffffff;
}
}
for (int i = 1;i <= m; ++ i)
{
int x, y, t;
cin >> x >> y >> t;
times[x][y] = min(times[x][y], t);
times[x + 1][y] = min(times[x][y], t);
times[max(x - 1, 0)][y] = min(times[x][y], t);
times[x][y + 1] = min(times[x][y], t);
times[x][max(y - 1, 0)] = min(times[x][y], t);
}
bfs();
return 0;
}
by dry_ @ 2024-03-11 20:20:37
queue<node> q;
要改成
priority_queue<node> q;
by tjtdrxxz @ 2024-03-11 21:17:42
哦对了,我用
priority_queue<node>
的时候出了问题。。 要不把
!vis[tx][ty]
给删了吧