首页 > 专题系列 > Java解POJ > POJ 2612 Mine Sweeper [解题报告] Java
2013
11-11

POJ 2612 Mine Sweeper [解题报告] Java

Mine Sweeper

问题描述 :

The game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with a mine is touched, the mine explodes and the player loses. If a positon not containing a mine is touched, an integer between 0 and 8 appears denoting the number of adjacent or diagonally adjacent grid positions that contain a mine. A sequence of moves in a partially played game is illustrated below.



Here, n is 8, m is 10, blank squares represent the integer 0, raised squares represent unplayed positions, and the figures resembling asterisks represent mines. The leftmost image represents the partially played game. From the first image to the second, the player has played two moves, each time choosing a safe grid position. From the second image to the third, the player is not so lucky; he chooses a position with a mine and therefore loses. The player wins if he continues to make safe moves until only m unplayed positions remain; these must necessarily contain the mines.

Your job is to read the information for a partially played game and to print the corresponding board.

输入:

The first line of input contains a single postitive integer n <= 10. The next n lines represent the positions of the mines. Each line represents the contents of a row using n characters: a period indicates an unmined positon while an asterisk indicates a mined position. The next n lines are each n characters long: touched positions are denoted by an x, and untouched positions by a period. The sample input corresponds to the middle figure above.

输出:

Your output should represent the board, with each position filled in appropriately. Positions that have been touched and do not contain a mine should contain an integer between 0 and 8. If a mine has been touched, all positions with a mine should contain an asterisk. All other positions should contain a period.

样例输入:

8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...

样例输出:

001.....
0013....
0001....
00011...
00001...
00123...
001.....
00123...

解题代码:

import java.io.PrintWriter;   
import java.util.Scanner;   
  
public class Main {   
    static char[][] table,touchTable,showTable;   
    static int[][] counts;   
    static int[] X = {0,-1,-1,-1,0,1,1,1};   
    static int[] Y = {-1,-1,0,1,1,1,0,-1};   
    public static void main(String[] args) {   
        Scanner scn = new Scanner(System.in);   
        //Scanner scn = new Scanner(Main.class.getResourceAsStream("in.dat"));   
        PrintWriter out = new PrintWriter(System.out);   
        int n;   
        n = scn.nextInt();   
        table = new char[n][n];   
        touchTable = new char[n][n];   
        showTable = new char[n][n];   
        counts = new int[n][n];   
        boolean touched = false;   
        for(int i = 0; i < n; i++){   
            table[i] = scn.next().toCharArray();   
        }   
        for(int i = 0; i < n; i++){   
            touchTable[i] = scn.next().toCharArray();   
        }   
        int xi = 0 ,yj = 0;   
        for(int i = 0; i < n; i++){   
            for(int j = 0; j < n; j++){   
                if(table[i][j] == '*'){   
                    for(int k = 0; k < 8; k ++){   
                        xi = i + X[k];   
                        yj = j + Y[k];   
                        if((xi < n && xi >= 0) && (yj < n && yj >= 0) && table[xi][yj] != 'x'){   
                            counts[xi][yj]++;   
                        }   
                    }   
                       
                }   
            }   
        }   
        for(int i = 0; i < n; i++){   
            for(int j = 0 ; j < n; j++){   
                showTable[i][j] = '.';   
                if(touchTable[i][j] == 'x' && table[i][j] == '.'){//娌℃�����风����   
                    showTable[i][j] = (char)(counts[i][j] + 48);    
                }else if(touchTable[i][j] == 'x' && table[i][j] == '*'){   
                    touched = true;   
                }   
            }   
        }   
        if(touched){   
            for(int i = 0 ; i < n; i++){   
                for(int j = 0; j < n; j++){   
                    if(table[i][j] == '*'){   
                        showTable[i][j] = '*';   
                    }   
                }   
            }   
        }   
        for(int i = 0; i < n; i++){   
            for(int j = 0; j < n; j ++){   
                out.print(showTable[i][j]);   
            }   
            out.println();   
        }   
        out.flush();   
    }   
}

  1. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  2. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!