zoukankan      html  css  js  c++  java
  • 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;
    }
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    程序员常用英语词汇
    声明式编程与命令式编程
    vue 常用ui组件库
    Vue 组件之间传值
    vscode插件之背景插件(background)
    iconfont的使用
    CSS3 @font-face 规则
    CSS抗锯齿 font-smoothing 属性介绍
    new Image 读取宽高为0——onload
    js的for循环中出现异步函数,回调引用的循环值始终是最后的值
  • 原文地址:https://www.cnblogs.com/itdef/p/11717721.html
Copyright © 2011-2022 走看看