JZOJ 5281 钦点

样例输入:

4 4 2
a a b b
a a b b
c c d d
c c d d
1 1 3 3 2 2
3 1 1 3 2 2

样例输出:

d d c c 
d d c c 
b b a a 
b b a a 

思路:

既然是在矩形里面操作,而且每次移动的是一个整块,同时移动的一对矩形不会相邻

那么就使用一个四向链表模拟矩形,保存每个节点的上下左右节点

实际使用时发现其实双向(向下和向右)就够了

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<string>
 6 using namespace std;
 7 int n,m,q;
 8 struct point{
 9     string v;
10     int r,d;
11 }ma[1001000];
12 int trans(int x,int y){
13     return y*(m+1)+x;
14 }
15 int pos(int x,int y){
16     if (x>y){
17         int ans=trans(x,0);
18         for (int i=0;i<y;i++) ans=ma[ans].d;
19         return ans;
20     }
21     else {
22         int ans=trans(0,y);
23         for (int i=0;i<x;i++) ans=ma[ans].r;
24         return ans;
25     }
26 }
27 
28 int main(){
29     std::ios::sync_with_stdio(false);
30     cin>>n>>m>>q;
31     for(int i=0;i<=n;i++)
32         for(int j=0;j<=m;j++){
33             if (i&&j) cin>>ma[trans(j,i)].v;
34             ma[trans(j,i)].r=trans(j+1,i);
35             ma[trans(j,i)].d=trans(j,i+1);
36         }
37         
38     while(q--){
39         int x1,y1,x2,y2,l,c;
40         cin>>y1>>x1>>y2>>x2>>l>>c;
41         int p1=pos(x1-1,y1-1),p2=pos(x2-1,y2-1);
42         int t1=p1,t2=p2;
43         for(int i=1;i<=l;i++){
44             t1=ma[t1].d;t2=ma[t2].d;
45             swap(ma[t1].r,ma[t2].r);
46         }
47         for(int i=1;i<=c;i++){
48             t1=ma[t1].r;t2=ma[t2].r;
49             swap(ma[t1].d,ma[t2].d);
50         }
51         t1=p1,t2=p2;
52         for(int i=1;i<=c;i++){
53             t1=ma[t1].r;t2=ma[t2].r;
54             swap(ma[t1].d,ma[t2].d);
55         }
56         for(int i=1;i<=l;i++){
57             t1=ma[t1].d;t2=ma[t2].d;
58             swap(ma[t1].r,ma[t2].r);
59         }
60     }
61     int p=0;
62     for(int i=1;i<=n;i++){
63         p=ma[p].d;
64         for(int j=1,q=p;j<=m;j++){
65             q=ma[q].r;
66             cout<<ma[q].v<<" ";
67         }
68         cout<<endl;
69     }
70 }
原文地址:https://www.cnblogs.com/dedicatus545/p/7445685.html