Blue Bridge Cup - school simulation game (grass problem)

2020 Blue Bridge Cup - school simulation game

topic:

Xiaoming piece of land, he will divide this space into small n rows and m columns, each row and each column length is 1.
Xiao Ming chose some of them small clearing, planted grass and other small pieces remain open space.
The grass grows very fast, every month, some of the grass will grow out, if a small piece of grass planted, it will be to own up, down, left and right four small open space expansion, the four small clearing It will become a small piece of grass.
Please tell Xiao Ming, k months later what place there is grass on the open space.

Input format:
The first line of input contains two integers m, n.
Subsequently n rows, each row containing m characters, the open space represents an initial state, there is no spaces between letters. If a decimal point, is represented as open space, if the letters g, represents a kind of grass.
Next contains an integer k.

Output Format:
Output n rows, each row containing m characters, k represents a state space months. If a decimal point, is represented as open space, if the letter g represents the long grass.

Sample input:

April 5
.g ...
.....
... g.
.....
2

Sample output:

gggg.
ggggg
.gggg
..ggg

Ideas:

Input in the form of type String, String type but not convenient to modify the operation, the new Char [] array is used to store all markers.

n months is the increase in n times the lawn, by n cycles, each cycle is to address each labeled g, surrounded by the specified address change marked g.

 

 1 import java.util.Scanner;
 2 
 3 public class Main{
 4     public static void main(String[] args) {
 5         Scanner scan = new Scanner(System.in);
 6         int n = scan.nextInt();
 7         int m = scan.nextInt();
 8         String[] place = new String[n+1];
 9         for(int i = 0; i< n; i++) {
10             place[i] = scan.next();    //输入地图
11         }
 12 is          int month The scan.nextInt = ();         // month entry 
13 is          
14          char [] [] = P new new  char [n-] [m];     // map into a two-dimensional array P [n-] [m]; 
15          for ( int I = 0; I <n-; I ++ ) {
 16              for ( int J = 0; J <m; J ++ ) {
 . 17                  P [I] [J] = Place [I] .charAt (J);
 18 is              }
 . 19          }
 20 is          char [] [] P2 = new new  char [n-] [m];     // temporary to P2 
21 is          for(int t = 0; t< month; t++) {
22         
23             for(int i = 0; i<n; i++) {
24                 for(int j = 0; j< m; j++) {
25                     if(p[i][j] == 'g') {
26                         p2[i][j] = 'g';
27                         if(i-1>=0) p2[i-1][j] = 'g';
28                         if(i+1<n) p2[i+1][j] = 'g';
29                         if(j-1>=0) p2[i][j-1] = 'g';
30                         if(j+1<m) p2[i][j+1] = 'g';
31                     }
32                 }
33             }
34             for(int i = 0; i<n; i++) {
35                 for(int j = 0; j< m; j++) {
36                     p[i][j]=p2[i][j];
37                 }
38             }
39         }
40         for(int i = 0; i<n; i++) {
41             for(int j = 0; j< m; j++) {
42                 if(p[i][j] == 'g')System.out.print(p[i][j]);
43                 else System.out.print(".");
44             }
45             System.out.println();
46         }
47         scan.close();
48     }
49 }

 

Guess you like

Origin www.cnblogs.com/polygon-live/p/12486868.html