zoukankan      html  css  js  c++  java
  • hdu 4941 map的使用

    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:
    ", 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
    ", graph[make_pair(row[x], col[y])]);
                    }
                }
            }
        }
        return 0;
    }


  • 相关阅读:
    java开发中的重中之重-------mysql(基础篇)
    开发中的重点-----设计模式
    java 不可不知的数据库知识-----事物
    redis 入门笔记
    转 Java对日期Date类进行加减运算一二三
    Ajax 中的高级请求和响应
    Ajax之基础总结
    Spring中的国际化资源以及视图跳转
    javascript基础总结
    SpringMVC的表单标签
  • 原文地址:https://www.cnblogs.com/zibaohun/p/4046840.html
Copyright © 2011-2022 走看看