sdnuoj1127 扫雷

题意:多组输入,每组先输入m和n,代表雷区的面积为mn;
再输入该雷区,判断每个点,若为则雷
则输出*,若不为雷*,则判断他的周围有几个雷,并输出

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <sstream>
#include <map>
#include <set>
typedef unsigned long long ull;
using namespace std;

char ch[105][105];//定义的二维数组,用来表示雷区
int a, b;//雷区的大小、

int dx[]= {
    
    0, 0, -1, -1, -1, 1, 1, 1};//用于判断周围的雷个数时,进行的走法
int dy[]= {
    
    1, -1, 0, -1, 1, 0, -1, 1};
int judge(int x, int y)//判断是否越界
{
    
    
    if(x < 0 || y < 0 || x >= a || y >= b)
        return 0;
    return 1;
}

void sl(int x, int y)//对(x,y)进行判断
{
    
    
    if(ch[x][y] == '*')
        cout<<'*';
    else
    {
    
    
        int sum = 0;//注意清0
        for(int i = 0; i < 8; i++)
        {
    
    
            if(ch[x + dx[i]][y + dy[i]] == '*' && judge(x+dx[i],y+dy[i]))//注意judge是(x,y),不是ch[]之类的
                sum++;
        }
        cout<<sum;
    }
}

int main()
{
    
    
    int n = 0;
    while(cin>>a>>b && a && b)
    {
    
    
        n++;
        for(int i = 0; i < a; i++)
        {
    
    
            cin>>ch[i];//是输入的一行,效果类似于两个for循环
            
        }
        cout<<"Field"<<' '<<'#'<<n<<':'<<'\n';//注意换行
        for(int i = 0; i < a; i++)
        {
    
    
            for(int j = 0; j < b; j++)
            {
    
    
                sl(i, j);
            }
            cout<<'\n';
        }
        cout<<'\n';//注意换行,不然就会PE

    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_51216553/article/details/109611875