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;
    }
  • 相关阅读:
    cocos2d-x 3.0rc1 编译cpp-testsproject
    [wxWidgets]_[0基础]_[不常见但有用的类wxStandardPaths]
    教你摆脱低级程序猿 项目中cocopads的安装使用
    Android使用代码模拟HOME键的功能
    UVA 1508
    asp.net mvc5 安装
    Java_并发线程_Semaphore、CountDownLatch、CyclicBarrier、Exchanger
    crm操作产品实体
    BZOJ 3172 [Tjoi2013]单词 AC自己主动机(fail树)
    ADO与ADO.Net
  • 原文地址:https://www.cnblogs.com/9968jie/p/12459363.html
Copyright © 2011-2022 走看看