Matrix Operation
Description
You are given a matrix M of type 1234x5678. It is initially filled with integers 1...1234x5678 in row major
order. Your task is to process a list of commands manipulating M. There are 4 types of commands:
"R x y" swap the xth and yth row of M; 1<=x, y<=1234.
"C x y" swap the xth and yth column of M; 1<=x, y<=5678.
"Q x y" write out M(x, y); 1<=x<=1234.1<=y<=5678.
"W z" write out x and y where z=M(x, y). 1<=z<=7006652 (1234 * 5678)
Input
The input file contains several test cases. The first line is N: the number of test cases. Then follows N lines.
A list of valid commands.1 <= N <= 10000.
Output
For each "Q x y" write out one line with the current value of M(x, y), for each "W z" write out one line with
the value of x and y (described as above) separated by a space.
Sample Input
10
R 1 2
Q 1 1
Q 2 1
W 1
W 5679
C 1 2
Q 1 1
Q 2 1
W 1
W 5679
Sample Output
5679
1
2
1
1 1
5680
2
2 2
1 2
HINT
解题报告
此题的意思是一个1234*5678,的矩阵,矩阵中的元素从1排到1234*5678。然后进行行列交换和查询,本题只要开4个数组记录相应的值即可,然后利用相应的公式计算出需要查询的值或者坐标即可,因为涉及到字符,所以请注意去除换行符。代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1234],ax[1234],b[5678],by[5678];
int main()
{
int n,i;
int x,y,num;
char c[10],m;
scanf("%d",&n);
for (i=0;i<1234;i++)
{
a[i]=ax[i]=i;
}
for(i=0;i<5678;i++)
{
b[i]=by[i]=i;
}
while(n--)
{
scanf("%s",c);
m=c[0];
if(m=='R')
{
scanf("%d%d",&x,&y);
swap(ax[a[x -1]],ax[a[y - 1]]);
swap(a[x-1],a[y-1]);
}
else if(m=='C')
{
scanf("%d%d",&x,&y);
swap(by[b[x-1]],by[b[y-1]]);
swap(b[x-1],b[y-1]);
}
else if(m=='Q')
{
scanf("%d%d",&x,&y);
printf("%d\n",a[x-1]*5678+b[y-1]+1);
}
else if(m=='W')
{
scanf("%d", &num);
printf("%d %d\n",ax[(num-1)/5678]+1,by[(num-1)%5678]+1);
}
}
return 0;
}