HDU 5671 Matrix
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5671
题目大意:给你一个n行m列的矩阵
进行四种操作 交换x与y行
交换x列与y列
给x行都加上y
给x列都加上y
输出进行操作后的矩阵
解题思路:一开始想用二维树状数组做,后来发现并不用,因为每次都是一行一列加,所以用两个数组保存行列加的值就可以了
一共开四个数组 hang[]表示当前矩阵第i行是最初矩阵的hang[i]行
lie[]表示当前矩阵i列是最初矩阵的lie[i]列
初始值 hang[1]=1 hang[2]=2 hang[3]=3
比如说交换 1 2 行 再交换 2 3 行
那么hang[1]=2 hang[2]=3 hang[3]=1
也就是说相对于一开始 第一行在1,第二行在2,第三行在3。
现在在第一行的是最初的第二行,现在在第二行的是最初的第三行,现在在第三行的是最初的第一行
hangpoi[]用来记录给最初矩阵某一行加上了多少
liepoi[]用来记录给矩阵某一列加上了多少
一般给x行加上y就是这样
hangpoi[hang[x]]+=y
因为题目想要加上y的是第x行,而我们知道当前的第x行其实是最初的矩阵的第hang[x]行,我们要记录给最初矩阵加上多少。
最后输出时候我们知道i行j列
是最初矩阵的a[hang[i] ][ lie[j] ]列,最初矩阵的i行加上了hangpoi[hang[i]],最初矩阵的j列加上了liepoi[lie[j]]
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
#define RI(N) scanf("%d",&(N))
#define RII(N,M) scanf("%d %d",&(N),&(M))
#define RIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define mem(a) memset((a),0,sizeof(a))
using namespace std;
const int inf=1e9;
const int inf1=-1*1e9;
typedef long long LL;
int a[1005][1005];
int main()
{
int t;
RI(t);
while(t--)
{
int n,m,q;
RIII(n,m,q);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
RI(a[i][j]);
}
int hang[1005];
int hangpoi[1005];
int lie[1005];
int liepoi[1005];
for(int i=1; i<=n; i++)
hang[i]=i;
for(int j=1; j<=m; j++)
lie[j]=j;
mem(hangpoi);
mem(liepoi);
for(int i=0; i<q; i++)
{
int a,x,y;
RIII(a,x,y);
if(a==1)
{
swap(hang[x],hang[y]);
}
if(a==2)
{
swap(lie[x],lie[y]);
}
if(a==3)
{
hangpoi[hang[x]]+=y;
}
if(a==4)
{
liepoi[lie[x]]+=y;
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<m; j++)
{
printf("%d ",a[hang[i]][lie[j]]+hangpoi[hang[i]]+liepoi[lie[j]]);
}
printf("%d\n",a[hang[i]][lie[m]]+hangpoi[hang[i]]+liepoi[lie[m]]);
}
}
return 0;
还没有评论,来说两句吧...