1 solutions

  • 0
    @ 2025-11-5 14:56:48

    C :

    #include"stdio.h"
    #include"string.h"
    int main()
    {
        int n,m,i,j,s=0;
        char str[105][105];
        while(scanf("%d%d",&n,&m),m+n)
        {
             memset(str,0,sizeof(str));
            for(i=1;i<=n;i++)
                scanf("%s",str[i]+1);
    
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
            {
                if(str[i][j]=='.')
                    {
                        int x,y,k=0;
                        for(x=-1;x<=1;x++)
                        for(y=-1;y<2;y++)
                        if(str[i+x][j+y]=='*')
                        k++;
                        str[i][j]=k+'0';
                    }
            }
            s++;
            printf("Field #%d:\n",s);
            for(i=1;i<=n;i++)
                puts(str[i]+1);
                printf("\n");
        }
        return 0;
    }
    

    C++ :

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    #define MAXSIZE 100
    
    inline bool range_checking(int x, int y, int row, int column)
    {
        return (0 <= x && x < row) && (0 <= y && y < column);
    }
    
    void display(char matrix[][MAXSIZE], int row, int column)
    {
        int bounds[8][2] =
            { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0},
            {1, 1} };
    
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < column; j++)
            {
                // 如果该位置为地雷则原样输出地雷的符号。
                if (matrix[i][j] == '*')
                    cout << "*";
                else
                {
                    // 统计该点周围 8 点的地雷总数。
                    int mines = 0;
                    for (int k = 0; k < 8; k++)
                    {
                        int m = i + bounds[k][0];
                        int n = j + bounds[k][1];
    
                        if (range_checking(m, n, row, column)
                            && matrix[m][n] == '*')
                            mines++;
                    }
    
                    cout << mines;
                }
            }
    
            cout << endl;
        }
    }
    
    int main(int ac, char *av[])
    {
        char matrix[MAXSIZE][MAXSIZE];
        int row, column, field = 0;
    
        while ((cin >> row >> column, row && column))
        {
            memset(matrix, 0, sizeof(matrix));
    
            for (int i = 0; i < row; i++)
                for (int j = 0; j < column; j++)
                    cin >> matrix[i][j];
    
            if (field > 0)
                cout << endl;
            field++;
    
            cout << "Field #" << field << ":" << endl;
    
            display(matrix, row, column);
        }
    
        return 0;
    }
    
    
    • 1

    Information

    ID
    16250
    Time
    3000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By