Google2015校招在线测试题1----扫雷最少点击次数

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;
}
时间: 2024-10-07 08:31:57

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

google校招在线测试题---2048

先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream> #include<string> using namespace std; int main() { int T; ifstream ifile("B-large-practice.in"); ofstream ofile("out1.txt"); i

JavaWeb-Servlet技术的监听器-解析与实例-网站在线用户信息与网页点击量

转载请注明出处: http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 在Web项目中,我们对下面这几个监听器必须熟练的使用,它们的作用真的很大.熟练的使用后,可以使我们少绕弯路,少写很多代码. 事件源 监听对像的创建和销毁 监听对像上属性的添加和删除 HttpSession HttpSessionListener HttpSessionAttributeListener - HttpSessionEvent ServletRequest Servl

去哪儿网2017校招在线笔试(前端工程师)编程题及JavaScript代码

编程题很简单.整个试卷结构为: 一.问答题: 对前端的理解,了解哪些框架库? 二.在线编程题:身份证分组 如下第一道:身份证分组 三.在线编程题:身份证分组.统计字符.酒店价格(三选二) 如下第二三四道题中三选二作答. 我也搞不懂为什么有两部分编程题~~~下面详细说一说编程题目及JS代码实现: 第一道:身份证分组 代码: 1 var line; 2 while(line = read_line()){ 3 while(line.indexOf(" ") != -1){ 4 line =

2018 校招在线编程 20题-01

1. 最大乘积(拼多多) 输入 3 4 1 2 输出 24 解题思路: 定义五个数,一个最大,一个次大,一个第三大,一个最小,一个次小.只要找到这五个数,问题就解决了.因为最大乘积只可能是最大*(次大*第三大) 或者是 最大*(最小*次小).时间复杂度O(n),空间复杂度O(1).PS:这道题输入有问题,题目给的样例是直接给了一组数,而此时用例先给了一个数的个数n,然后再给了一组数. 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 in

indeed2017校招在线编程题(网测)三

A. Calculate Sequence 分析:就是斐波那契递推公式,但是初始值是指定的,只用求第10个数,数据范围和复杂度都比较小,直接写. B. 忘了叫啥了. 就是有a-j十个字符组成的字符串,要求去掉一些字符,使得剩下的长度大于k,且这些满足要求里面的字符串字典序最小的.然后需要枚举删除的字符串,2^10 = 1024,字符长度最大值100,所以复杂度为1024*100,在1s的时限内完全可以,直接编写. C. Anagram Multiple Number 这题好像更简单哎,求一个数的

阿里校招在线笔试题。。。感觉被虐了

1.按照CommonJS规范,在任何模块代码的作用域下内置了以下哪些变量? module context require exports 2.一下方法中哪些存在兼容性问题? spilt indexof trim Date.parse 3.下面哪些技术可用于优化 CSS 图片加载 ? ·       CSSSprite ·       SVGSprite ·       Iconfont ·       Base64 4.有一道选择题问哪些是html5的新特性. 5.从前端工程师的角度如何提高页面

阿里巴巴集团2015年秋季校招在线笔试附加题分析

刚做完,选择题做吐血,好多智力题....附加题有两道编程题.题面是回忆的内容. 1.在text中查找子串quary,返回符合匹配的quary中连续的最大的子串长度,例如 quary = "acbac",text = "acaccbabb",quary 中 "cba"是最大的连续子串,返回3. [分析] 两重循环获得quary的所有连续子串,使用KMP算法在text 查找匹配,如果匹配,则记录子串长度,最后返回最大的子串长度. 代码如下,编译通过

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

题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了.就是mix = ceil(x0-r)//因为是小的值,所以要向上取整.mxx=floor(x0+r)//大的值要向下取整 对于y.我们也能用欧股定理确定.y也是有一个范围.但是并不是所有y都要枚举的.明显.y的值是离圆心越远越好.所以对于每一个x而言只需要枚举最远的两个y值 #include <cs

华为在线测试题[基础篇]--判断IP是否合法

现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字). 现在需要你用程序来判断IP是否合法. //判断是否为合法IP #include<iostream> #include<string> #include<vector> using namespace std;