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.

思路:简单地说就是给出一个扫雷地图,算出最少点击次数。使得全部非地雷区被訪问过。须要一个数组存每一个位置周围地雷数目。点击到地雷数为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;
}

Google2015校招在线測试题1----扫雷最少点击次数的更多相关文章

  1. google校招在线測试题---2048

    先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream&g ...

  2. 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题

    题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...

  3. (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)

    刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...

  4. 当当网-前端project师測试题

                                     前端project师測试题(笔试时间20分钟.面试时间20分钟)   一.笔试 1.基础问题 (1)前端页面有哪三层构成,各自是什么? ...

  5. jsfiddle在线測试Html、CSS、JavaScript——http://jsfiddle.net/

    jsfiddle在线測试Html.CSS.JavaScript,并展示測试结果 1.选择jQuery1.9.1 2.选择jQuery UI 1.9.2 3.Html <ul id="n ...

  6. 阿里腾讯校招Java面试题总结及答案

    阿里校招java面试题汇总 1.HashMap和HashTable的区别,及其实现原理. Hashtable继承自Dictionary类,而HashMap是Java1.2引进的,继承自Abstract ...

  7. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  8. 2015网易校招Java开发工程师(技术架构)在线笔试题

    1.  程序和进程的本质区别是? A.在外存和内存存储 B.非顺序和顺序执行机器指令 C.独占使用和分时使用计算机资源 D.静态和动态特征 参考答案分析: 进程与应用程序的区别: 进程(Process ...

  9. 2015年网易校招Java开发工程师(技术架构)在线笔试题

    1.  程序和进程的本质区别是? A.在外存和内存存储 B.非顺序和顺序执行机器指令 C.独占使用和分时使用计算机资源 D.静态和动态特征 参考答案分析: 进程与应用程序的区别: 进程(Process ...

随机推荐

  1. 如何让一个div里面的div垂直居中?

    如何让一个div里面的div垂直居中? 如何让上面灰色有文字那个div和背景图标垂直居中,不管屏幕大小有好大,始终在垂直方向上的中间.上面有整个布局和样式表,谢谢高手指点 CSS3时代当然要用CSS3 ...

  2. 模块挂载、切换,uml模式、流程图模式

    模块挂载.切换,uml模式.流程图模式

  3. cstring 转string

    (1)CString转换为string CString cs(_T("cs")); string s; s = (LPCSTR)(CStringA)(cs); (2)string转 ...

  4. 虚拟机找不到本机vmnet0,vmnet8,无法连接xshell,解决方案

    首先出现这个问题肯定是第一次下载虚拟机把之前的注册表覆盖了,网卡找不到,首先卸载VMware 并且将C\ProgramData下的VMware文件夹删除掉 ,下载cceaner,点击注册表清除干净,再 ...

  5. Windows下运行jekyll,编码已不再是问题

    很久没更新jekyll了,所以好奇着去官网看了下更新记录,发现如下更新条目(版本1.3.0/2013-11-04发布): Add encoding configuration option (#144 ...

  6. 打造完美的ImageLoader——LruCache+DiskLruCache

    做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...

  7. 题解 洛谷P1501/BZOJ2631【[国家集训队]Tree II】

    Link-Cut-Tree 的懒标记下传正确食用方法. 我们来逐步分析每一个操作. 1:+ u v c:将u到v的路径上的点的权值都加上自然数c; 解决方法: 很显然,我们可以 split(u,v) ...

  8. C/C++野指针

    野指针: 野指针不同于空指针,空指针是指一个指针的值为null,而野指针的值并不为null,野指针会指向一段实际的内存,只是它指向哪里我们并不知情,或者是它所指向的内存空间已经被释放,所以在实际使用的 ...

  9. Number Theory Problem(The 2016 ACM-ICPC Asia China-Final Contest 找规律)

    题目: Mr. Panda is one of the top specialists on number theory all over the world. Now Mr. Panda is in ...

  10. django中配置允许跨域请求

    对于django 安装django-cors-headers,详情请看官方文档 pip install django-cors-headers 配置settings.py文件 a.在INSTALLED ...