Google2015 school recruitment Online Test Question 1 ---- minimum number of clicks for mine clearance

Source: Internet
Author: User
Problem

Minesweeper is a computer game that became popular in the 1980 s, and is still pinned 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 in a digit between 0 and 8, aggressive, which corresponds to the number of neighboring cells that contain in 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 in 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 ining '*' 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.

Idea: Simply put, a mine clearance map is provided to calculate the minimum number of clicks so that all non-mine zones have been visited. An array is required to store the number of mines around each location. When the number of mines is 0, the system will automatically access the eight surrounding regions and recursively access the 0 regions. Therefore, to solve the problem, you only need to perform a deep search. First, you need to access the area where the number of mines is 0, then perform a deep search, and then access the area where the number of non-mine nodes is not 0, you can achieve at least click.

Code:

/* 2014-10-16 for Google graduates online test * Author: zhuangweibo IAO */# include <iostream> # include <fstream> # include <string> using namespace STD; string STR [302]; // deposit mine clearance matrix 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; elsedfs (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;} elsenum [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 school recruitment Online Test Question 1 ---- minimum number of clicks for mine clearance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.