十字链表 Codeforces Round #367 E Working routine

 1 // 十字链表 Codeforces Round #367 E Working routine
 2 // 题意:给你一个矩阵,q次询问,每次交换两个子矩阵,问最后的矩阵
 3 // 思路:暴力肯定不行。我们可以每个元素建立十字链表,记录右边和下边的元素,和每个元素的下标(从0开始),每次询问只需要交换四条边的指向即可。
 4 // 本题要建立(n+1)*(m+1)的矩阵
 5 
 6 #include <bits/stdc++.h>
 7 using namespace std;
 8 #define LL long long
 9 const double inf = 123456789012345.0;
10 const LL MOD =100000000LL;
11 const int N =1010*1010;
12 #define clc(a,b) memset(a,b,sizeof(a))
13 const double eps = 1e-7;
14 void fre() {freopen("in.txt","r",stdin);}
15 void freout() {freopen("out.txt","w",stdout);}
16 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
17 
18 int n,m,q;
19 struct node{
20     int v,r,d;
21 }a[N];
22 
23 int chg(int y,int x){
24     return y*(m+1)+x;
25 }
26 
27 int main(){
28     scanf("%d%d%d",&n,&m,&q);
29     for(int i=1;i<=n;i++){
30         for(int j=1;j<=m;j++){
31             scanf("%d",&a[chg(i,j)].v);
32         }
33     }
34 
35     for(int i=0;i<=n;i++){
36         for(int j=0;j<=m;j++){
37             a[chg(i,j)].r=chg(i,j+1);
38             a[chg(i,j)].d=chg(i+1,j);
39         }
40     }
41     while(q--){
42         int x1,y1,x2,y2,h,w;
43         scanf("%d%d%d%d%d%d",&y1,&x1,&y2,&x2,&h,&w);
44         int p1=0,p2=0;
45         for(int i=1;i<y1;i++) p1=a[p1].d;
46         for(int i=1;i<x1;i++) p1=a[p1].r;
47         for(int i=1;i<y2;i++) p2=a[p2].d;
48         for(int i=1;i<x2;i++) p2=a[p2].r;
49         int t1=p1;
50         int t2=p2;
51         for(int i=1;i<=h;i++){
52             t1=a[t1].d;
53             t2=a[t2].d;
54             swap(a[t1].r,a[t2].r);
55         }
56         for(int i=1;i<=w;i++){
57             t1=a[t1].r;
58             t2=a[t2].r;
59             swap(a[t1].d,a[t2].d);
60         }
61         t1=p1,t2=p2;
62         for(int i=1;i<=w;i++){
63             t1=a[t1].r;
64             t2=a[t2].r;
65             swap(a[t1].d,a[t2].d);
66         }
67         for(int i=1;i<=h;i++){
68             t1=a[t1].d;
69             t2=a[t2].d;
70             swap(a[t1].r,a[t2].r);
71         }
72     }
73     int p=0;
74     for(int i=1;i<=n;i++){
75         p=a[p].d;
76         int q=p;
77         for(int j=1;j<=m;j++){
78             q=a[q].r;
79             printf("%d ",a[q].v);
80         }
81         printf("
");
82     }
83     return 0;
84 }
原文地址:https://www.cnblogs.com/ITUPC/p/5845389.html