常州模拟赛d5t3 appoint
Posted zbtrs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常州模拟赛d5t3 appoint相关的知识,希望对你有一定的参考价值。
分析:这道题比较奇葩.因为字符串没有swap函数,所以一个一个字符串交换只有30分.但是我们可以不用直接交换字符串,而是交换字符串的指针,相当于当前位置是哪一个字符串,每次交换int,可以拿60分.
对于二维问题,通常转化为一维问题去考虑,得到适当的方法再应用到二维上来,这道题如果转移到一维上就是给你一个序列,每次交换一对区间,区间不重叠,最后要求顺序输出整个序列,很显然,我们只要记录每个数旁边的数就好了,所以用链表能很快解决.转化到二维上,我们记录一个右方的链表,下方的链表,每次交换操作只需要更改四周的链表就好了.
需要注意的是char数组不能够开成2维的,题目中只告诉了字符串的总长度,因此需要转化为一维的,输出则在前一个字符串的基础上输出.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1010; int n, m, q, a[maxn][maxn], sum[maxn * maxn], r[maxn * maxn], d[maxn * maxn],tot; char s[maxn * maxn]; int pos(int down, int right) { int x = 0; while (down--) x = d[x]; while (right--) x = r[x]; return x; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n * m; i++) { scanf("%s", s + sum[i - 1] + 1); sum[i] = sum[i - 1] + strlen(s + sum[i - 1] + 1); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j] = (i - 1) * m + j; tot = n * m; for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m + 1; j++) if ((i || j) && !a[i][j]) a[i][j] = ++tot; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) { r[a[i][j]] = a[i][j + 1]; d[a[i][j]] = a[i + 1][j]; } while (q--) { int x1, y1, x2, y2, l, c; scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &l, &c); int pos1 = pos(x1 - 1, y1 - 1), Pos1 = d[r[pos1]];//先移动到方阵左上角的左上角一格 int pos2 = pos(x2 - 1, y2 - 1), Pos2 = d[r[pos2]]; for (int i = 1, p1 = d[pos1], p2 = d[pos2]; i <= l; i++, p1 = d[p1], p2 = d[p2]) //更改方阵第一列左边一列 swap(r[p1], r[p2]); for (int i = 1, p1 = r[pos1], p2 = r[pos2]; i <= c; i++, p1 = r[p1], p2 = r[p2]) swap(d[p1], d[p2]); pos1 = Pos1, pos2 = Pos2; for (int i = 1; i < c; i++) //跳到方阵最后一列 { pos1 = r[pos1]; pos2 = r[pos2]; } for (int i = 1, p1 = pos1, p2 = pos2; i <= l; i++, p1 = d[p1], p2 = d[p2]) //交换方阵最后一列 swap(r[p1], r[p2]); pos1 = Pos1, pos2 = Pos2; for (int i = 1; i < l; i++) //跳到方阵最后一行 { pos1 = d[pos1]; pos2 = d[pos2]; } for (int i = 1, p1 = pos1, p2 = pos2; i <= c; i++, p1 = r[p1], p2 = r[p2]) //交换方阵最后一行 swap(d[p1], d[p2]); } for (int i = 1, p1 = d[0]; i <= n; i++, p1 = d[p1]) { for (int j = 1, p2 = r[p1]; j <= m; j++, p2 = r[p2]) //p2千万不能写成r[0],有可能d[0]和r[0]不是同一格 { for (int k = sum[p2 - 1] + 1; k <= sum[p2]; k++) printf("%c", s[k]); printf(" "); } printf("\\n"); } return 0; }
以上是关于常州模拟赛d5t3 appoint的主要内容,如果未能解决你的问题,请参考以下文章