uva 10189 扫雷

简单的输入 判断周围上下左右组合的八个方向的雷 然后输出

代码

#include <iostream>
#include <memory.h>

using namespace std;


int n, m;

const int N = 110;

char mine[N][N];

int addx[8] = { 1,-1,1,-1,1,-1,0,0 };
int addy[8] = { 1,-1,-1,1,0,0,1,-1 };

char GetMineCount(int x, int y)
{
    char ret;

    if (mine[x][y] == '*')
        return '*';

    int count = 0;
    for (int i = 0; i < 8; i++) {
        int newx = x + addx[i];
        int newy = y + addy[i];

        if (newx >= 0 && newx < n && newy >= 0 && newy < m && mine[newx][newy] == '*')
            count++;
    }

    ret = '0' + count;
    return ret;
}

int main()
{
    int idx = 0;
    while (1) {
        cin >> n >> m;
        if (n == 0 || m == 0) return 0;
        memset(mine, 0, N*N);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> mine[i][j];
            }
        }
        if(idx >0 ) cout << endl;

        cout << "Field #" << ++idx << ":" << endl;

        for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
                mine[x][y] = GetMineCount(x, y);
                cout << mine[x][y];
            }
            cout << endl;
        }
    }


    return 0;
}

 

posted on 2019-10-22 08:58  itdef  阅读(235)  评论(0编辑  收藏  举报

导航