Thursday, April 24, 2014

Minesweeper Master (Google Code Jam Qualification Round: Problem C) : My attempt

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...**
***....***
**********
**********
**********




So here's the code that I wrote: (Algorithm applied: http://stackoverflow.com/questions/23039471/minesweeper-master-from-google-code-jam2014-qualification-round )

Algorithm

Let's first define N, the number of non-mine cells:
N = R * C - M
A simple solution is to fill an area of N non-mine cells line-by-line from top to bottom. Example for R=5C=5M=12:
c....
.....
...**
*****
*****
That is:
  • Always start in the top-left corner.
  • Fill N / C rows with non-mines from top to bottom.
  • Fill the next line with N % C non-mines from left to right.
  • Fill the rest with mines.
There are only a few special cases you have to care about.

Single non-mine

If N=1, any configuration is a correct solution.

Single row or single column

If R=1, simply fill in the N non-mines from left-to-right. If C=1, fill N rows with a (single) non-mine.

Too few non-mines

If N is even, it must be >= 4.
If N is odd, it must be >= 9. Also, R and C must be >= 3.
Otherwise there's no solution.

Can't fill first two rows

If N is even and you can't fill at least two rows with non-mines, then fill the first two rows with N / 2non-mines.
If N is odd and you can't fill at least two rows with non-mines and a third row with 3 non-mines, then fill the first two rows with (N - 3) / 2 non-mines and the third row with 3 non-mines.

Single non-mine in the last row

If N % C = 1, move the final non-mine from the last full row to the next row.
Example for R=5C=5M=9:
c....
.....
....*
..***
*****

Here's the code that I wrote in C++:
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream input;
ofstream output;
int **board;
int index=0;

void display(int r, int c)
{
 cout<<"Case #"<<index+1<<": \n";
 output<<"Case #"<<index+1<<": \n";
 for(int i=0;i<r;++i)
  {
   for(int j=0;j<c;++j)
   {
    if(board[i][j]==0)
    {
     cout<<"c";
     output<<"c";
    }
    if(board[i][j]==1)
    {
     cout<<"*";
     output<<"*";
    }
    if(board[i][j]==2)
    {
     cout<<".";
     output<<".";
    }
   }
   output<<endl;
   cout<<endl;
  }
}

int main()
{
 input.open("d:\\C-large-practice.in");
 output.open("d:\\out.txt");

 int nCases=0;
 input>>nCases;

 for(;index<nCases;++index)
 {
  output.flush();
  int r, c, m, n;
  input>>r>>c>>m;

  n = r*c-m;

  board = new int *[r];
  for(int j=0;j<r;++j)
  {
   board[j] = new int[c];
  }

  if(n==1)
  {
   //output<<"Case 1\n";
   for(int i = 0;i<r;++i)
   {
    for(int j=0;j<c;++j)
    {
     board[i][j]=1; //mine
    }
   }
   board[0][0]=0; //click
   display(r, c);
   continue;
  }

  if(r==1)
  {
   //output<<"Case 2\n";
   int tNM = n; //temporary non mine counter
   for(int i =0;i<c;++i)
   {
    if(tNM==0)
    {
     board[0][i]=1; //mine
    }
    else
    {
    board[0][i]=2;
    tNM--;    //non-mine
    }
   }
   board[0][0]=0;   //click
   display(r, c);
   continue;
  }
  if(c==1)
  {
   //output<<"Case 3\n";
   int tNM = n; //temporary non mine counter
   for(int i =0;i<r;++i)
   {
    if(tNM==0)
    {
     board[i][0]=1; //mine
    }
    else
    {
    board[i][0]=2;
    tNM--;    //non-mine
    }
   }
   board[0][0]=0;   //click
   display(r, c);
   continue;
  }

  if((n%2==0 && n<4) || (n%2==1 && ((r<3||c<3)||n<9)))
  {
   cout<<"Case #"<<index+1<<": \nImpossible\n";
   output<<"Case #"<<index+1<<": \nImpossible\n";
   output.flush();
   continue;
  }

  if(n%2==0 && ((n/c)<2))
  {
   //output<<"Case 4\n";
   for(int i = 0;i<2;++i)
   {
    for(int j =0; j<n/2;++j)
    {
     board[i][j]=2;
    }
   }

   //rest are mines
   for(int i=0;i<r;++i)
   {
    for(int j =0;j<c;++j)
    {
     if(board[i][j]!=2)
      board[i][j]=1;
    }
   }
   board[0][0]=0; //click
   display(r, c);
   continue;
  }

  if(n%2==1 && ((n/c<2) ||((n-2*c)<3)))
  {
   //output<<"Case 5\n";
   int tNM = n;
   for(int i = 0;i<2;++i)
   {
    for(int j =0; j<(n-3)/2;++j)
    {
     board[i][j]=2;
    }
   }

   board[2][0] = 2;
   board[2][1] = 2;
   board[2][2] = 2; //the three non mines

   //rest are mines
   for(int i=0;i<r;++i)
   {
    for(int j =0;j<c;++j)
    {
     if(board[i][j]!=2)
      board[i][j]=1;
    }
   }
   board[0][0]=0; //click
   display(r, c);
   continue;
  }

  //else the last case:
  int tNM = n;
  for(int i =0;i<r;++i)
  {
   //output<<"Case 6\n";
   for(int j =0;j<c;++j)
   {
    if(tNM!=0)
    {
    board[i][j]=2;
    tNM--;
    }
    else
     board[i][j]=1;
   }
  }
   if(n%c==1)
   {
    board[n/c-1][c-1] = 1;
    board[n/c][1] = 2;
   }
   board[0][0] = 0; 
  display(r, c);
 }
}
It looks a lot like "write-only" code but it does work.

No comments:

Post a Comment