Blue Bridge Cup: Long Grass (detailed analysis)

topic

[Problem description]
   Xiao Ming has an open space. He divides the open space into n rows and m columns. The length of each row and each column is 1.
   Xiao Ming selected some of the small plots and planted grass, while the other plots remained open.
   These grasses grow very fast. Every month, the grass will grow outwards. If a small piece of grass is planted, it will expand to four small spaces above, below, left and right, these four small spaces All will become small pieces with grass.
   Please tell Xiao Ming where there is grass on the open ground in k months.
[Input format]
   The first line of input contains two integers n, m.
   In the next n lines, each line contains m letters, indicating the initial state of open space, with no spaces between the letters. If it is a decimal point, it means that it is an open space. If the letter is g, it means that grass is planted.
   Next contains an integer k.
[Output format]
   Output n lines, each line contains m letters, indicating the state of the open space after k months. If it is a decimal point, it means that it is empty, if the letter is g, it means that there is grass.
[Sample input]
. 4. 5
. G...
.....
.. G..
.....
2
(typesetting, added the intermediate space, manually enter the title of the present sample input)

[Sample output]
   gggg.
   gggg.
   ggggg
   .ggg.
[ Measurement use case scale and conventions]
   For 30% of the measurement use cases, 2 <= n, m <= 20.
   For 70% of the evaluation cases, 2 <= n, m <= 100.
   For all evaluation cases, 2 <= n, m <= 1000, and 1 <= k <= 1000.

Make mistakes

 Index out of bounds

Solution

  To prevent the index out-of-bounds exception, arr[n][m] can be changed to arr[n+2][m+2] and initialized to '0'.

Difficulties

  How to correctly assign a value of grass to the surrounding open space

Solutions to major difficulties

  First mark the open space near the grass as '1', and then change the '1' to'g' after all markings. A

more detailed explanation is in the code comments

Code

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    //种草
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        char arr[][]=new char[n+2][m+2];//防止索引越界异常
        for(int i=0;i<n+2;i++){
    
    
            for(int j=0;j<m+2;j++){
    
    
                arr[i][j]='0'; //初始化为'0'
            }
        }
        String str[] = new String[n];
        for(int i=0;i<n;i++){
    
    
            str[i]=scanner.next(); //将值以字符串的形式输入
        }
        for(int i=1;i<n+1;i++){
    
    
            for(int j=1;j<m+1;j++){
    
    
                arr[i][j]=str[i-1].charAt(j-1);//将字符串中的值赋给数组
            }
        }
        int k = scanner.nextInt();
        for(int l=0;l<k;l++){
    
    
            for(int i=1;i<n+1;i++){
    
    
                for(int j=1;j<m+1;j++){
    
    
                    if(arr[i][j]=='g'){
    
    //将有草周边且非0的空地标为1
                        if(arr[i][j+1]!='0'&&arr[i][j+1]!='g'){
    
    
                            arr[i][j+1]='1';
                        }
                        if(arr[i][j-1]!='0'&&arr[i][j-1]!='g'){
    
    
                            arr[i][j-1]='1';
                        }
                        if(arr[i+1][j]!='0'&&arr[i+1][j]!='g'){
    
    
                            arr[i+1][j]='1';
                        }
                        if(arr[i-1][j]!='0'&&arr[i-1][j]!='g'){
    
    
                            arr[i-1][j]='1';
                        }
                    }
                }
            }
            for(int i=1;i<n+1;i++){
    
    //将空地变为有草地
                for(int j=1;j<m+1;j++){
    
    
                    if(arr[i][j]=='1'){
    
    
                        arr[i][j]='g';
                    }
                }
            }
        }

        for(int i=1;i<n+1;i++){
    
    //打印输出结果
            for(int j=1;j<m+1;j++){
    
    
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }

    }
}

Guess you like

Origin blog.csdn.net/qq_47168235/article/details/108910428