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