3 seconds
256 megabytes
standard input
standard output
The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement.
UCDHP stores some secret information about meteors as an n × m table with integers in its cells. The order of meteors in the Universe
is changing. That's why the main UCDHP module receives the following queries:
- The query to swap two table rows;
- The query to swap two table columns;
- The query to obtain a secret number in a particular table cell.
As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.
The first line contains three space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 500000)
— the number of table columns and rows and the number of queries, correspondingly.
Next n lines contain m space-separated
numbers each — the initial state of the table. Each number p in the table is an integer and satisfies the inequality 0 ≤ p ≤ 106.
Next k lines contain queries in the format "si xi yi",
where si is
one of the characters "с", "r"
or "g", and xi, yi are
two integers.
-
If si =
"c", then the current query is the query to swap columns with indexes xi and yi (1 ≤ x, y ≤ m, x ≠ y); -
If si =
"r", then the current query is the query to swap rows with indexes xi and yi (1 ≤ x, y ≤ n, x ≠ y); -
If si =
"g", then the current query is the query to obtain the number that located in the xi-th
row and in the yi-th
column (1 ≤ x ≤ n, 1 ≤ y ≤ m).
The table rows are considered to be indexed from top to bottom from 1 to n, and the table columns — from left to right from 1 to m.
For each query to obtain a number (si =
"g") print the required number. Print the answers to the queries in the order of the queries in the input.
3 3 5 1 2 3 4 5 6 7 8 9 g 3 2 r 3 2 c 2 3 g 2 2 g 3 2
8 9 6
2 3 3 1 2 4 3 1 5 c 2 1 r 1 2 g 1 3
5
Let's see how the table changes in the second test case.
After the first operation is fulfilled, the table looks like that:
2 1 4
1 3 5
After the second operation is fulfilled, the table looks like that:
1 3 5
2 1 4
So the answer to the third query (the number located in the first row and in the third column) will be 5.
解题说明:此题的意思很明确,就是给你一个矩阵,进行行列变换,输出某个位置的值。最简单的想法是每一个操作过来对矩阵做一次变换,不过显然在操作数目很多的情况下极容易超时。为此,应该考虑在变换的时候并不是真的对矩阵进行处理,而是记录下矩阵的交换信息,在输出矩阵中某个值的时候才根据交换信息进行输出。用一个二维数组存矩阵,两个一维数组分别存放行列变换信息,交换时只改变一维数组中的值,输出时用再从一维数组中去查找即可。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <algorithm> using namespace std; int a[1001][1001]; int h[1001],l[1001]; int main() { int n,m,k,i,j; char op; int pos1,pos2; int temp; scanf("%d %d %d",&n,&m,&k); for(i=0;i<1001;i++) { h[i]=i; l[i]=i; } for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<k;i++) { getchar(); scanf("%c %d %d",&op,&pos1,&pos2); pos1--; pos2--; if(op=='g') { printf("%d\n",a[h[pos1]][l[pos2]]); } else if(op=='c') { temp=l[pos1]; l[pos1]=l[pos2]; l[pos2]=temp; } else if(op=='r') { temp=h[pos1]; h[pos1]=h[pos2]; h[pos2]=temp; } } return 0; }