题解
[1007] 魔法少女小Scarlet
1 条题解
-
0
1. 解题思路
按圈分层处理每次旋转;每层枚举一条边上的偏移,将上、右、下、左四个对应位置循环交换。
**关键规则:**每层边长偏移只枚举0到side-1,避免四个角重复;z=0顺时针,z=1逆时针。
2. 坑点和注意事项
- 数据范围:1≤n,m≤500,旋转方阵始终在原矩阵内。
- 每层边长偏移只枚举0到side-1,避免四个角重复;z=0顺时针,z=1逆时针。
- 保留实际正式提交的原地四点交换及缓冲输入输出,避免改回另一个旧std;FastIO为已有性能实现,数组固定且非负整数输入满足读入前提。
3. 复杂度
- **时间复杂度:**O(n² + Σr²),最坏O(mn²)。
- **空间复杂度:**O(n²)。
/* 思路:按圈分层处理每次旋转;每层枚举一条边上的偏移,将上、右、下、左四个对应位置循环交换。 关键规则:每层边长偏移只枚举0到side-1,避免四个角重复;z=0顺时针,z=1逆时针。 数据范围:1≤n,m≤500,旋转方阵始终在原矩阵内;注意数组下标和中间运算的数据类型。 时间复杂度:O(n² + Σr²),最坏O(mn²);空间复杂度:O(n²)。 */ #include <algorithm> #include <cstdio> using namespace std; static int board[501][501]; class FastIO { static constexpr int SIZE = 1 << 16; char input[SIZE], output[SIZE]; int position = 0, length = 0, used = 0; public: int next() { int value = 0; char ch; do { if (position == length) { length = (int)fread(input, 1, SIZE, stdin); position = 0; } ch = input[position++]; } while (ch < '0' || ch > '9'); do { value = value * 10 + ch - '0'; if (position == length) { length = (int)fread(input, 1, SIZE, stdin); position = 0; if (!length) return value; } ch = input[position++]; } while (ch >= '0' && ch <= '9'); return value; } void put(char ch) { output[used++] = ch; if (used == SIZE) flush(); } void number(int value) { char digits[12]; int count = 0; do { digits[count++] = char('0' + value % 10); value /= 10; } while (value); while (count) put(digits[--count]); } void flush() { fwrite(output, 1, used, stdout); used = 0; } }; int main() { static FastIO io; const int n = io.next(); const int m = io.next(); // [1007] 魔法少女小Scarlet:每层边长偏移只枚举0到side-1,避免四个角重复;z=0顺时针,z=1逆时针。 for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) board[i][j] = (i - 1) * n + j; for (int operation = 0; operation < m; ++operation) { const int x = io.next(), y = io.next(), radius = io.next(), direction = io.next(); for (int layer = 0; layer < radius; ++layer) { const int top = x - radius + layer, bottom = x + radius - layer; const int left = y - radius + layer, right = y + radius - layer; const int side = right - left; if (direction == 0) { for (int offset = 0; offset < side; ++offset) { const int saved = board[top][left + offset]; board[top][left + offset] = board[bottom - offset][left]; board[bottom - offset][left] = board[bottom][right - offset]; board[bottom][right - offset] = board[top + offset][right]; board[top + offset][right] = saved; } } else { for (int offset = 0; offset < side; ++offset) { const int saved = board[top][left + offset]; board[top][left + offset] = board[top + offset][right]; board[top + offset][right] = board[bottom][right - offset]; board[bottom][right - offset] = board[bottom - offset][left]; board[bottom - offset][left] = saved; } } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (j > 1) io.put(' '); io.number(board[i][j]); } io.put('\n'); } io.flush(); return 0; }
- 1