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

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

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

 

#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;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值