题目连接
http://acm.xidian.edu.cn/problem.php?cid=1018&pid=0
Description
给定一个n*m的矩阵,要求支持下面的操作:
0 x y:交换第x行与第y行
1 x y:交换第x列与第y列
Input
多组数据。对于每组测试数据,第一行包含三个数n,m,k(1<=n,m<=1000;1<=k<=100000),分别表示行数,列数,操作数。
接下来n行,每行m个整数,表示初始的矩阵,矩阵中每个数小于10^9。
接下来k行每行一个操作,格式如题目所述。
Output
对于每组数据,输出完成所有操作后的矩阵。
Sample Input
3 2 2
1 2
3 4
5 6
0 1 3
1 1 2
Sample Output
6 5
4 3
2 1
Hint
题意
就是如题所示,按要求交换行列
题解
虽然是道很水很水题题,但是自己做的方法太蠢了,一直暴时间,不应该傻傻地交换行列的元素,就直接交换行列下标码就可以了
代码
#include<bits/stdc++.h>
using namespace std;
#define max 1000
typedef pair<int,int>p;
#define INF 1e9+7
#define ll long long
#define eps 1e-6
int a[1010][1010];
int row[1010];
int col[1010];
int main(int argc, char const *argv[])
{
int n,m,k;
cin>>n>>m>>k;
for (int i = 1; i <=n; ++i)
{
for (int j = 1; j <=m; ++j)
{
cin>>a[i][j];
}
}
for (int i = 1; i <=n; ++i)row[i]=i;
for (int i = 1; i <=m; ++i)col[i]=i;
int tmp;
for (int i = 1; i <=k; ++i)
{
int t,p,q;
cin>>t>>p>>q;
if (t==0)
{
tmp=row[p];
row[p]=row[q];
row[q]=tmp;
}
if (t==1)
{
tmp=col[p];
col[p]=col[q];
col[q]=tmp;
}
}
for (int i = 1; i <=n; ++i)
{
for (int j = 1; j <=m; ++j)
{
printf("%d",a[row[i]][col[j]] );//注意这里的打印方法
}
printf("\n");
}
return 0;
}