Google 10月份在线笔试ProblemA(个人代码,未必最优,请不吝赐教)

Problem

Minesweeper is a computer game that became popular in the 1980s, and is still included in some versions of the Microsoft Windows operating system. This problem has a similar idea, but it does not assume you have played Minesweeper.

In this problem, you are playing a game on a grid of identical cells. The content of each cell is initially hidden. There are M mines hidden in M different cells of the grid. No other cells contain mines. You may click on any cell to reveal it. If the revealed cell contains a mine, then the game is over, and you lose. Otherwise, the revealed cell will contain a digit between 0 and 8, inclusive, which corresponds to the number of neighboring cells that contain mines. Two cells are neighbors if they share a corner or an edge. Additionally, if the revealed cell contains a 0, then all of the neighbors of the revealed cell are automatically revealed as well, recursively. When all the cells that don't contain mines have been revealed, the game ends, and you win.

For example, an initial configuration of the board may look like this ('*' denotes a mine, and 'c' is the first clicked cell):

*..*...**.
....*.....
..c..*....
........*.
..........

There are no mines adjacent to the clicked cell, so when it is revealed, it becomes a 0, and its 8 adjacent cells are revealed as well. This process continues, resulting in the following board:

*..*...**.
1112*.....
00012*....
00001111*.
00000001..

At this point, there are still un-revealed cells that do not contain mines (denoted by '.' characters), so the player has to click again in order to continue the game.

You want to win the game as quickly as possible. You want to find the minimum number of clicks to win the game. Given the size of the board (N x N), output such minimum number of clicks.

Input

The first line of the input gives the number of test cases, TTtest cases follow. First line of each test case contains one integer N. N lines strings with length N follows containing '*' and '.', denotes the Minesweeper initial board.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum number of clicks to win.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ N ≤ 50.

Large dataset

1 ≤ N ≤ 300.

Sample


Input 
 

Output 
 
2
3
..*
..*
**.
5
..*..
..*..
.*..*
.*...
.*...
Case #1: 2
Case #2: 8

解法:

本题不是很难,在所有题里也是通过率最高的一道。采用DFS可解,先对数据进行处理,计算出每个位置周围有多少个mine,然后遍历整个地图,看看是否有多少个需要整块点击的位置,搜索过的位置要记录,以免重复搜索。搜索某个位置的时候需要注意,要搜索其8个合法的方向,当搜索到周围mine的个数大于0的时候则不往下调用DFS。


具体代码如下,大小集合通用:


#include <iostream>

using namespace std;

#define MINEVAL 10
#define DIRNUM 8

char dataSrc[320][320];
int dataProce[320][320];
int dataState[320][320];

int dirX[DIRNUM] = {-1, -1, -1, 0, 0,  1, 1, 1};
int dirY[DIRNUM] = {-1,  0,  1,-1, 1, -1, 0, 1};

int nCurLines = 0;

int legalPos(int x, int y)
{
    return x >= 0 && x < nCurLines && y >= 0 && y < nCurLines;
}

int GetMineNum(int x, int y)
{
    int minenum = 0;
    for (int i = 0; i < DIRNUM; i++)
    {
        int xtmp = x + dirX[i];
        int ytmp = y + dirY[i];
        if (legalPos(xtmp, ytmp))
        {
            if (dataSrc[xtmp][ytmp] == '*')
            {
                minenum++;
            }
        }
    }
    return minenum;
}

void initState()
{
    for (int i = 0; i < nCurLines; i++)
    {
        for (int j = 0; j < nCurLines; j++)
        {
            dataState[i][j] = 0;
        }
    }
}

void ProcessData()
{
    initState();
    for (int i = 0; i < nCurLines; i++)
    {
        for (int j = 0; j < nCurLines; j++)
        {
            if (dataSrc[i][j] == '*')
            {
                dataProce[i][j] = MINEVAL;
                dataState[i][j] = 1;
            }else
            {
                dataProce[i][j] = GetMineNum(i, j);
            }
        }
    }
}

int shouldClick(int x, int y)
{
    int should = 0;
    if (dataState[x][y] == 0)
    {
        should = 1;
        dataState[x][y] = 1;
        for (int i = 0; i < DIRNUM; i++)
        {
            int xtmp = x + dirX[i];
            int ytmp = y + dirY[i];
            if (legalPos(xtmp, ytmp))
            {
                if (dataState[xtmp][ytmp] == 0)
                {
                    if (dataProce[xtmp][ytmp] > 0)
                    {
                        if (dataProce[x][y] == 0)
                        {
                            dataState[xtmp][ytmp] = 1;
                        }
                    }else
                    {
                        shouldClick(xtmp, ytmp);
                    }
                }
            }
        }
    }
    return should;
}

int minClick()
{
    int minclick = 0;
    ProcessData();
    for (int i = 0; i < nCurLines; i++)
    {
        for (int j = 0; j < nCurLines; j++)
        {
            if (shouldClick(i, j))
            {
                minclick++;
            }
        }
    }
    return minclick;
}

int main()
{
    int T, idx = 0;
    cin >> T;
    while(idx < T)
    {
        idx++;
        cin >> nCurLines;
        for(int i = 0; i < nCurLines; i++)
        {
            for(int j = 0; j < nCurLines; j++)
            {
                cin >> dataSrc[i][j];
            }
        }
        cout << "Case #" << idx << ": " << minClick() << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值