扫雷

在这里插入图片描述

#include<iostream>
#include<cstdio>
using namespace std;
char s[101][101];
char w[101][101];
int main()
{
    int n,m;
    cin>>n>>m;
    char c;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {cin>>s[i][j];}
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        int ans=0;
        if(s[i][j]!='*')
        {
        for(int r=-1;r<=1;r++)
        {
            if(i+r>=1&&i+r<=n)
        {
            for(int c=-1;c<=1;c++)
        {
            if(j+c>=1&&j+c<=m)
            {
                //cout<<'1';
                if(s[i+r][j+c]=='*') {ans++;}
            }  
        }
        }
        w[i][j]=ans+'0';
        }
        }
        else
        {
            w[i][j]='*';
        }
        
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        cout<<w[i][j];
        cout<<endl;
    }
    return 0;
}
posted @ 2022-06-06 20:35  lishangli  阅读(19)  评论(0)    收藏  举报