hdu 4941 map的使用

来源:互联网 发布:长款流苏耳环淘宝价 编辑:程序博客网 时间:2024/06/05 20:39

http://acm.hdu.edu.cn/showproblem.php?pid=4941

给定N,M和K,表示在一个N*M的棋盘上有K个棋子,给出K个棋子的位置和值,然后是Q次操作,对应的是:
1 a b :交换a和b两行
2 a b : 交换a和b两列
3 a b :查询a b这个位置上棋子的值,没有棋子的话输出0

不能直接模拟,对应行列交换,只需交换map映射值,查询时输出map[x],map[y]上值即可

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <queue>#include <vector>#include<map>#include <iostream>#include <algorithm>using namespace std;#define RD(x) scanf("%d",&x)#define RD2(x,y) scanf("%d%d",&x,&y)#define clr0(x) memset(x,0,sizeof(x))typedef long long LL;struct node {    int x;    int y;    int c;};node a[110000];int main() {    int _;    RD(_);    for (int i = 1; i <= _; i++) {        printf("Case #%d:\n", i);        int n, m, k;        RD2(n,m);RD(k);        map<int, int> row;        map<int, int> col;        map<pair<int, int>, int> graph;        for (int i = 0; i < k; i++) {            scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].c);            row.insert(make_pair(a[i].x, a[i].x));            col.insert(make_pair(a[i].y, a[i].y));            graph.insert(make_pair(make_pair(a[i].x, a[i].y), a[i].c));        }        int T;RD(T);        while (T--) {            int q, x, y;            scanf("%d%d%d", &q, &x, &y);            if (q == 1) {                if (row.find(x) != row.end() && row.find(y) != row.end()) {                    swap(row[x], row[y]);                }            }            if (q == 2) {                if (col.find(x) != col.end() && col.find(y) != col.end()) {                    swap(col[x], col[y]);                }            }            if (q == 3) {                if (row.find(x) == row.end() || col.find(y) == col.end()) {                    puts("0");                } else {                    printf("%d\n", graph[make_pair(row[x], col[y])]);                }            }        }    }    return 0;}


0 0
原创粉丝点击