天天看点

洛谷 P2670

欢迎提出改进的意见

洛谷 P2670

给出题目链接

  1. 先获取整个数组
  2. 然后遍历数组,同时并创建一个新的数组用来存储数据
  3. 自己写一个函数,用来获取当 ‘?’ 时该位置的周围存在的雷数

下面给出我写的代码

#include <iostream>

using namespace std;

const int N = 110;

int n,m;
char d[N][N];
int b[N][N];

int sum(char q[][N], int i, int j)
{
    int n = 0;
    for (int k = i - 1; k <= i +1; k ++){
        for (int h = j - 1; h <= j + 1; h ++){
            if (q[k][h] == '*') n ++;
        }
    }
    return n;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n;i ++)    
        for (int j = 0; j < m; j ++)
        {
            cin >> d[i][j];
        }
    
    for (int i = 0; i < n; i ++){
        for (int j = 0; j < m;j ++)
        {
            b[i][j] = 0;
            if (d[i][j] == '*') b[i][j] = -1;
            else {
                b[i][j] = sum( d , i ,j);
            }
        }
    }
    for (int i = 0; i <n;i ++)
    {
        for (int j = 0; j < m;j ++)
        {
            if (b[i][j] == -1) cout << '*' ;
            else cout <<b[i][j];
        }
        cout <<endl;
    }
}
           

继续阅读