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;
}
相关推荐
Json____23 分钟前
使用python的 FastApi框架开发图书管理系统-前后端分离项目分享
开发语言·python·fastapi·图书管理系统·图书·项目练习
人生在勤,不索何获-白大侠44 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
LIN-JUN-WEI1 小时前
[ESP32]VSCODE+ESP-IDF环境搭建及blink例程尝试(win10 win11均配置成功)
c语言·开发语言·ide·vscode·单片机·学习·编辑器
望获linux2 小时前
【Linux基础知识系列】第四十三篇 - 基础正则表达式与 grep/sed
linux·运维·服务器·开发语言·前端·操作系统·嵌入式软件
眠りたいです2 小时前
Mysql常用内置函数,复合查询及内外连接
linux·数据库·c++·mysql
笑鸿的学习笔记2 小时前
qt-C++语法笔记之Stretch与Spacer的关系分析
c++·笔记·qt
留不住丨晚霞2 小时前
说说SpringBoot常用的注解?
java·开发语言
hardStudy_h3 小时前
C++——内联函数与Lambda表达式
开发语言·jvm·c++
艾莉丝努力练剑3 小时前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法
ZZZS05163 小时前
stack栈练习
c++·笔记·学习·算法·动态规划