【扬中集训DAY5T1】 交换矩阵

【题目链接】

          点击打开链接

【算法】

         链表,对于每个点,存它的上,下,左,右分别是谁

【代码】

          

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000

struct node {
    int val;
    node *l,*r,*u,*d; 
} mat[MAXN+10][MAXN+10];

int i,j,x,N,M,Q,A,B,C,D,H,W;
node *pos;

template <typename T> inline void read(T &x) {
    int f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
    for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
    x *= f;
}

template <typename T> inline void write(T x) {
    if (x < 0) { x = -x; putchar('-'); }
    if (x > 9) write(x/10);
    putchar(x%10+'0');    
}

template <typename T> inline void writeln(T x) {
    write(x);
    puts("");    
}

inline void modify(int X1,int Y1,int X2,int Y2,int W,int H) {
    int i;
    node *pos1,*pos2,*tmp1,*tmp2;
    pos1 = &mat[X1][0]; pos2 = &mat[X2][0];
    for (i = 1; i <= Y1; i++) pos1 = pos1 -> r;
    for (i = 1; i <= Y2; i++) pos2 = pos2 -> r;
    tmp1 = pos1; tmp2 = pos2;
    for (i = 1; i <= W; i++) {
        tmp1 -> l -> r = tmp2;
        tmp2 -> l -> r = tmp1;
        swap(tmp1->l,tmp2->l);
        tmp1 = tmp1 -> d; tmp2 = tmp2 -> d;
    }
    tmp1 = pos1; tmp2 = pos2;
    for (i = 1; i < H; i++) tmp1 = tmp1 -> r;
    for (i = 1; i < H; i++) tmp2 = tmp2 -> r;
    for (i = 1; i <= W; i++) {
        tmp1 -> r -> l = tmp2;
        tmp2 -> r -> l = tmp1;
        swap(tmp1->r,tmp2->r);
        tmp1 = tmp1 -> d; tmp2 = tmp2 -> d;
    }
    tmp1 = pos1; tmp2 = pos2;
    for (i = 1; i <= H; i++) {
        tmp1 -> u -> d = tmp2;
        tmp2 -> u -> d = tmp1;
        swap(tmp1->u,tmp2->u);
        tmp1 = tmp1 -> r; tmp2 = tmp2 -> r;
    }
    tmp1 = pos1; tmp2 = pos2;
    for (i = 1; i < W; i++) tmp1 = tmp1 -> d;
    for (i = 1; i < W; i++) tmp2 = tmp2 -> d;
    for (i = 1; i <= H; i++) {
        tmp1 -> d -> u = tmp2;
        tmp2 -> d -> u = tmp1;
        swap(tmp1->d,tmp2->d);
        tmp1 = tmp1 -> r; tmp2 = tmp2 -> r;
    }
}

int main() {
    
    read(N); read(M); read(Q);
    for (i = 0; i <= 1005; i++) {
        for (j = 0; j <= 1005; j++) {
            mat[i][j].r = &mat[i][j+1];
            mat[i][j].d = &mat[i+1][j];
            mat[i][j].l = &mat[i][j-1];
            mat[i][j].u = &mat[i-1][j];
        }
    }
    for (i = 1; i <= N; i++) {
        for (j = 1; j <= M; j++) {
            read(x);
            mat[i][j].val = x;
          } 
    } 
    
    while (Q--) {
        read(A); read(B); read(C); read(D); read(H); read(W);
        modify(A,B,C,D,H,W); 
    }
    
    for (i = 1; i <= N; i++) {
        pos = mat[i][0].r;
        for (j = 1; j <= M; j++) {
            write(pos->val);
            if (j != M) putchar(' ');
            pos = pos -> r;
        }    
        puts("");
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/evenbao/p/9196426.html