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;
}
相关推荐
草莓熊Lotso18 分钟前
【C语言强化训练16天】--从基础到进阶的蜕变之旅:Day12
c语言·开发语言·c++·刷题
不喜欢学数学er28 分钟前
算法第五十三天:图论part04(第十一章)
开发语言·python·图论
你怎么知道我是队长38 分钟前
python---构造函数、析构函数
开发语言·python
用户7227868123441 小时前
C++ 使用分治减小模板递归深度
c++
heeheeai1 小时前
jvm对象内存占用
开发语言·jvm·内存分析
ALex_zry1 小时前
Golang云端编程深度指南:架构本质与高阶实践
开发语言·架构·golang
Evand J1 小时前
【PSINS工具箱】MATLAB例程,二维平面上的组合导航,EKF融合速度、位置和IMU数据,4维观测量
开发语言·matlab·平面
As33100102 小时前
IDM 下载失败排查指南:全面解析与解决方案
开发语言·php·idm
2025年一定要上岸2 小时前
【数据结构】-4-顺序表(上)
java·开发语言·数据结构
尘世闲鱼2 小时前
移动零【三种思路】
c++·leetcode