栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

扫雷

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

扫雷

题目描述

扫雷是一个经典的游戏,在一个$N*M$区域中 
输入一个$N*M$的雷区中的k个雷坐标,打印出所有雷区的分布 
一个位置如果是雷则表示为*,否则应该是0~8,表示他周围八连通域中的地雷总数。

输入

雷区尺寸:$3=地雷数目:$k<=M*N$
和他们的坐标:$xi$,$yi$

输出

雷区的分布 
每组后空一行 

题解
#include
#include 

using namespace std;
const int maxn = 21;
const int BOMB = 999;
int a[maxn][maxn];
int m, n, k, x, y;

void show() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] != BOMB) {
                cout << a[i][j];
            } else {
                cout << "*";
            }
        }
        cout << endl;
    }
}

void cal() {
    for (x = 0; x < n; x++)
        for (y = 0; y < m; y++) {
            if (a[x][y] == BOMB) {
                continue;
            }
            int &tot = a[x][y];
            for (int dx = -1; dx <= 1; ++dx)
                for (int dy = -1; dy <= 1; ++dy) {
                    if (dx == 0 && dy == 0) {
                        continue;
                    }
                    int nx = x + dx;
                    int ny = y + dy;
                    if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
                        continue;
                    }
                    if (a[nx][ny] == BOMB) {
                        ++tot;
                    }
                }
        }
}

void input() {
    memset(a, 0, sizeof(a));
    while (k--) {
        cin >> x >> y;
        a[x][y] = BOMB;
    }
}

int main() {
    while (cin >> n >> m >> k) {
        input();
        cal();
        show();
        cout << endl;
    }
    return 0;
}

相关

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011875.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号