2021.8.5 Meteor Shower
题目:P2895 [USACO08FEB]Meteor Shower S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
4 0 0 2 2 1 2 1 1 2 0 3 5
Sample Output
5
这两天练习做的题,第一遍用了DFS做,最后超时了。第二遍用BFS做,有一个测试点没过去,不知道哪里出了问题,等BFS熟练了再看看。
方法一:DFS
#include<iostream>
#include<cstring>
using namespace std;
int M, mapp[305][305], ans = 1e9, f[305][305];//f数组标记每个点的最短时间,进行记忆化搜索
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int x, int y, int t)//x,y代表当前的坐标,t代表当前所用的时间
{
if (t >= ans)
{
return;
}
if (mapp[y][x] < 0 && mapp[y - 1][x] < 0 && mapp[y + 1][x] < 0 && mapp[y][x - 1] < 0 && mapp[y][x + 1] < 0 )
{
ans = min(ans, t);
return;
}
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if ((mapp[yy][xx] >= 0 && t + 1 >= mapp[yy][xx]) ||
(mapp[yy - 1][xx] >= 0 && t + 1 >= mapp[yy - 1][xx]) ||
(mapp[yy + 1][xx] >= 0 && t + 1 >= mapp[yy + 1][xx]) ||
(mapp[yy][xx - 1] >= 0 && t + 1 >= mapp[yy][xx - 1]) ||
(mapp[yy][xx + 1] >= 0 && t + 1 >= mapp[yy][xx + 1]) || xx < 0 || yy < 0)
{
continue;
}
f[yy][xx] = f[y][x] + 1;
dfs(xx, yy, f[yy][xx]);
}
}
int main(void)
{
int x, y;
memset(mapp, -1, sizeof(mapp));
scanf_s("%d",&M);
for (int i = 1; i <= M; i++)
{
scanf_s("%d%d",&x,&y);
scanf_s("%d", &mapp[y][x]);//mapp上某一位置大小代表流星到来的时间
}
dfs(0, 0, 0);
if (ans < 1e9)
printf("%d", ans);
else
printf("-1");
return 0;
}
方法二:BFS
#include<iostream>
#include<queue>
using namespace std;
struct Pos {
int x, y;
};
int M, mapp[305][305], path[305][305];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int flg[305][305],flag;
void bfs(int x, int y)
{
queue<Pos> qu;
Pos p;
p.x = x; p.y = y;
qu.push(p);
while (!qu.empty())
{
p = qu.front(); qu.pop();
if (mapp[p.y][p.x] < 0 && mapp[p.y - 1][p.x] < 0 && mapp[p.y + 1][p.x] < 0 && mapp[p.y][p.x - 1] < 0 && mapp[p.y][p.x + 1] < 0)
{
flag = 1;
printf("%d", path[p.y][p.x]);
return;
}
for (int i = 0; i < 4; i++)
{
int xx = p.x + dx[i];
int yy = p.y + dy[i];
if ((mapp[yy][xx] >= 0 && path[p.y][p.x] + 1 >= mapp[yy][xx]) ||
(yy - 1 >=0 && mapp[yy - 1][xx] >= 0 && path[p.y][p.x] + 1 >= mapp[yy - 1][xx]) ||
(mapp[yy + 1][xx] >= 0 && path[p.y][p.x] + 1 >= mapp[yy + 1][xx]) ||
(xx - 1 >=0 && mapp[yy][xx - 1] >= 0 && path[p.y][p.x] + 1 >= mapp[yy][xx - 1]) ||
(mapp[yy][xx + 1] >= 0 && path[p.y][p.x] + 1 >= mapp[yy][xx + 1]) || xx < 0 || yy < 0 || flg[yy][xx])
{
continue;
}
flg[yy][xx] = 1;//标记已经搜索过了
path[yy][xx] = path[p.y][p.x] + 1;
Pos p2;
p2.y = yy; p2.x = xx;
qu.push(p2);
}
}
}
int main(void)
{
int x, y;
memset(mapp, -1, sizeof(mapp));
scanf_s("%d",&M);
for (int i = 1; i <= M; i++)
{
scanf_s("%d%d",&x,&y);
scanf_s("%d", &mapp[y][x]);//mapp上某一位置大小代表流星到来的时间
}
flg[0][0] = 1;
bfs(0,0);
if (!flag)
cout<<-1;
return 0;
}
推荐阅读
-
React 与 React-Native 使用同一个 meteor 后台
-
Meteor Shower POJ - 3669 (bfs+优先队列)
-
meteor的入门安装讲解
-
Intel 4工艺Meteor Lake自曝:热设计功耗最低5W
-
Intel 4工艺Meteor Lake 14代酷睿晶圆首秀:2023年发布
-
Intel 7nm工艺进展顺利 2023年Meteor Lake处理器首发
-
Intel 14代酷睿Meteor Lake处理器工厂实拍照曝光:7nm工艺
-
Intel称7nm工艺Meteor Lake处理器Q3试产 首次用上多芯片架构
-
Meteor 用户登录注册密码验证 php版本
-
关于bootstrap的datepicker在meteor应用中的使用(不包含bootstrap框架)