zcmu-1176 扫雷(简单题)

 题目链接

因为一直没玩过扫雷,前几天训练赛遇到了一道扫雷的题目,完全没办法下手,就狂玩扫雷,还挺好玩的!扫出来的数字就代表这个格子的周围(3*3的格子,这个数放在中心,剩余的8个格子叫做“它的周围”)有几个炸弹!下面是我玩过的通关截图,没玩过的可以帮助理解。

这道题目比较简单,只要计算是-1的格子的周围有几个9就行了 


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int a[105][105];
int main()
{
    int n,m,flag = 0;
    while(cin>>n>>m)
    {
        memset(a, 0, sizeof(a));
        if(flag == 1)//处理空行,不要忘了!
            cout<<endl;
        flag = 1;
        for(int i = 1;i <= n;i++)
        {
            for(int j =1;j <= m;j++)
            {
                cin>>a[i][j];
            }
        }
        for(int i = 1;i <= n;i++)
        {
            for(int j =1;j <= m;j++)
            {
                if(a[i][j] != 9)
                {
                    for(int p = i -1 ;p <= i + 1;p++)
                    {
                        for(int q = j -1; q <= j + 1;q++)
                        {
                            if(a[p][q] == 9)
                                a[i][j] ++;//统计不是炸弹的格子 周围的炸弹数
                        }
                    }
                }
            }
        }
        for(int i = 1;i <= n;i++)
        {
            for(int j =1;j <= m;j++)
            {
                if(a[i][j] == 9)
                    cout<<a[i][j];
                else
                    cout<<a[i][j] + 1;
            }
            cout<<endl;
        }
        
        
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/81976304
今日推荐