Character Wheels-(矩阵的按层旋转小模拟)

题目连接:https://ac.nowcoder.com/acm/contest/8688/I
博客园食用链接: https://www.cnblogs.com/lonely-wind-/p/13907129.html
You are given one n × n n \times n n×n character wheels, and it is guaranteed that n is even. Please implement these following operations:
在这里插入图片描述
1.Clockwise/Counter-clockwise rotate the x-th wheel y ( 1 ≤ y ≤ 1 0 9 ) y(1\leq y\leq10^9) y(1y109)times. Rotating one time means rotatting 90 degrees. The instruction is L/R x y, R indicates rotating clockwise and L indicates rotating counter-clockwise. For example, R 1 3 means rotate the 1-st wheel 3 times clockwise.
2.Print all the wheels, the instruction is P.

输入描述:
First line contains one integer 𝑛 ( 4 ≤ 𝑛 ≤ 50), indicates the size of the wheels.
Then followed the wheels.
After that, one line contains one integer 𝑚 ( 1 ≤ 𝑚 ≤ 100), indicates the number ofoperations.
Then followed 𝑚 lines, one line one instruction.
it is guaranteed that at least there is one P instruction.

输出描述:
If the instruction is P, then print all the wheels.

示例1
输入
4
abcd
cabe
fgha
edbe
5
L 1 1
P
R 1 3
L 2 1
P

输出
deae
cabb
bghd
acfe
ebde
abhf
eagc
dcba

emmm,就是个小模拟,一个矩阵从外到内分别为第一层到第 n 2 \frac{n}{2} 2n层,现在有m次操作,L/R x y表示将第x层旋转(逆时针/瞬时针)90度,操作P为询问现在矩阵的样子。

那么我们只需要知道坐标(i,j)经过旋转可以到达那里,那么我们带入几个数据进去算就知道顺时针旋转90度就是 ( i , j ) → ( j , n − i + 1 ) (i,j)\rightarrow (j,n-i+1) (i,j)(j,ni+1)逆时针转一次就是顺时针转3次,于是小模拟一下就完事了。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> pill;
const int mac=55;

char mp[mac][mac],tmp[mac][mac];
int n;

void print()
{
	for (int i=1; i<=n; i++)
		printf ("%s\n",mp[i]+1);
}

pill turn(int x,int y)
{
	return {y,n-x+1};
}

void solve(char dir,int r,int nb)
{
	if (dir=='L') nb=4-nb;
	for (int i=1; i<=n; i++)
		for (int j=1; j<=n; j++)
			tmp[i][j]=mp[i][j];
	for (int i=r; i<=n-r+1; i++){
		pill now={r,i},nowbo={n-r+1,i};
		for (int j=1; j<=nb; j++){
			now=turn(now.first,now.second);
			nowbo=turn(nowbo.first,nowbo.second);
		}
		tmp[now.first][now.second]=mp[r][i];
		tmp[nowbo.first][nowbo.second]=mp[n-r+1][i];
	}

	for (int i=r+1; i<=n-r; i++){
		pill now={i,r},nowbo={i,n-r+1};
		for (int j=1; j<=nb; j++){
			now=turn(now.first,now.second);
			nowbo=turn(nowbo.first,nowbo.second);
		}
		tmp[now.first][now.second]=mp[i][r];
		tmp[nowbo.first][nowbo.second]=mp[i][n-r+1];
	}

	for (int i=1; i<=n; i++) 
		for (int j=1; j<=n; j++)
			mp[i][j]=tmp[i][j];
}

int main(int argc, char const *argv[])
{
	scanf ("%d",&n);
	for (int i=1; i<=n; i++)
		scanf ("%s",mp[i]+1);
	int m;
	scanf ("%d",&m);
	while (m--){
		char op[10];
		scanf ("%s",op);
		if (op[0]=='P') print();
		else {
			int r,nb;
			scanf ("%d%d",&r,&nb);
			nb%=4;
			solve(op[0],r,nb);
		}
	}
	return 0;
}
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页