Matrix

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 271    Accepted Submission(s): 126


Problem Description
There is a matrix M that has n rows and m columns (1n1000,1m1000) .Then we perform q(1q100,000) operations:

1 x y: Swap row x and row y (1x,yn) ;

2 x y: Swap column x and column y (1x,ym) ;

3 x y: Add y to all elements in row x (1xn,1y10,000) ;

4 x y: Add y to all elements in column x (1xm,1y10,000) ;
 

 

Input
There are multiple test cases. The first line of input contains an integer T(1T20) indicating the number of test cases. For each test case:

The first line contains three integers n , m and q .
The following n lines describe the matrix M.(1Mi,j10,000) for all (1in,1jm) .
The following q lines contains three integers a(1a4) , x and y .
 

 

Output
For each test case, output the matrix M after all q operations.
 

 

Sample Input
2
3 42
1 2 3 4
2 3 4 5
3 4 5 6
1 1 2
3 1 10
2 2 2
1 10
10 1
1 1
2 2
1 2
 

 

Sample Output
12 13 14 15
1 2 3 4
3 4 5 6
1 10
10 1
Hint
Recommand to use scanf and printf
 

 

Source
 
题意:对矩阵执行q次  4种类型的操作 输出 最终矩阵
 
题解:

对于交换行、交换列的操作,分别记录当前状态下每一行、每一列是原始数组的哪一行、哪一列即可。

对每一行、每一列加一个数的操作,也可以两个数组分别记录。注意当交换行、列的同时,也要交换增量数组。

输出时通过索引找到原矩阵中的值,再加上行、列的增量

 1  #include<iostream>
 2  #include<cstring>
 3  #include<cstdio>
 4  #include<queue>
 5  #include<stack>
 6  #include<map>
 7  #include<set>
 8  #include<algorithm>
 9  #define LL __int64
10  #define pi acos(-1.0)
11  #define mod 1
12  #define maxn 10000
13  using namespace std;
14 int t;
15 int mp[1005][1005] ;
16 int n,m,q;
17 int a,x,y;
18 int l[1005],h[1005];
19 int ladd[1005],hadd[1005];
20 int main()
21 {
22     scanf("%d",&t);
23     for(int i=1;i<=t;i++)
24     {
25         scanf("%d %d %d",&n,&m,&q);
26         for(int j=1;j<=n;j++)
27          for(int k=1;k<=m;k++)
28           scanf("%d",&mp[j][k]);
29           for(int j=1;j<=n;j++)
30           {
31           h[j]=j;hadd[j]=j;
32           }
33           for(int j=1;j<=m;j++)
34           {
35           l[j]=j; ladd[j]=0;
36           }
37           memset(hadd,0,sizeof(hadd));
38           memset(ladd,0,sizeof(ladd));
39           int t;
40           for(int j=1;j<=q;j++)
41           {
42                scanf("%d %d %d",&a,&x,&y);
43                if(a==1)
44                {
45                    t=h[y];
46                 h[y]=h[x];
47                    h[x]=t;
48              }
49                else
50                if(a==2)
51                {
52                    t=l[y];
53                 l[y]=l[x];
54                    l[x]=t;
55                }
56                else
57                if(a==3)
58                {
59                    hadd[h[x]]+=y;
60               }
61                else
62                   ladd[l[x]]+=y;
63           }
64           for(int j=1;j<=n;j++)
65            {
66                printf("%d",mp[h[j]][l[1]]+hadd[h[j]]+ladd[l[1]]);
67              for(int k=2;k<=m;k++)
68            {
69                    printf(" %d",mp[h[j]][l[k]]+hadd[h[j]]+ladd[l[k]]);
70            }
71            printf("\n");
72            }
73     }
74 return 0; 
75 }