zoukankan      html  css  js  c++  java
  • 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

    1
    9
    001@11@10
    001111110
    001111110
    001@22@10
    0012@2110
    221222011
    @@11@112@
    2211111@2
    000000111

    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_OFFLINE
    37     freopen("in.txt", "r", stdin);
    38     freopen("out.txt", "w", stdout);
    39 #endif
    40     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
    ", ++kase, ans);
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    图-拓扑排序
    图-最短路径-Dijkstra及其变种
    【链表问题】打卡7:将单向链表按某值划分成左边小,中间相等,右边大的形式
    【链表问题】打卡5:环形单链表约瑟夫问题
    【链表问题】打卡6:三种方法带你优雅判断回文链表
    【链表问题】打卡4:如何优雅着反转单链表
    【链表问题】打卡3:删除单链表的中间节点
    【链表问题】打卡2:删除单链表的第 K个节点
    史上最全面试题汇总,没有之一,不接受反驳
    一些可以让你装逼的算法技巧总结
  • 原文地址:https://www.cnblogs.com/LiuACG/p/4249015.html
Copyright © 2011-2022 走看看