Q:看到题目可能会疑惑变砸变跑怎么实现呢?
A:所以我们要在bfs之前初始化好,陨石砸下来的位置,然后从起始点进行bfs
Q:怎么进行bfs呢?
A:从起始点开始向四周便利,然后在开始统计的数组中这个时候土地还是完好的那么考虑移动,如果这个点一直不会被砸到,那么就可以输出答案了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
using namespace std;
int a[350][350],m;
int num[350][350],vis[350][350],ans=-1;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
inline int read()
{
int res=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){res=(res<<1)+(res<<3)+(ch&15);ch=getchar();}
return res*f;
}
struct node
{
int x;
int y;
};
queue<node>q;
void bfs()
{
vis[0][0]=1;
node st;st.x=0,st.y=0;
q.push(st);
while(!q.empty())
{
node k=q.front();q.pop();
if(a[k.x][k.y]>1000)
{
ans=num[k.x][k.y];
break;
}
for(int i=0;i<4;i++)
{
int xx=k.x+dx[i];
int yy=k.y+dy[i];
if(xx>=0&&xx<=320&&yy>=0&&yy<=320)
{
if(!vis[xx][yy])
{
if(a[xx][yy]>num[k.x][k.y]+1)
{
num[xx][yy]=num[k.x][k.y]+1;
vis[xx][yy]=1;
node ne;ne.x=xx;ne.y=yy;
q.push(ne);
}
}
}
}
}
}
int main()
{
m=read();
memset(a,1e6,sizeof(a));
for(int i=1;i<=m;i++)
{
int x,y,z;x=read();y=read();z=read();
a[x][y]=min(z,a[x][y]);
for(int k=0;k<4;k++)//向四周初始化
if(x+dx[k]>=0&&y+dy[k]>=0)
a[x+dx[k]][y+dy[k]]=min(a[x+dx[k]][y+dy[k]],z);
}
bfs();
printf("%d\n",ans);
return 0;
}