POJ2612 Mine Sweeper(ZOJ1862, UVA10279)【Ad Hoc+模拟】

Mine Sweeper

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7153   Accepted: 2769

Description

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. 

Input

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.

Output

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.

Sample Input

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

Sample Output

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

Source

Waterloo local 1999.10.02

问题链接POJ2612 Mine Sweeper

问题简述:(略)

问题分析

  这个题统计计算一下,再模拟一下扫雷过程就好了,不用解释了吧。

程序说明

  该问题与ZOJ1862和UVA10279是同一问题,只是输入输出格式略有不同,相应的AC程序一并给出。

题记:(略)

参考链接:(略)

AC的C++语言程序(POJ)如下:

/* POJ2612 Mine Sweeper */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int drow[]={-1, 0, 1, -1, 1, -1, 0, 1};
int dcol[]={1, 1, 1, 0, 0, -1, -1, -1};
const int D = sizeof(drow) / sizeof(int);

const int N = 10;
char mine[N][N+1], touch[N][N + 1];
int cnt[N][N];

int main()
{
    int n;
    while(~scanf("%d", &n)) {
        memset(cnt, 0, sizeof(cnt));

        // 输入地雷阵
        for(int i = 0; i < n; i++)
            scanf("%s", mine[i]);
        // 输入触发阵
        for(int i = 0; i < n; i++)
            scanf("%s", touch[i]);

        int bflag = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++) {
                if(mine[i][j] == '*' && touch[i][j] == 'x')
                    bflag = 1;
                for(int k = 0; k < D; k++) {
                    int nextrow = i + drow[k];
                    int nextcol = j + dcol[k];
                    if(0 <= nextrow && nextrow < n && 0 <= nextcol && nextcol < n)
                        if(mine[nextrow][nextcol] == '*')
                            cnt[i][j]++;
                }
            }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++) {
                if(bflag == 1 && mine[i][j] == '*')
                    touch[i][j]='*';
                else if(touch[i][j] == 'x')
                    touch[i][j] = cnt[i][j] + '0';
            }

        // 输出结果
        for(int i = 0; i < n; i++)
            puts(touch[i]);
    }

    return 0;
}

AC的C++语言程序(ZOJ1862)如下:

/* ZOJ1862 Mine Sweeper */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int drow[]={-1, 0, 1, -1, 1, -1, 0, 1};
int dcol[]={1, 1, 1, 0, 0, -1, -1, -1};
const int D = sizeof(drow) / sizeof(int);

const int N = 10;
char mine[N][N+1], touch[N][N + 1];
int cnt[N][N];

int main()
{
    int n, caseno = 0;
    while(~scanf("%d", &n)) {
        memset(cnt, 0, sizeof(cnt));

        // 输入地雷阵
        for(int i = 0; i < n; i++)
            scanf("%s", mine[i]);
        // 输入触发阵
        for(int i = 0; i < n; i++)
            scanf("%s", touch[i]);

        int bflag = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++) {
                if(mine[i][j] == '*' && touch[i][j] == 'x')
                    bflag = 1;
                for(int k = 0; k < D; k++) {
                    int nextrow = i + drow[k];
                    int nextcol = j + dcol[k];
                    if(0 <= nextrow && nextrow < n && 0 <= nextcol && nextcol < n)
                        if(mine[nextrow][nextcol] == '*')
                            cnt[i][j]++;
                }
            }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++) {
                if(bflag == 1 && mine[i][j] == '*')
                    touch[i][j]='*';
                else if(touch[i][j] == 'x')
                    touch[i][j] = cnt[i][j] + '0';
            }

        // 输出结果
        if(caseno++)
            putchar('\n');
        for(int i = 0; i < n; i++)
            puts(touch[i]);
    }

    return 0;
}

AC的C++语言程序(UVA10279)如下:

/* UVA10279 Mine Sweeper */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int drow[]={-1, 0, 1, -1, 1, -1, 0, 1};
int dcol[]={1, 1, 1, 0, 0, -1, -1, -1};
const int D = sizeof(drow) / sizeof(int);

const int N = 10;
char mine[N][N+1], touch[N][N + 1];
int cnt[N][N];

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        memset(cnt, 0, sizeof(cnt));

        // 输入地雷阵
        for(int i = 0; i < n; i++)
            scanf("%s", mine[i]);
        // 输入触发阵
        for(int i = 0; i < n; i++)
            scanf("%s", touch[i]);

        int bflag = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++) {
                if(mine[i][j] == '*' && touch[i][j] == 'x')
                    bflag = 1;
                for(int k = 0; k < D; k++) {
                    int nextrow = i + drow[k];
                    int nextcol = j + dcol[k];
                    if(0 <= nextrow && nextrow < n && 0 <= nextcol && nextcol < n)
                        if(mine[nextrow][nextcol] == '*')
                            cnt[i][j]++;
                }
            }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++) {
                if(bflag == 1 && mine[i][j] == '*')
                    touch[i][j]='*';
                else if(touch[i][j] == 'x')
                    touch[i][j] = cnt[i][j] + '0';
            }

        // 输出结果
        for(int i = 0; i < n; i++)
            puts(touch[i]);
        if(t)
            putchar('\n');
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81452068