列表

详情


BC142. 扫雷

描述

sun上课的时候非常喜欢玩扫雷。他现小sun有一个初始的雷矩阵,他希望你帮他生成一个扫雷矩阵。

 

扫雷矩阵的每一行每一列都是一个数字,每个数字的含义是与当前位置相邻的8个方向中,有多少个雷(在下图中,雷用*表示);如果当前位置就是雷的话,仍输出一个*。

比如初始的雷矩阵如下:

....

..**

*.*.

.*.*
对应的数字矩阵为:

0122

13**

 *4*4

2*3* 





输入描述

第一行两个整数n,m,代表矩阵有n行m列

接下来共n行,每行m个字符

输出描述

输出共n行m列,为扫雷矩阵。

示例1

输入:

4 4
....
..**
*.*.
.*.*

输出:

0122
13**
*4*4
2*3*

示例2

输入:

3 4
....
*..*
.*.*

输出:

1111
*23*
2*3*

原站题解

C 解法, 执行用时: 16ms, 内存消耗: 1920KB, 提交时间: 2022-04-16

#include<stdio.h>
#include<string.h>
int main()
{
    int n,m,count;
    scanf("%d %d",&n,&m);
    char A[1002][1002]={0};
    getchar();
    for(int i=1;i<=n;i++){
         for(int j=1;j<=m;j++)
         {
              A[i][j]=getchar();
         }    getchar();
       }     
   
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(A[i][j]=='.')
            {
                count=0;
                if(A[i-1][j-1]=='*')    count++;
                if(A[i-1][j]=='*')      count++;
                if(A[i-1][j+1]=='*')    count++;
                if(A[i][j-1]=='*')      count++;
                if(A[i][j+1]=='*')      count++;
                if(A[i+1][j-1]=='*')    count++;
                if(A[i+1][j]=='*')      count++;
                if(A[i+1][j+1]=='*')    count++;
                A[i][j]='0'+count;
                printf("%c",A[i][j]);
            }else if(A[i][j]=='*')
                printf("%c",A[i][j]);
        }       printf("\n");
    }
    
}

C 解法, 执行用时: 16ms, 内存消耗: 1932KB, 提交时间: 2022-04-27

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

C 解法, 执行用时: 16ms, 内存消耗: 1932KB, 提交时间: 2022-04-04

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

C 解法, 执行用时: 16ms, 内存消耗: 1980KB, 提交时间: 2022-05-06

#include<stdio.h>
#include<string.h>
int main() {
    int n, m, count;
    scanf("%d %d", &n, &m);
    char A[1002][1002] = {0};
    getchar();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            A[i][j] = getchar();
        }
        getchar();
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (A[i][j] == '.') {
                count = 0;
                if (A[i - 1][j - 1] == '*')    count++;
                if (A[i - 1][j] == '*')      count++;
                if (A[i - 1][j + 1] == '*')    count++;
                if (A[i][j - 1] == '*')      count++;
                if (A[i][j + 1] == '*')      count++;
                if (A[i + 1][j - 1] == '*')    count++;
                if (A[i + 1][j] == '*')      count++;
                if (A[i + 1][j + 1] == '*')    count++;
                A[i][j] = '0' + count;
                printf("%c", A[i][j]);
            } else if (A[i][j] == '*')
                printf("%c", A[i][j]);
        }
        printf("\n");
    }

}

C 解法, 执行用时: 16ms, 内存消耗: 2712KB, 提交时间: 2022-07-28

#include <stdio.h>
#include <string.h>
int main()
{
    int n, m, count;
    scanf("%d %d", &n, &m);
    char A[1002][1002] = { 0 };
    getchar();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
        {
            A[i][j] = getchar();
        }    getchar();
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (A[i][j] == '.')
            {
                count = 0;
                if (A[i - 1][j - 1] == '*')    count++;
                if (A[i - 1][j] == '*')      count++;
                if (A[i - 1][j + 1] == '*')    count++;
                if (A[i][j - 1] == '*')      count++;
                if (A[i][j + 1] == '*')      count++;
                if (A[i + 1][j - 1] == '*')    count++;
                if (A[i + 1][j] == '*')      count++;
                if (A[i + 1][j + 1] == '*')    count++;
                A[i][j] = '0' + count;
                printf("%c", A[i][j]);
            }
            else if (A[i][j] == '*')
                printf("%c", A[i][j]);
        }       printf("\n");
    }
    return 0;
}

上一题