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, T. Ttest 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.
思路:简单地说就是给出一个扫雷地图,算出最少点击次数。使得全部非地雷区被訪问过。须要一个数组存每一个位置周围地雷数目。点击到地雷数为0的时候将自己主动訪问呢周围八个区域,并且会递归訪问0的区域。
所以解题方法事实上就仅仅要是须要深搜。先将地雷数为0的訪问,然后深搜訪问。到最后再訪问那些非地雷非0的区域,就能实现最少点击。
代码:
/*2014-10-16 for google graduates online test *author:zhuangweibiao */ #include<iostream> #include<fstream> #include<string> using namespace std; string str[302];//存扫雷矩阵 int num[302][302]; bool visit[302][302]; void dfs(int i, int j, int n) { visit[i][j] = true; for(int ii = i - 1; ii <= i + 1; ii++) for(int jj = j - 1; jj <= j + 1; jj++) { if(ii >= 0 && ii < n && jj >= 0 && jj < n && str[ii][jj] != '*' && !visit[ii][jj]) { if(num[ii][jj] != 0) visit[ii][jj] = true; else dfs(ii, jj, n); } } } int main() { ifstream ifile("A-large-practice (1).in"); ofstream ofile("a2.txt"); int T; ifile >> T; for(int i = 1; i <= T; i++) { int n; ifile >> n; for(int j = 0; j < n; j++) ifile >> str[j]; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { if(str[i][j] != '*') { int count = 0; for(int ii = i - 1; ii <= i + 1; ii++) for(int jj =j - 1; jj <= j + 1; jj++) { if(ii >= 0 && ii < n && jj >= 0 && jj < n && str[ii][jj] == '*') count++; } num[i][j] = count; } else num[i][j] = -1; visit[i][j] = false; } int count = 0; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { if(num[i][j] == 0 && !visit[i][j]) { dfs(i, j, n); count++; } } for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { if(str[i][j] != '*' && !visit[i][j]) count++; } ofile << "Case #" << i << ": " << count << endl; } return 0; }