首页 > 代码库 > 2016弱校联盟十一专场10.2部分题解

2016弱校联盟十一专场10.2部分题解

1/10

J. Matrix Transformation

技术分享
  1 /*zhen hao*/  2 #include <bits/stdc++.h>  3 using namespace std;  4   5 #define lson l, m, rt*2  6 #define rson m + 1, r, rt*2+1  7 #define xx first  8 #define yy second  9  10 typedef long long LL; 11 typedef unsigned long long ULL; 12 typedef pair<int,int> pii; 13  14 const int N = 210; 15  16 struct Node { 17   int right, down, val; 18 } nodes[N * N]; 19  20 int n, Q, tmp[N], tmp2[N]; 21  22 int get_id(int x, int y) { 23   return x * (n + 1) + y; 24 } 25  26 inline int walk(int p, int x, int y) { 27   while (x--) p = nodes[p].right; 28   while (y--) p = nodes[p].down; 29   return p; 30 } 31  32 int main() { 33 #ifdef JUDGE 34   freopen("case.in", "r", stdin); 35 //  freopen("case.out", "w", stdout); 36 #endif 37   while (~scanf("%d%d", &n, &Q)) { 38     int cnt = 0; 39     for (int i = 0; i <= n; i++) 40       for (int j = 0; j <= n; j++) { 41         if (i != n) nodes[get_id(i, j)].down = get_id(i + 1, j); 42         if (j != n) nodes[get_id(i, j)].right = get_id(i, j + 1); 43         if (i && j) nodes[get_id(i, j)].val = cnt++; 44       } 45     while (Q--) { 46       int t, l, r, d, p, q, o, tp, tq; 47       scanf("%d%d%d%d", &t, &l, &r, &d); 48       if (d == 0) continue; 49       l++; r++; 50       if (t == 1) { 51         tp = p = walk(0, 0, l - 1); 52         q = walk(p, d, 0), o = walk(q, n - d, 0); 53         for (int i = l; i <= r; i++) { 54           p = walk(p, 0, 1); 55           q = walk(q, 0, 1); 56           o = walk(o, 0, 1); 57           nodes[o].right = nodes[p].right; 58           nodes[p].right = nodes[q].right; 59         } 60         p = tp; 61         tq = q = walk(p, 0, r - l + 1); 62         for (int i = 1; i <= n; i++) { 63           p = walk(p, 1, 0); 64           q = walk(q, 1, 0); 65           tmp[i] = nodes[p].down; 66           tmp2[i] = nodes[q].down; 67         } 68         p = tp; 69         q = tq; 70         for (int i = 1; i <= n; i++) { 71           p = walk(p, 1, 0); 72           q = walk(q, 1, 0); 73           nodes[p].down = tmp[i + d <= n ? i + d : i + d - n]; 74           nodes[q].down = tmp2[i - d >= 1 ? i - d : i - d + n]; 75         } 76       } 77       else { 78         tp = p = walk(0, l - 1, 0); 79         q = walk(p, 0, d), o = walk(q, 0, n - d); 80         for (int i = l; i <= r; i++) { 81           p = walk(p, 1, 0); 82           q = walk(q, 1, 0); 83           o = walk(o, 1, 0); 84           nodes[o].down = nodes[p].down; 85           nodes[p].down = nodes[q].down; 86         } 87         p = tp; 88         tq = q = walk(p, r - l + 1, 0); 89         for (int i = 1; i <= n; i++) { 90           p = walk(p, 0, 1); 91           q = walk(q, 0, 1); 92           tmp[i] = nodes[p].right; 93           tmp2[i] = nodes[q].right; 94         } 95         p = tp; 96         q = tq; 97         for (int i = 1; i <= n; i++) { 98           p = walk(p, 0, 1); 99           q = walk(q, 0, 1);100           nodes[p].right = tmp[i + d <= n ? i + d : i + d - n];101           nodes[q].right = tmp2[i - d >= 1 ? i - d : i - d + n];102         }103       }104     }105     int p = 0, q;106     for (int i = 1; i <= n; i++) {107       q = p = nodes[p].down;108       bool first = 0;109       for (int j = 1; j <= n; j++) {110         q = nodes[q].right;111         printf("%d", nodes[q].val);112         if (j != n) putchar( );113       }114       puts("");115     }116   }117   return 0;118 }119 /*120 题意:给你一个n * n的矩阵,有两种操作:121 1、1 l r d 表示将l到r行顺时针转d距离122 2、2 l r d 表示将l到r列顺时针转d距离123 让你输出最后的矩阵124 题解:125 用十字链表126 具体是建立一个结构体作为一个结点,维护下,右和值127 然后记得每次只是将原来指向某元素的指针赋给新的指向该元素的指针即可128 最后输出矩阵129 */
代码君

 

2016弱校联盟十一专场10.2部分题解