扫雷实施输出似乎没问题,但UVa Judge测试失败了

时间:2013-04-13 21:43:45

标签: java

输入

输入将包含任意数量的字段。每个字段的第一行包含两个整数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();
        }
    }
}

0 个答案:

没有答案