hdu 4941 Magical Forest(hash映射),hdu4941

來源:互聯網
上載者:User

hdu 4941 Magical Forest(hash映射),hdu4941

題目連結:hdu 4941 Magical Forest

題目大意:給定N,M和K,表示在一個N*M的棋盤上有K個棋子,給出K個棋子的位置和值,然後是Q次操作,對應的是:

  • 1 a b :交換a和b兩行
  • 2 a b : 交換a和b兩列
  • 3 a b :查詢a b這個位置上棋子的值,沒有棋子的話輸出0

解題思路:一開始X[i]=i,X[j]=j,如果需要交換i和j,那麼就令X[i]=j,X[j]=i即可,因為N和M很大,所以用map映射。

#include <cstdio>#include <cstring>#include <map>#include <algorithm>using namespace std;typedef pair<int, int> pii;map<pii, int> g;map<int , int> X;map<int, int> Y;int N, M, K;void init () {    g.clear();    X.clear();    Y.clear();    int a, b, w;    scanf("%d%d%d", &N, &M, &K);    for (int i = 0; i < K; i++) {        scanf("%d%d%d", &a, &b, &w);        g[make_pair(a,b)] = w;    }}int get (map<int, int>& u, int x) {    if (u.count(x))        return u[x];    return u[x] = x;}int main () {    int cas, n;    scanf("%d", &cas);    for (int kcas = 1; kcas <= cas; kcas++) {        init();        scanf("%d", &n);        printf("Case #%d:\n", kcas);        int s, a, b;        for (int i = 0; i < n; i++) {            scanf("%d%d%d", &s, &a, &b);            if (s == 1) {                int p = get(X, a);                int q = get(X, b);                swap(X[a], X[b]);            } else if (s == 2) {                int p = get(Y, a);                int q = get(Y, b);                swap(Y[a], Y[b]);            } else {                int p = get(X, a);                int q = get(Y, b);                printf("%d\n", g.count(make_pair(p, q)) ? g[make_pair(p, q)] : 0);            }        }    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.