蓝桥杯校内模拟赛-I长草

I. 长草

问题描述

小明有一块空地,他将这块空地划分为 n 行 m 列的小块,每行和每列的长度都为 1。
  小明选了其中的一些小块空地,种上了草,其他小块仍然保持是空地。
  这些草长得很快,每个月,草都会向外长出一些,如果一个小块种了草,则它将向自己的上、下、左、右四小块空地扩展,这四小块空地都将变为有草的小块。
  请告诉小明,k 个月后空地上哪些地方有草。

输入格式
  输入的第一行包含两个整数 n, m。
  接下来 n 行,每行包含 m 个字母,表示初始的空地状态,字母之间没有空格。如果为小数点,表示为空地,如果字母为 g,表示种了草。
  接下来包含一个整数 k。

输出格式
  输出 n 行,每行包含 m 个字母,表示 k 个月后空地的状态。如果为小数点,表示为空地,如果字母为 g,表示长了草。

样例输入
4 5
.g…
.….
…g…
.….
2
样例输出
gggg.
gggg.
ggggg
.ggg.
评测用例规模与约定
对于 30% 的评测用例,2 <= n, m <= 20。
对于 70% 的评测用例,2 <= n, m <= 100。
对于所有评测用例,2 <= n, m <= 1000,1 <= k <= 1000。

知识点:bfs(广度优先搜索)

解题思路:这一题我可能没做出来(实现上有问题)但是这一题用bfs,起点是每个长草的位置g,然后结束条件是每个队首加一天,天数和输入天数相等则退出bfs,然后输出这副图就好了。

总结:bfs用队列实现,关键是我没找好结束条件,导致这一题出错。

这是我在网上看到的100分题解
转自CSDN博主「专科辣鸡在线丢人」
原文链接:https://blog.csdn.net/qq_42815590/article/details/103544729

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1004;
char a[maxn][maxn];

typedef struct
{
    int x;
    int y;
    int cnt;
} Node;

int main()
{
    int n, m;
    int k;
    cin>>n>>m;
    for(int i = 0; i < n; i++)
        cin>>a[i];
    cin>>k;

    queue<Node> q;
    Node temp;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            if(a[i][j] == 'g')
            {
                temp.x = i, temp.y = j, temp.cnt = 0;
                q.push(temp);
            }

    while(!q.empty())
    {
        Node mod = q.front();
        a[mod.x][mod.y] = 'g';
        q.pop();
        if(mod.cnt != k)
        {
            for(int x = -1; x <= 1; x++)
                for(int y = -1; y <= 1; y++)
                    if(!x || !y)
                    {
                        temp.x = mod.x + x, temp.y = mod.y + y, temp.cnt = mod.cnt + 1;
                        if(temp.x < 0 || temp.x >= n || temp.y < 0 || temp.y >= m)
                            continue;
                        q.push(temp);
                    }
        }
    }

    for(int i = 0; i <= n; i++)
        puts(a[i]);

    return 0;
}

运行结果:
在这里插入图片描述

我的代码(错误版):

#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
struct Node{
    int x;
    int y;
};
int n,m;
char mp[1010][1010];
int dx[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
void bfs(int x, int y, int day){
	int curx = x;
	int cury = y; 
    int cnt = 0;
    Node node;
    node.x = x;
    node.y = y;
    queue<Node>q;
    q.push(node);
    while(!q.empty()){
        
        node = q.front();
        if(abs(node.x-day)==curx||abs(node.y-day)==cury||(abs(node.x-day-1)==curx&&abs(node.x-day-1)==cury))break;
        q.pop();
        Node k;
        for(int i = 0; i < 4; i++){
            int xx = node.x+dx[i][0];
            int yy = node.y+dx[i][1];
            
            if(mp[xx][yy]=='.'&&xx>=0&&xx<n&&yy>=0&&yy<m){
                mp[xx][yy] = 'g';
                k.x=xx;
                k.y=yy;
                q.push(k);
            }
        }
        cnt++;
        cout<<cnt<<endl;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin>>mp[i][j];
        }
    }
    int day;
    cin>>day;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(mp[i][j]=='g')bfs(i,j,day);
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cout<<mp[i][j];
        }
        cout<<endl;
    }
    return 0;
}

身如逆流船,心比铁石坚。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值