Vasiliy finally got to work, where there is a huge amount of tasks waiting for him. Vasiliy is given a matrix consisting of n rows and m columns and q tasks. Each task is to swap two submatrices of the given matrix.
For each task Vasiliy knows six integers ai, bi, ci, di, hi, wi, where ai is the index of the row where the top-left corner of the first rectangle is located, bi is the index of its column, ci is the index of the row of the top-left corner of the second rectangle, di is the index of its column, hi is the height of the rectangle and wi is its width.
It's guaranteed that two rectangles in one query do not overlap and do not touch, that is, no cell belongs to both rectangles, and no two cells belonging to different rectangles share a side. However, rectangles are allowed to share an angle.
Vasiliy wants to know how the matrix will look like after all tasks are performed.
Input
The first line of the input contains three integers n, m and q (2 ≤ n, m ≤ 1000, 1 ≤ q ≤ 10 000) — the number of rows and columns in matrix, and the number of tasks Vasiliy has to perform.
Then follow n lines containing m integers vi, j (1 ≤ vi, j ≤ 109) each — initial values of the cells of the matrix.
Each of the following q lines contains six integers ai, bi, ci, di, hi, wi (1 ≤ ai, ci, hi ≤ n, 1 ≤ bi, di, wi ≤ m).
Output
Print n lines containing m integers each — the resulting matrix.
Example
4 4 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
1 1 3 3 2 2
3 1 1 3 2 2
4 4 3 3
4 4 3 3
2 2 1 1
2 2 1 1
4 2 1
1 1
1 1
2 2
2 2
1 1 4 1 1 2
2 2
1 1
2 2
1 1
真的非常涨姿势的一道题。
容易发现,矩阵中元素的位置关系实际上只用某一个点的右侧、下方是谁就可以描述。并且进行矩阵交换时,实际上矩阵内部的元素中间的上述关系并没有发生改变,发生改变的只有部分矩阵内的框架及部分其外部的框架。故可以将暴力交换改为对“框架”的维护。
这样做利用的就是十字链表的想法,而实际实现时结合具体情况,只要使用数组进行维护即可。
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 using namespace std; 10 const int MAX=15e5; 11 int rright[MAX],down[MAX],num[MAX]; 12 int n,m,q; 13 int id; 14 int getid(int x,int y)//x行,y列 15 { 16 return x*m+y; 17 } 18 void init(int x,int y) 19 { 20 for(int i=0;i<=x;i++) 21 for(int j=0;j<=y;j++) 22 { 23 id=getid(i,j); 24 if(i&&j) 25 scanf("%d",&num[id]); 26 down[id]=getid(i+1,j); 27 rright[id]=getid(i,j+1); 28 } 29 } 30 int main() 31 { 32 scanf("%d%d%d",&n,&m,&q); 33 init(n,m); 34 while(q--) 35 { 36 int a,b,c,d,h,w; 37 scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&h,&w); 38 int x=0,y=0,xx,yy; 39 for(int i=1;i<a;i++) 40 x=down[x]; 41 for(int i=1;i<b;i++) 42 x=rright[x]; 43 for(int i=1;i<c;i++) 44 y=down[y]; 45 for(int i=1;i<d;i++) 46 y=rright[y]; 47 xx=x;yy=y; 48 for(int i=0;i<h;i++) 49 { 50 xx=down[xx];yy=down[yy]; 51 swap(rright[xx],rright[yy]); 52 } 53 for(int i=0;i<w;i++) 54 { 55 xx=rright[xx];yy=rright[yy]; 56 swap(down[xx],down[yy]); 57 } 58 xx=x;yy=y; 59 for(int i=0;i<w;i++) 60 { 61 xx=rright[xx];yy=rright[yy]; 62 swap(down[xx],down[yy]); 63 } 64 for(int i=0;i<h;i++) 65 { 66 xx=down[xx];yy=down[yy]; 67 swap(rright[xx],rright[yy]); 68 } 69 } 70 int x=0,y; 71 for(int i=1;i<=n;i++) 72 { 73 x=down[x]; 74 y=x; 75 for(int j=1;j<=m;j++) 76 { 77 y=rright[y]; 78 printf("%d ",num[y]); 79 } 80 printf(" "); 81 } 82 }