1 條題解
-
0
题目大意
有 行 列的四连通的地图,地图中有男孩、女孩、鬼、墙、空地。男孩每秒钟可移动 格。女孩每秒钟可移动 格。每个鬼的杀伤区域每秒钟扩大 格(无视墙)。求两人的最短会合时间 。
解题思路
相比于一般的走迷宫问题,本题中起点、终点的位置都在变化。如果采取深搜,显然难以回溯。
不难想到,其实男女的移动是互不影响的。所以使用双向广搜,并使用数组记录可达性。男孩先更新 次,女孩再更新 次。把男孩和女孩的状态分开两个队列存储会更容易实现一些。
若某次更新后,某个位置同时能被两个人到达,则此时就是会合时间。注意,随着鬼杀伤区域的扩大,本来可以到达的地块可能后来不能再到达。
鬼的杀伤区域不需要复杂的处理。只需要计算出人和鬼的距离并与 比较即可。
参考代码
#include<bits/stdc++.h> using namespace std; #define ull unsigned long long #define ll long long #define ld long double const int maxmn=800+10; const int dx[]={0,0,1,-1},dy[]={1,-1,0,0}; struct point { int x,y; }; int T,n,m,t,tot,len; char c[maxmn][maxmn]; bool human_at[maxmn][maxmn][2]; vector<point>ghost; queue<point>q1,q2; int dis(point a,point b) { return abs(a.x-b.x)+abs(a.y-b.y); } bool check_in_map(int x,int y,int gender) { if (c[x][y]!='X'&&x>=1&&x<=n&&y>=1&&y<=m&&(!human_at[x][y][gender])) return true;else return false; } bool check_ghost(point a) { if (dis(a,ghost[0])<=2*t||dis(a,ghost[1])<=2*t) return false;return true; } void bfs() { while (q1.size()&&q2.size()) { t++; tot=3; while (tot--) { len=q1.size(); while (len--) { point u=q1.front(); q1.pop(); if (!check_ghost(u)) continue; for (int i=0;i<4;i++) { int x_=u.x+dx[i],y_=u.y+dy[i]; if (check_in_map(x_,y_,0)&&check_ghost((point){x_,y_})) { human_at[x_][y_][0]=1; if (human_at[x_][y_][1]==1) { cout<<t<<"\n"; return; } q1.push((point){x_,y_}); } } } } tot=1; while (tot--) { len=q2.size(); while (len--) { point u=q2.front(); q2.pop(); if (!check_ghost(u)) continue; for (int i=0;i<4;i++) { int x_=u.x+dx[i],y_=u.y+dy[i]; if (check_in_map(x_,y_,1)&&check_ghost((point){x_,y_})) { human_at[x_][y_][1]=1; if (human_at[x_][y_][0]==1) { cout<<t<<"\n"; return; } q2.push((point){x_,y_}); } } } } } cout<<"-1\n"; return; } int main() { cin>>T; while (T--) { memset(human_at,0,sizeof(human_at)); t=0; while (q1.size()) q1.pop(); while (q2.size()) q2.pop(); ghost.clear(); cin>>n>>m; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cin>>c[i][j]; if (c[i][j]=='M') { q1.push((point){i,j}); human_at[i][j][0]=true; } if (c[i][j]=='G') { q2.push((point){i,j}); human_at[i][j][1]=true; } if (c[i][j]=='Z') { ghost.push_back((point){i,j}); } } } bfs(); } return 0; }
- 1
資訊
- ID
- 16
- 時間
- 1000ms
- 記憶體
- 256MiB
- 難度
- 4
- 標籤
- 遞交數
- 10
- 已透過
- 5
- 上傳者