蓝桥云课-长草
https://www.lanqiao.cn/problems/149/learning/
解题思路
将初始草的位置入队列,在把整张图全部长完,只将满足月份的草输出
#include <bits/stdc++.h>
using namespace std;
char bk[1010][1010];
int b[1010][1010];
struct Node{
int x,y;
};
queue <Node> q;
int main()
{ int n,m,k;
int fx[4][4]={{0,1},{0,-1},{1,0},{-1,0}};
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>bk[i][j];
if(bk[i][j]=='g'){
//将草入队列
q.push({i,j});
b[i][j]=0;
}
}
}
cin>>k;
while(q.size()){
//全部长完草
Node u = q.front();
q.pop();
for(int i=0;i<4;i++){
int x1,y1;
x1=u.x+fx[i][0];
y1=u.y+fx[i][1];
if(x1<1||y1<1||x1>n||y1>m||bk[x1][y1]=='g'||b[x1][y1]>=k-1)
continue;
bk[x1][y1]='g';
b[x1][y1]=b[u.x][u.y]+1;
q.push({x1,y1});
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
//输出满足月份的草
if(b[i][j]<=k)
cout<<"g";
else
cout<<".";
}
cout<<endl;
}
return 0;
}