题解 | #扫雷#

扫雷

https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd

#include<stdio.h>
int main()
{
    int x,y;
    scanf("%d%d",&x,&y);
    getchar();
    char str[x][y];
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            scanf("%c",&str[i][j]);
        }
        getchar();
    }
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            int k=0;
            if(str[i][j]!='*')
            {
                if(i-1>=0&&str[i-1][j]=='*')
                {
                k++;
                }
                if(i-1>=0&&j-1>=0&&str[i-1][j-1]=='*')
                {
                k++;
                }
                if(j-1>=0&&str[i][j-1]=='*')
                {
                    k++;
                }
                if(i-1>=0&&j+1<y&&str[i-1][j+1]=='*')
                {
                    k++;
                }
                if(j+1<y&&str[i][j+1]=='*')
                {
                    k++;
                }
                if(i+1<x&&j-1>=0&&str[i+1][j-1]=='*')
                {
                    k++;
                }
                if(i+1<x&&str[i+1][j]=='*')
                {
                    k++;
                }
                if(i+1<x&&j+1<y&&str[i+1][j+1]=='*')
                {
                    k++;
                }
                printf("%d",k);
        }
            else
                printf("*");
    }
        printf("\n");
}
    return 0;
}
    

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务