Description
There is a matrix $M$ that has $n$ rows and $m$ columns $(1 \leq n \leq 1000 ,1 \leq m \leq 1000 )$.Then we perform $q (1 \leq q \leq 100,000)$ operations:
1 x y: Swap row x and row y $(1 \leq x,y \leq n)$;
2 x y: Swap column x and column y $(1 \leq x,y \leq m)$;
3 x y: Add y to all elements in row x $(1 \leq x \leq n,1 \leq y \leq 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 y: Swap row x and row y $(1 \leq x,y \leq n)$;
2 x y: Swap column x and column y $(1 \leq x,y \leq m)$;
3 x y: Add y to all elements in row x $(1 \leq x \leq n,1 \leq y \leq 10,000)$;
4 x y: Add y to all elements in column x $(1 \leq x \leq m,1 \leq y \leq 10,000)$;
Input
There are multiple test cases. The first line of input contains an integer $T (1\leq T\leq 20)$ 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.$(1 \leq M_{i,j} \leq 10,000)$ for all $(1 \leq i \leq n,1 \leq j \leq m)$.
The following $q$ lines contains three integers $a(1 \leq a \leq 4)$, $x$ and $y$.
The first line contains three integers $n$, $m$ and $q$.
The following $n$ lines describe the matrix M.$(1 \leq M_{i,j} \leq 10,000)$ for all $(1 \leq i \leq n,1 \leq j \leq m)$.
The following $q$ lines contains three integers $a(1 \leq a \leq 4)$, $x$ and $y$.
Output
For each test case, output the matrix $M$ after all $q$ operations.
Sample Input
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
分析:
刚开始直接用的暴力,在BestCoder上侥幸也过了,但是现在自己做的时候就超时了,所以为了做到不超时,显而易见,我们不能把每一次操作通通执行一遍,而是去记录每行每列都发生了什么操作,然后在最后一次计算一遍就可以了,而要做到这一点,我们需要数组pr[]记录后来的每一行分别对应最初的第几行,数组pc[]记录后来的每一列对应最初的第几列,而对x行或x列执行的加法操作,就是对最初的pr[x]行或pc[x]列执行加法操作暴力求解:
#include<stdio.h>
int a[1100][1100];
int main()
{
int n,m,t,k,q,p,x,y,i,j;
scanf("%d",&k);
while(k--)
{
scanf("%d%d%d",&n,&m,&q);
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
scanf("%d",&a[i][j]);
for(j=0; j<q; j++)
{
scanf("%d%d%d",&p,&x,&y);
if(p==1)
{
for(i=1; i<=m; i++)
{
t=a[x][i];
a[x][i]=a[y][i];
a[y][i]=t;
}
}
else if(p==2)
{
for(i=1; i<=n; i++)
{
t=a[i][x];
a[i][x]=a[i][y];
a[i][y]=t;
}
}
else if(p==3)
{
for(i=1; i<=m; i++)
a[x][i]+=y;
}
else
for(i=1; i<=n; i++)
a[i][x]+=y;
}
for(i=1; i<=n; i++)
{
for(j=1; j<m; j++)
printf("%d ",a[i][j]);
printf("%d\n",a[i][m]);
}
}
return 0;
}
非暴力求解:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1100][1100],pr[1100],pc[1100];
struct A
{
int sum;
} r[1100],c[1100];
int main()
{
int n,m,t,k,q,p,x,y,i,j;
scanf("%d",&k);
while(k--)
{
memset(r,0,sizeof(r));
memset(c,0,sizeof(c));
scanf("%d%d%d",&n,&m,&q);
for(i=1; i<=n; i++)
{
pr[i]=i; //把行号标记一下,便于最后计算
for(j=1; j<=m; j++)
{
pc[j]=j;//列号标记一下,便于计算
scanf("%d",&a[i][j]);
}
}
for(j=0; j<q; j++)
{
scanf("%d%d%d",&p,&x,&y);
if(p==1)
swap(pr[x],pr[y]); 吧对应的行号交换一下
else if(p==2)
swap(pc[x],pc[y]); //把对应的列号交换一下
else if(p==3)
r[pr[x]].sum+=y; //把第x行加的值放入结构体中
else
c[pc[x]].sum+=y;//把第x行加的值放入结构体中
}
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
printf("%d%c",a[pr[i]][pc[j]]+r[pr[i]].sum+c[pc[j]].sum,j!=m?' ':'\n');
//输入最终的结果,交换后的值再加上需要加的值,没有加法处理的值加的是0
}
return 0;
}