1.map用法

一一映射,hashmap

1)map.count(Key)函数

返回值为1或者0,1返回存在,0返回不存在,返回的是布尔类型的值,因为在map类型中所有的数据的Key值都是不同的,所以被count的数要么存在1次,要么不存在

key-value

2.pair&make_pair区别

template pair make_pair(T1 a, T2 b) { return pair(a, b); }

make_pair返回的是pair的集合

#include<cstdio>
#include<cstring>
#include<map>
using namespace std;

struct UF {
    int F[200010];
    int find(int x) {
        if (F[x] == -1) return x;
        return F[x] = find(F[x]);
    }
    void init() {
        memset(F, -1, sizeof(F));
    }
    void join(int x, int y) {
        int t1 = find(x);
        int t2 = find(y);
        if (t1 != t2) {
            F[t2] = t1;
        }
    }
};

UF N, E, W, S;

map<pair<int, int>, int> p2Id;
int tot;
map<int, pair<int, int>> id2P;
void init() {
    N.init();
    E.init();
    W.init();
    S.init();
    tot = 0;
    p2Id.clear();
    id2P.clear();
}

int getId(int x, int y) {
    pair<int, int> p = make_pair(x, y);
    if (!p2Id.count(p)) {
        p2Id[p] = tot;
        id2P[tot] = p;
        tot++;
    }
    return p2Id[p];
}

void gao(int x, int y) {
    int now = getId(x, y);

    int w = getId(x, y-1);
    int e = getId(x, y+1);
    int n = getId(x-1, y);
    int s = getId(x+1, y);
    W.join(w, now);
    E.join(e, now);
    N.join(n, now);
    S.join(s, now);
}

pair<int, int> getNext(int x, int y, char dir) {
    int now = getId(x, y);
    int nextId;
    if (dir == 'N') {
        nextId = N.find(now);
    } else if (dir == 'E') {
        nextId = E.find(now);
    } else if (dir == 'W') {
        nextId = W.find(now);
    } else if (dir == 'S') {
        nextId = S.find(now);
    }
    return id2P[nextId];
}

char str[50010];

int main() {
    int T;
    int iCase = 0;
    scanf("%d", &T);
    while (T--) {
        iCase++;
        int N, R, C, sx, sy;
        scanf("%d%d%d%d%d", &N, &R, &C, &sx, &sy);
        scanf("%s", str);
        init();
        gao(sx, sy);
        pair<int, int> now = make_pair(sx, sy);
        for (int i = 0; i < N; ++i) {
            now = getNext(now.first, now.second, str[i]);
            gao(now.first, now.second);
        }
        printf("Case #%d: %d %d\n", iCase, now.first, now.second);
    }
    return 0;
}

 

 

posted on 2020-01-24 12:11  黑暗尽头的超音速炬火  阅读(147)  评论(0编辑  收藏  举报