本文介绍: 言归正传,瞅瞅这道编程题,不难,用pair表示坐标,map关联容器存储各坐标点状态(key-2维坐标,value-地雷状态0/1),然后迭代器遍历map地雷阵,计算当前坐标点处四周8个位置的地雷数(注意地雷矩阵边缘处的邻近区域的特殊性,可以通过map的find()成员函数的返回值鉴定),输出。刷到一个扫雷的题目,之前没有玩怎么过扫雷,于是我就去玩了玩,一玩就凌晨两点,直接上瘾好几天哈哈。C++STL之Map容器 – 数据结构教程 – C语言网 (dotcpp.com)

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

原文地址:https://blog.csdn.net/qq_43855258/article/details/135331358

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_52526.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注