1

输入

输入将由任意数量的字段组成。每个字段的第一行包含两个整数 n 和 m (0 < n,m <= 100),分别代表字段的行数和列数。接下来的 n 行正好包含 m 个字符并代表该字段。每个安全方块都用一个“.”表示。字符(不带引号),每个地雷方块由“*”字符表示(也不带引号)。n = m = 0 的第一条场线表示输入结束,不应处理。

输出

对于每个字段,您必须单独在一行中打印以下消息:

字段 #x:其中 x 代表字段的编号(从 1 开始)。接下来的 n 行应包含带有“.”的字段。字符替换为该方格的相邻地雷数。字段输出之间必须有一个空行。

样本输入

4 4

*...

....

.*..

....

3 5

**...

.....

.*...

0 0

样本输出

Field #1:

*100

2210

1*10

1110


Field #2:

**100

33200

1*100

我得到给定输入的正确输出。

import java.util.*;

public class Main{

    public static void main(String[] args) {
        //field size
        int n, m;
        //string to hold a row of a minesweeper board
        String line;
        //array to hold all the minesweeper boards entered
        ArrayList<char[][]> allBoards = new ArrayList<char[][]>();

        Scanner scan = new Scanner(System.in);

        //get the field size
        n = scan.nextInt();
        m = scan.nextInt();

        //keep going until n=0 and m=0
        while ((n+m)!=0) {

            //create the minesweeper board
            //the field sizes are 2 spaces bigger to prevent error checking 
            //at the edges of the minesweeper board            
            char[][] board = new char[n + 2][m + 2];

            //fill the appropriate spaces with the mines '*' and blank spaces '.'
            for (int row = 1; row < board.length - 1; row++) {
                line = scan.next();
                for (int col = 1; col < board[0].length - 1; col++) {
                    board[row][col] = line.charAt(col - 1);
                }
            }

            //add the current minesweeper board to the array
            allBoards.add(board);

            //get new field size
            n = scan.nextInt();
            m = scan.nextInt();
        }

        printResults(allBoards);
    }

    //function to find out how many mines are around a certain position
    //check all positions surrounding the current one.
    public static int getMines(char[][] board, int row, int col) {
        int nMines = 0;

        if (board[row - 1][col - 1] == '*') {
            nMines++;
        }

        if (board[row - 1][col] == '*') {
            nMines++;
        }

        if (board[row - 1][col + 1] == '*') {
            nMines++;
        }

        if (board[row][col - 1] == '*') {
            nMines++;
        }

        if (board[row][col + 1] == '*') {
            nMines++;
        }

        if (board[row + 1][col - 1] == '*') {
            nMines++;
        }

        if (board[row + 1][col] == '*') {
            nMines++;
        }

        if (board[row + 1][col + 1] == '*') {
            nMines++;
        }

        return nMines;
    }

    //print the results
    private static void printResults(ArrayList<char[][]> allBoards) {
        for (int i = 1; i <= allBoards.size(); i++) {
            System.out.println("Field #" + i + ":");
            for (int row = 1; row < allBoards.get(i - 1).length - 1; row++) {
                for (int col = 1; col < allBoards.get(i - 1)[0].length - 1; col++) {
                    if (allBoards.get(i - 1)[row][col] != '*') {
                        System.out.print(getMines(allBoards.get(i - 1), row, col));
                    } else {
                        System.out.print("*");
                    }
                }
                System.out.println();
            }
            System.out.println();
        }
    }
}
4

0 回答 0