lanqiao OJ 149 长草

简介: lanqiao OJ 149 长草

1.长草 - 蓝桥云课 (lanqiao.cn)

这是bfs的简单应用,经过k次繁衍,也就是说我们要进行k次bfs ,每一次先遍历一遍图,把是草的字符位置找出来,压入队列中,然后进行bfs,处理这几个草,繁衍完后进行下一次bfs ,直到k==0 ;

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std ;
 
const int N = 1010 ;
struct node{//记录点
  int x , y ;
  node(int xx,int yy){
    x = xx , y = yy ;
  } 
};
int d[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
char mp[N][N] ;//记录图
queue<node> q ;
int n , m , k ;
int main(){
  cin >> n >> m ;
  for(int i = 0 ; i < n ; i ++) cin >> mp[i] ;
  cin >> k ;
  while(k --){
           //遍历图,找出草的位置压入队列
    for(int i = 0; i < n ; i ++){
      for(int j= 0 ; j < m ; j ++){
        if(mp[i][j] == 'g') q.push(node(i,j));
      }
    }
    //进行一次bfs把草都处理一遍
    while(!q.empty()){
      node now = q.front() ;
      q.pop() ;
      int tx = now.x , ty = now.y ;
      for(int i = 0 ; i < 4 ; i ++) {
        int dx = tx + d[i][0] , dy = ty + d[i][1] ;
        if(dx<0||dx>=n||dy<0||dy>=m) continue ;
        mp[dx][dy] = 'g' ; 
      }
    }
    
  }
  for(int i = 0 ; i < n ; i ++) cout << mp[i] << endl ;
  return 0 ;
}
目录
相关文章
|
3天前
lanqiao OJ 1030 蓝肽子序列
lanqiao OJ 1030 蓝肽子序列
13 2
|
3天前
lanqiao OJ 1388 寒假作业
lanqiao OJ 1388 寒假作业
15 0
|
3天前
lanqiao OJ 689 四阶幻方
lanqiao OJ 689 四阶幻方
10 0
|
3天前
lanqiao OJ 389 摆花
lanqiao OJ 389 摆花
8 2
|
3天前
lanqiao OJ 364 跳石头
lanqiao OJ 364 跳石头
18 6
|
3天前
lanqiao OJ 649 算式900
lanqiao OJ 649 算式900
10 1
|
3天前
lanqiao OJ 108 发现环
lanqiao OJ 108 发现环
8 1
|
3天前
lanqiao OJ 525 传球游戏
lanqiao OJ 525 传球游戏
11 2
|
1天前
lanqiao oj 1085 小猪存钱罐
lanqiao oj 1085 小猪存钱罐
6 0
|
1天前
lanqiao oj 1050 补给
lanqiao oj 1050 补给
8 0