HDU 5671 Matrix

题目链接:

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5671

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=692&pid=1001

题解:

由于都是整行整列操作的,所以可以吧行列分开讨论,最后再一一映射求出变化之后每个点的值。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring> 
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1010;
 8 
 9 int n, m, q;
10 
11 LL mat[maxn][maxn];
12 int x[maxn], y[maxn];
13 LL addx[maxn], addy[maxn];
14 LL ans[maxn][maxn];
15 
16 void init() {
17     for (int i = 1; i <= n; i++) {
18         x[i] = i;
19     }
20     for (int j = 1; j <= m; j++) {
21         y[j] = j;
22     }
23     memset(addx, 0, sizeof(addx));
24     memset(addy, 0, sizeof(addy));
25 }
26 
27 int main() {
28     int tc;
29     scanf("%d", &tc);
30     while (tc--) {
31         scanf("%d%d%d", &n, &m, &q);
32         init();
33         for (int i = 1; i <= n; i++) {
34             for (int j = 1; j <= m; j++) {
35                 scanf("%lld", &mat[i][j]);
36             }
37         }
38         while (q--) {
39             int cmd, xx, yy;
40             scanf("%d%d%d", &cmd, &xx, &yy);
41             if (cmd == 1) {
42                 swap(x[xx], x[yy]);
43                 swap(addx[xx], addx[yy]);
44             }
45             else if (cmd == 2) {
46                 swap(y[xx], y[yy]);
47                 swap(addy[xx], addy[yy]);
48             }
49             else if (cmd == 3) {
50                 addx[xx] += yy;
51             }
52             else if (cmd == 4) {
53                 addy[xx] += yy;
54             }
55         }
56         for (int i = 1; i <= n; i++) {
57             for (int j = 1; j <= m; j++) {
58                 ans[i][j] = mat[x[i]][y[j]] + addx[i] + addy[j];
59             }
60         }
61         for (int i = 1; i <= n; i++) {
62             printf("%lld", ans[i][1]);
63             for (int j = 2; j <= m; j++) {
64                 printf(" %lld", ans[i][j]);
65             }
66             printf("\n");
67         }
68     }
69     return 0;
70 }

 

posted @ 2016-04-23 15:26  fenicnn  阅读(139)  评论(0编辑  收藏  举报