直接使用指针,交换时交换矩阵周围的指针即可。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 1005; struct dl{ int v; struct dl *d, *r; }e[N][N]; int main(){ int n, m , q; scanf("%d%d%d", &n, &m, &q); int x, y, s, t, h, w; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++) scanf("%d", &e[i][j].v); } for(int i = 0; i <= n; i++){ for(int j = 0; j <= m; j++){ e[i][j].r = &e[i][j + 1]; e[i][j].d = &e[i + 1][j]; } } for(int i = 1; i <= q; i++){ scanf("%d%d%d%d%d%d", &x, &y, &s, &t, &h, &w); dl *pos1, *pos2; pos2 = pos1 = &e[0][0]; int th = h, tw = w; while(-- x) pos1 = pos1 -> d; while(-- y) pos1 = pos1 -> r; th = h, tw = w; while(-- s) pos2 = pos2 -> d; while(-- t) pos2 = pos2 -> r; dl* tp1 = pos1, *tp2 = pos2; for(int r = 1; r <= h; r++){ pos1 = pos1 -> d; pos2 = pos2 -> d; swap(pos1 -> r, pos2 -> r); } for(int c = 1; c <= w; c++){ pos1 = pos1 -> r; pos2 = pos2 -> r; swap(pos1 -> d, pos2 -> d); } for(int c = 1; c <= w; c++){ tp1 = tp1 -> r; tp2 = tp2 -> r; swap(tp1 -> d, tp2 -> d); } for(int r = 1; r <= h; r ++){ tp1 = tp1 -> d; tp2 = tp2 -> d; swap(tp1 -> r, tp2 -> r); } } dl *pos1 = &e[0][0]; for(int i = 1; i <= n; i++){ pos1 = pos1 -> d; dl *tmp = pos1; for(int j = 1; j <= m; j++){ pos1 = pos1 ->r; if(j == 1) printf("%d", pos1 ->v); else printf(" %d", pos1 ->v); } puts(""); pos1= tmp; } return 0; }
时间: 2024-10-07 16:28:38