首页 > 代码库 > FZU 1920
FZU 1920
Description
Mine sweeper is a very popular small game in Windows operating system. The object of the game is to find mines, and mark them out. You mark them by clicking your right mouse button. Then you will place a little flag where you think the mine is. You click your left mouse button to claim a square as not being a mine. If this square is really a mine, it explodes, and you lose. Otherwise, there are two cases. In the first case, a little colored numbers, ranging from 1 to 8, will display on the corresponding square. The number tells you how many mines are adjacent to the square. For example, if you left-clicked a square, and a little 8 appeared, then you would know that this square is surrounded by 8 mines, all 8 of its adjacent squares are mines. In the second case, when you left-click a square whose all adjacent squares are not mines, then all its adjacent squares (8 of its adjacent squares) are mine-free. If some of these adjacent squares also come to the second case, then such deduce can go on. In fact, the computer will help you to finish such deduce process and left-click all mine-free squares in the process. The object of the game is to uncover all of the non-mine squares, without exploding any actual mines. Tom is very interesting in this game. Unfortunately his right mouse button is broken, so he could only use his left mouse button. In order to avoid damage his mouse, he would like to use the minimum number of left clicks to finish mine sweeper. Given the current situation of the mine sweeper, your task is to calculate the minimum number of left clicks.
Input
The first line of the input contains an integer T (T <= 12), indicating the number of cases. Each case begins with a line containing an integer n (5 <= n <= 9), the size of the mine sweeper is n×n. Each of the following n lines contains n characters M ij(1 <= i,j <= n), M ijdenotes the status of the square in row i and column j, where ‘@’ denotes mine, ‘0-8’ denotes the number of mines adjacent to the square, specially ‘0’ denotes there are no mines adjacent to the square. We guarantee that the situation of the mine sweeper is valid.
Output
For each test case, print a line containing the test case number (beginning with 1) and the minimum left mouse button clicks to finish the game.
Sample Input
19001@11@10001111110001111110001@22@100012@2110221222011@@11@112@2211111@2000000111
Sample Output
Case 1: 24
题解:按照扫雷的规则易知, 清掉0附近的元素,剩下遍历未被访问且非‘@’的个数即可。详见代码。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX = 16; 7 const int x[] = { -1, -1, -1, 0, 1, 1, 1, 0 }; 8 const int y[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; 9 10 char arr[MAX][MAX];11 bool vis[MAX][MAX];12 13 void DFS(int a, int b, int n)14 {15 int ta, tb;16 if(vis[a][b])17 {18 vis[a][b] = false;19 }20 if(arr[a][b] != ‘0‘)21 {22 return;23 }24 for(int i = 0; i < 8; i++)25 {26 ta = a + x[i]; tb = b + y[i];27 if((ta >= 0 && ta < n) && (tb >= 0 && tb < n) && vis[ta][tb])28 {29 DFS(ta, tb, n);30 }31 }32 }33 34 int main()35 {36 #ifdef CDZSC_OFFLINE37 freopen("in.txt", "r", stdin);38 freopen("out.txt", "w", stdout);39 #endif40 int t, n, kase = 0;41 scanf("%d", &t);42 while(t--)43 {44 memset(arr, 0, sizeof(arr));45 memset(vis, true, sizeof(vis));46 scanf("%d", &n);47 for(int i = 0; i < n; i++)48 {49 scanf("%s", arr[i]);50 }51 int ans = 0;52 for(int i = 0; i < n; i++)53 {54 for(int j = 0; j < n; j++)55 {56 if(arr[i][j] == ‘@‘)57 {58 vis[i][j] = false;59 }60 }61 }62 for(int i = 0; i < n; i++)63 {64 for(int j = 0; j < n; j++)65 {66 if(vis[i][j] && arr[i][j] == ‘0‘)67 {68 DFS(i, j, n);69 ans++;70 }71 }72 }73 for(int i = 0; i < n; i++)74 {75 for(int j = 0; j < n; j++)76 {77 if(vis[i][j] && arr[i][j] != ‘@‘)78 ans++;79 }80 }81 printf("Case %d: %d\n", ++kase, ans);82 }83 return 0;84 }
FZU 1920
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。