Google Code Jam 2014-Qualification Round-Problem C. Minesweeper Master

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. There is nothing quicker than winning in one click. Given the size of the board (R x C) and the number of hidden mines M, is it possible (however unlikely) to win in one click? You may choose where you click. If it is possible, then print any valid mine configuration and the coordinates of your click, following the specifications in the Output section. Otherwise, print "Impossible".

Input

The first line of the input gives the number of test cases, TT lines follow. Each line contains three space-separated integers: RC, and M.

Output

For each test case, output a line containing "Case #x:", where x is the test case number (starting from 1). On the following R lines, output the board configuration with C characters per line, using '.' to represent an empty cell, '*' to represent a cell that contains a mine, and 'c' to represent the clicked cell.

If there is no possible configuration, then instead of the grid, output a line with"Impossible" instead. If there are multiple possible configurations, output any one of them.

Limits

0 ≤ M < R * C.

Small dataset

1 ≤ T ≤ 230.
1 ≤ RC ≤ 5.

Large dataset

1 ≤ T ≤ 140.
1 ≤ RC ≤ 50.

Sample


Input 
 

Output 
 
5
5 5 23
3 1 1
2 2 1
4 7 3
10 10 82

Case #1:
Impossible
Case #2:
c
.
*
Case #3:
Impossible
Case #4:
......*
.c....*
.......
..*....
Case #5:
**********
**********
**********
****....**
***.....**
***.c...**
***....***
**********
**********
**********

題意:掃雷遊戲中,給出矩形大小R*C,以及雷數M,求是否存在一種擺法,使得一次點擊就贏,若存在,輸出任一種合理的擺法。(方格內數字表示周圍8個格的雷數,若點開方格內數字爲0,則向周圍八個格遞歸擴展)

分析:考慮了很多種擺雷的策略,最後終於找到一種正確的。計算空格數R*C-M,先把起始點放在左上角,這樣需要的周圍三個空格即可遞歸擴展,具體方法如下:

1、從大到小枚舉每一行的空格數,不大於上一行空格數,不能小於2,。(一行只有一個空格,這個空格無法被擴展到)

2、第二行空格數與第一行相同。(保證第一行最後一個空格不是凸出來的,否則無法被擴展到)

3、記錄已經用了的空格數,若其等於空格總數,找到一個合法擺法。

需特殊處理的情況:

1、全部是雷,R*C=M

2、只有一行或一列,R=1或C=1

3、只有一個空格,R*C=M+1

代碼:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;

const int N=60;
int r,c,m,sp;
int col[N];

bool check(int row,int pre,int now)
{
    if(sp==1)
    {
        col[0] = 1;
        return true;
    }
    if(row>=r)
    {
        return now==sp;
    }
    if(now>sp) return false;
    if(now==sp) return true;
    if(row==1)
    {
        col[row] = pre;
        return check(row+1,pre,now+pre);
    }
    for(int i=pre; i>=2; i--)
    {
        col[row] = i;
        if(check(row+1,i,now+i))
            return true;
        col[row] = 0;
    }
    return false;
}

int main()
{
	freopen("C-large.in","r",stdin);
	freopen("C-large.out","w",stdout);
	
	int t;
	scanf("%d",&t);
	for(int cnt=1;cnt<=t;cnt++)
	{
	    
	    scanf("%d%d%d",&r,&c,&m);
	    printf("Case #%d:\n",cnt);
	    if(m==r*c)
        {
            printf("Impossible\n");
            continue;
        }
        if(r==1 || c==1)
        {
            int ls = r*c-m;
            for(int i=0; i<r; i++)
            {
                for(int j=0; j<c; j++)
                {
                    if(ls>0)
                    {
                        if(i==0 && j==0) printf("c");
                        else printf(".");
                        ls--;
                    }
                    else printf("*");
                }
                printf("\n");
            }
            continue;
        }
        sp = r*c-m;
        memset(col,0,sizeof(col));
        if(check(0,min(sp/2,c),0))
        {
            for(int i=0; i<r; i++)
            {
                for(int j=0; j<c; j++)
                {
                    if(i==0 && j==0) printf("c");
                    else if(j<col[i]) printf(".");
                    else printf("*");
                }
                printf("\n");
            }
        }
        else
            printf("Impossible\n");
	} 
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章