HDU 5671 Matrix (矩阵操作)

来源:互联网 发布:警官证在淘宝上怎么搜 编辑:程序博客网 时间:2024/05/18 02:10

Matrix 


 Time Limit: 3000/1500 MS (Java/Others)   Memory Limit: 131072/131072 K (Java/Others)


Problem Description
There is a matrix MM that has nn rows and mm columns (1 \leq n \leq 1000 ,1 \leq m \leq 1000 )(1≤n≤1000,1≤m≤1000).Then we perform q (1 \leq q \leq 100,000)q(1≤q≤100,000) operations:


1 x y: Swap row x and row y (1 \leq x,y \leq n)(1≤x,y≤n);


2 x y: Swap column x and column y (1 \leq x,y \leq m)(1≤x,y≤m);


3 x y: Add y to all elements in row x (1 \leq x \leq n,1 \leq y \leq 10,000)(1≤x≤n,1≤y≤10,000);


4 x y: Add y to all elements in column x (1 \leq x \leq m,1 \leq y \leq 10,000)(1≤x≤m,1≤y≤10,000);


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


The first line contains three integers nn, mm and qq. The following nn lines describe the matrix M.(1 \leq M_{i,j} \leq 10,000)(1≤M
​i,j
​​ ≤10,000) for all (1 \leq i \leq n,1 \leq j \leq m)(1≤i≤n,1≤j≤m). The following qq lines contains three integers a(1 \leq a \leq 4)a(1≤a≤4), xx and yy.


Output
For each test case, output the matrix MM after all qq operations.


Sample Input
Copy
2
3 4 2
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 

Problem Description
There is a matrix MM that has nn rows and mm columns (1 \leq n \leq 1000 ,1 \leq m \leq 1000 )(1≤n≤1000,1≤m≤1000).Then we perform q (1 \leq q \leq 100,000)q(1≤q≤100,000) operations:


1 x y: Swap row x and row y (1 \leq x,y \leq n)(1≤x,y≤n);


2 x y: Swap column x and column y (1 \leq x,y \leq m)(1≤x,y≤m);


3 x y: Add y to all elements in row x (1 \leq x \leq n,1 \leq y \leq 10,000)(1≤x≤n,1≤y≤10,000);


4 x y: Add y to all elements in column x (1 \leq x \leq m,1 \leq y \leq 10,000)(1≤x≤m,1≤y≤10,000);


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


The first line contains three integers nn, mm and qq. The following nn lines describe the matrix M.(1 \leq M_{i,j} \leq 10,000)(1≤M
​i,j
​​ ≤10,000) for all (1 \leq i \leq n,1 \leq j \leq m)(1≤i≤n,1≤j≤m). The following qq lines contains three integers a(1 \leq a \leq 4)a(1≤a≤4), xx and yy.


Output
For each test case, output the matrix MM after all qq operations.


Sample Input
Copy
2
3 4 2
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 


题解思路:

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

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

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

复杂度O(q+mn)O(q+mn)




超时TLE代码(当天比赛的时候提交还过了,比赛后又重判了,TLE ,看样子比赛的时候的数据水啊)

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;long long int Map[1100][1100];int n,m;void calculate1(int x,int y){for(int i = 0;i < m;i++){swap(Map[x][i],Map[y][i]);}}void calculate2(int x,int y){for(int i = 0;i < n;i++){swap(Map[i][x],Map[i][y]);}}void calculate3(int x,int y){for(int i = 0;i < m;i++){Map[x][i] += y;}}void calculate4(int x,int y){for(int i = 0;i < n;i++){Map[i][x] += y;}}int main(){int p,t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&p);for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){scanf("%I64d",&Map[i][j]);}}int a;int x,y;while(p--){scanf("%d%d%d",&a,&x,&y);switch(a){case 1 : calculate1(x-1,y-1);break;case 2 : calculate2(x-1,y-1);break;case 3 : calculate3(x-1,y);break;case 4 : calculate4(x-1,y);break;}}for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){if(j != 0)printf(" ");printf("%I64d",Map[i][j]);}printf("\n");}}return 0;} 


今天又重新写了一遍,AC代码

#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>using namespace std;long long int  Map[1100][1100],row[1100],col[1100];   //  row[x] 表示 应该在第 x 行的在第 row[x] 行 ,col同理表示列int n,m;void Init(){for(int i = 0;i < 1100;i++){row[i] = i;col[i] = i;}}void calculate3(int x,int y){for(int i = 0;i < m;i++){Map[x][i] += y;}}void calculate4(int x,int y){for(int i = 0;i < n;i++){Map[i][x] += y;}}int main(){int p,t;scanf("%d",&t);while(t--){Init();           //   初始化 ,最初的时候,都对应自己的行自己的列scanf("%d%d%d",&n,&m,&p);for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){scanf("%I64d",&Map[i][j]);}}int a;int x,y;while(p--){scanf("%d%d%d",&a,&x,&y);switch(a){case 1 : swap(row[x-1],row[y-1]);break; //  交换行的时候,只把标记改下就行case 2 : swap(col[x-1],col[y-1]);break;case 3 : calculate3(row[x-1],y);break;case 4 : calculate4(col[x-1],y);break;}}for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){if(j != 0)printf(" ");printf("%I64d",Map[row[i]][col[j]]);}printf("\n");}}return 0;} 


0 0