题意:
流星雨,给每次在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;
}