zoukankan      html  css  js  c++  java
  • CCF 201912-2 回收站选址

    ///因为这道题的键值很大所以用数组来实现hash是不现实的,这时我们就要用map<node, int>
    ///来实现hash
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>
    #include <string.h>
    #include <map>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    struct node{
        int x, y;
        bool operator < (const node& other) const{//map中的key一定要有大小之分,像node这种类型本身无大小
            if(x!=other.x)                        //则需要定义他们元素之间大小的规则
                return x < other.x;
            else
                return y < other.y;
        }
    }s[1010];
    
    map<node, int> h;
    
    int dir[4][2]={{-1,0}, {0,-1}, {1,0}, {0,1}};
    
    bool fun(node t)
    {
        for(int i=0; i<4; i++)
        {
            node point;
            point.x=t.x+dir[i][0];
            point.y=t.y+dir[i][1];
            if(h.find(point)==h.end())//h.find(key)返回键为key的映射迭代器,用h.count(point)==0是同样的效果
                return false;
        }
        node point;
        point.x=t.x+1;
        point.y=t.y+1;
        if(h.count(point))//h.count(key)返回键为key的是否存在,存在返回1,不存在返回0
            h[t]++;
    
        point.x=t.x+1;
        point.y=t.y-1;
        if(h.count(point))
            h[t]++;
    
        point.x=t.x-1;
        point.y=t.y+1;
        if(h.count(point))
            h[t]++;
    
        point.x=t.x-1;
        point.y=t.y-1;
        if(h.count(point))
            h[t]++;
    
        return true;
    }
    
    int main()
    {
        int n, cnt[10];
        memset(cnt, 0, sizeof(cnt));
        cin >> n;
        for(int i=0; i<n; i++)
        {
            cin >> s[i].x >> s[i].y;
            h[s[i]]=0;
        }
        map<node, int>::iterator it=h.begin();
        for(; it!=h.end(); it++)
        {
            if(fun(it->first))
                cnt[it->second]++;
        }
    
        for(int i=0; i<=4; i++)
            cout << cnt[i] << endl;
        return 0;
    }
  • 相关阅读:
    七.贪心算法
    六。二叉树
    从git指定commit拉分支
    二分法
    mysql 解决生僻字,特殊字符插入失败
    MYSQL性能优化以及建议
    PDF快捷键
    GC 核心关注点都在这里
    R语言载入包时报错:Error: 程辑包‘survival’没有名字空间
    Centos buff/cache过高
  • 原文地址:https://www.cnblogs.com/9968jie/p/12459363.html
Copyright © 2011-2022 走看看