【PTA】Minesweeper Game

Please add image description
Personal business card:

blogger: Alcoholics ᝰ.
Personal profile: Indulge in wine, and strive for a future with a drink.
column: PTA exercises and analysis
introduce: Recorded the blogger's practice questions in pta

Please add image description

foreword

1 Introduction

"PTA programming experimental auxiliary teaching platform" is an auxiliary teaching platform for programming courses led by Zhejiang University, managed and operated by Hangzhou Baiteng Education Technology Co., Ltd., and jointly constructed by teachers in colleges and universities across the country. At present, 538 colleges and universities across the country have participated, and 3,152 teachers have jointly constructed 66,095 high-quality topics, and the number of registered students has reached 1.36 million; there are 14 fixed topic sets (covering C language, JAVA language, Python language, data structure, database system) and China University Computer Competition Question Bank) and 55 professional course topic sets (covering computer, electronics, literature, foreign language and Huawei certification). There are 10 types of questions including true and false questions, fill-in-the-blank questions, multiple-choice questions, multiple-choice questions, program fill-in-the-blank questions, function questions, programming questions, subjective questions, multi-file programming questions and SQL programming questions.

2. Advantages

  • Support 200,000 people online at the same time
  • Support multiple question types
  • Support a variety of judgment modes
  • Provide comprehensive guarantee for teaching quality
  • Instant question answering system

1. The topic

Friends who have played minesweeper know that the goal of the game is to find all the mines in an nm matrix. In this question, you need to count the number of mines around each cell, and each cell has the most There are 8 adjacent cells, as shown in the figure below, in the 44 cells, "*" means mine, and "^" means no mine.
insert image description here

The calculated output is:

*100

2210

1*10

1110

Input format:
The input contains several matrices. For each matrix, the first row contains two numbers M and N, which represent the number of rows and columns of the matrix respectively (0<N, M<100), and the next N rows contain M characters, that is, the matrix, with "*" for mines and "^" for blanks. When N=M=0, it means the end, and the line is not processed.

Output format:
For each matrix, first print the matrix serial number on a separate line: Field #X: where X is the number of the matrix, numbered from 1, and in the next N lines, the "^" read in is used around the position. Instead of the number of mines, the place of mines is still represented by "*". There is a blank line between the output of two adjacent matrices.

Sample Input:
Here is a set of inputs. E.g:
insert image description here

Sample output:
The corresponding output is given here. E.g:

Field #1:
100
2210
1
10
1110

Field #2:
324310
420
2
5641
23
6**1
2
3431
334531
23
5**1
1
33**31

Code Length Limit 16 KB
Time Limit 400 ms
Memory Limit 64 MB

2. Code

import java.util.Scanner;


public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int number = 0;
        int M = 1;
        int N = 1;
        while (M != 0 && N != 0){
    
    
            M = sc.nextInt();
            N = sc.nextInt();
            int[][] num = new int[M][N];
            String str;
            int sum = 0;
            for (int i = 0; i < M; i++) {
    
    
                str = sc.next();
                for (int j = 0; j < N; j++) {
    
    
                    num[i][j] = str.charAt(j);
                }
            }
            number++;
            if (M != 0 && N != 0 && number == 1){
    
    
                System.out.println("Field #" + number + ":");
            }else if (M != 0 && N != 0 && number != 1){
    
    
                System.out.println();
                System.out.println();
                System.out.println("Field #" + number + ":");
            }
            for (int i = 0; i < M; i++) {
    
    
                for (int j = 0; j < N; j++) {
    
    
                    if (num[i][j] == '*'){
    
    
                        System.out.print("*");
                    }else {
    
    
                        for (int k = i-1; k <= i+1; k++) {
    
    
                            for (int l = j-1; l <= j+1; l++) {
    
    
                                if (k >= M || l >= N || k < 0 || l < 0){
    
    
                                }else if (num[k][l] == '*'){
    
    
                                    sum++;
                                }
                            }
                        }
                        System.out.print(sum);
                        sum = 0;
                    }
                }
                if (i != M-1){
    
    
                    System.out.println();
                }
            }
        }
        System.out.println();
        System.out.println();
    }
}

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/127115353