HDU 5671 Matrix
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5671
题目翻译
有一个n行m列的矩阵,在这个矩阵上进行q个操作:
1 x y: 交换矩阵M的第x行和第y行
2 x y: 交换矩阵M的第x列和第y列
3 x y: 对矩阵M的第x行的每一个数加上y
4 x y: 对矩阵M的第x列的每一个数加上y
题解
首先我们发现,对行和列做操作是不会造成行列之间的影响的。
每一行/每一列是一个整体,相互之间不影响。
所以我们只需要对行的增量和列的增量记录一下,然后再记录一下每一个行/列原来的位置即可。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
//while(true) RP++; #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int Maxn=1000; int addRow[Maxn+5],addColumn[Maxn+5]; int recRow[Maxn+5],recColumn[Maxn+5]; int matrix[Maxn+5][Maxn+5]; int m,n,q; inline void solve(int T){ memset(addRow,0,sizeof(addRow)); memset(addColumn,0,sizeof(addColumn)); memset(recRow,0,sizeof(recRow)); memset(recColumn,0,sizeof(recColumn)); memset(matrix,0,sizeof(matrix)); scanf("%d%d%d",&n,&m,&q); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) scanf("%d",&matrix[i][j]); for (int i=1;i<=n;i++) recRow[i]=i; for (int j=1;j<=m;j++) recColumn[j]=j; for (int i=1;i<=q;i++){ int op,x,y; scanf("%d%d%d",&op,&x,&y); switch(op){ case 1: swap(addRow[x],addRow[y]); swap(recRow[x],recRow[y]); break; case 2: swap(addColumn[x],addColumn[y]); swap(recColumn[x],recColumn[y]); break; case 3: addRow[x]+=y; break; case 4: addColumn[x]+=y; break; } } for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ printf("%d",addRow[i]+addColumn[j]+matrix[recRow[i]][recColumn[j]]); if (j!=m) printf(" "); } printf("\n"); } } int main(){ int T=0; while(scanf("%d",&T)!=EOF) for (int i=1;i<=T;i++) solve(i); return 0; } |

原文链接:HDU 5671 Matrix
WNJXYKの博客 版权所有,转载请注明出处。
还没有任何评论,你来说两句吧!