cf 222b 【数组行列交换操作】.cpp

题意:

  给出n行m列的数组,并给出k个操作;

  其中操作方法有三种:

  ① c x y 表示把数组的第x列和第y列交换

  ② r x y 表示把数组的第x行和第y行交换

  ③ g x y 表示求数组的第x行第y列的数

 

思路:

  用一个数组c 和 r表示第c行和第r列当前值是实际数组中的第几行第几列

 

Tips:

  因为n m k给的值都很大

  所以如果按暴力模拟的话,即一个一个交换就会超时

  所以想到用一个虚拟的数组保存当前数组的每一行每一列在原数组中的哪一行哪一列

Code:

View Code
 1 #include <stdio.h>
 2 #include <cstring>
 3 #include <ctime>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int arr[1010][1010];
 8 int main()
 9 {
10     int n, m, k;
11     int r[1010], c[1010];
12     int tx, ty;
13     char ord;
14     while(EOF != scanf("%d %d %d", &n, &m, &k)) {
15         for(int i = 1; i <= n; ++i) r[i] = i;
16         for(int i = 1; i <= m; ++i) c[i] = i;
17 
18         for(int i = 1; i <= n; ++i)
19             for(int j = 1; j <= m; ++j)
20                 scanf("%d", &arr[i][j]);
21         for(int i = 1; i <= k; ++i) {
22             scanf("%*c%c %d %d", &ord, &tx, &ty);
23             if(ord == 'c') {
24                 swap(c[tx], c[ty]);
25             } else if(ord == 'r') {
26                 swap(r[tx], r[ty]);
27             } else printf("%d\n", arr[r[tx]][c[ty]]);
28         }
29     }
30     return 0;
31 }

 

链接:http://www.codeforces.com/problemset/problem/222/B

posted @ 2013-03-16 21:18  Griselda.  阅读(293)  评论(0编辑  收藏  举报