STL map容器与pair类模板(解决扫雷问题)

C++STL之Map容器 - 数据结构教程 - C语言网 (dotcpp.com)https://www.dotcpp.com/course/118C++STL之Pair类模板 - 数据结构教程 - C语言网 (dotcpp.com)https://www.dotcpp.com/course/119 刷到一个扫雷的题目,之前没有玩怎么过扫雷,于是我就去玩了玩,一玩就凌晨两点,直接上瘾好几天哈哈。

言归正传,瞅瞅这道编程题,不难,用pair表示坐标,map<pair<int,int>,int>关联容器存储各坐标点状态(key-2维坐标,value-地雷状态0/1),然后迭代器遍历map地雷阵,计算当前坐标点处四周8个位置的地雷数(注意地雷矩阵边缘处的邻近区域的特殊性,可以通过map的find()成员函数的返回值鉴定),输出。

cpp 复制代码
#include<iostream>
#include<map>
#include<vector>
using namespace std;

void mineSweeper(const map<pair<int,int>,int>& sweeper, vector<int>& sweeper_size){
    
    for(auto it=sweeper.begin(); it!=sweeper.end(); it++){
        // sweeper
        if(it->second == 1){
            cout<<"*";
        }
        // not sweeper, count sweeper_num around
        else{
            int sweeper_sum=0;
            for(int i=-1; i<=1; i++){
                for(int j=-1; j<=1; j++){
                    int locs_1 = it->first.first + i;
                    int locs_2 = it->first.second + j;
                    if(sweeper.find(make_pair(locs_1,locs_2)) != sweeper.end()){
                        // cout<<"("<<locs_1<<","<<locs_2<<")";
                        sweeper_sum += sweeper.find(make_pair(locs_1,locs_2))->second;
                        // cout<<"<"<<sweeper_sum<<">";
                    }
                }
            }
            cout<<sweeper_sum;
        }
        if(it->first.second == sweeper_size[1]){
            cout<<endl;
        }
    }
}

int main()
{
    int num = 0;
    while(1){
        ++num;
        // input
        pair<int,int> locs;
        char input;
        map<pair<int,int>,int> sweeper;
        vector<int> sweeper_size(2);
        // sweeper size  input
        cin>>sweeper_size[0]>>sweeper_size[1];
        if(sweeper_size[0]==0 && sweeper_size[1]==0){
            break;
        }
        // sweeper content input
        for(int i=1; i<=sweeper_size[0]; i++){
            for(int j=1; j<=sweeper_size[1]; j++){
                cin>>input;
                locs=make_pair(i,j);
                if(input == '*'){
                    sweeper.insert(pair<pair<int,int>,int>(locs,1));
                }
                else{
                    sweeper.insert(pair<pair<int,int>,int>(locs,0));
                }
            }
        }
        
        // // debug input
        // for(auto it=sweeper.begin(); it!=sweeper.end(); it++){
        //     // cout<<it->first.first<<","<<it->first.second<<":"<<it->second<<endl;
        //     cout<<it->second;
        //     if(it->first.second == 4){
        //         cout<<endl;
        //     }
        // }  
        
        // mineSweeper
        cout<<"Field #"<<num<<":"<<endl;
        mineSweeper(sweeper, sweeper_size);
        cout<<endl;
    }

    return 0;
}
相关推荐
笨笨马甲5 分钟前
Qt Quick模块功能及架构
开发语言·qt
乄夜8 分钟前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
夜晚回家19 分钟前
「Java基本语法」代码格式与注释规范
java·开发语言
YYDS31424 分钟前
C++动态规划-01背包
开发语言·c++·动态规划
前端页面仔33 分钟前
易语言是什么?易语言能做什么?
开发语言·安全
树叶@1 小时前
Python数据分析7
开发语言·python
wydaicls1 小时前
十一.C++ 类 -- 面向对象思想
开发语言·c++
Biomamba生信基地1 小时前
R语言基础| 下载、安装
开发语言·r语言·生信·医药
姜君竹1 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
思捻如枫2 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++