【作业】Mental Rotation (模拟)
题目链接:https://vjudge.net/contest/345791#problem/L
【问题描述】
Mental rotation is a difficult thing to master. Mental rotation is the ability to imagine in your mind how an object would look like from a viewer's side if you were to rotate it in a particular direction. It is something very important for engineers to learn and master, especially if you are required to do engineering drawing. There are many stages to these mental rotation tasks. We will approach a simple rotation task in this problem.
If you are given the following square -
After rotating it to the right once, it will look like the following -
After rotating it to the left once, it will look like the following -
For this problem you will be given an initial square of size n and a list of rotations to perform.
Your task is to output the final representation of the square after performing all the rotation.
输入:
The first line of input contains an integer NN (1 ≤ N ≤ 1000). and a string containing a combination of ′L′ and ′R′′. Where ′L′ means left rotation and ′R′ means right rotation. Length of the string will not exceed 100. Starting from the second line, the following N line each will contain NN of the following characters (>, <, ∨, ∧ or .). Empty space is indicated by a '.' (dot).
输出:
The output should be N lines, each containing N characters representing the final representation of the square after all the rotation. For ∨ and ∧ use v (small v) and Shift+6 respectively.
样例输入:
3 R >v> ... <^<
3 L >v> ... <^<
3 LL >v> ... <^<
样例输出:
^.v >.< ^.v
^.v >.< ^.v
>v> ... <^<
试题分析:
这是一道简单的模拟题,我们可以写出向左向右翻转的函数,然后直接遍历字符串即可。但这样会超时,可以进行优化,翻转周期为4,并且可以将左转转化为右转。
代码如下:
1 #include<stdio.h> 2 #include<map> 3 #include<string.h> 4 const int MAXN = 1100; 5 using namespace std; 6 7 int n; 8 char s[MAXN], ans[MAXN][MAXN], str[MAXN][MAXN]; 9 map<char, char> mp; 10 11 void move() 12 { 13 for(int col = 1; col <= n; col ++) 14 for(int row = n; row >= 1; row --) 15 ans[col][n - row + 1] = mp[str[row][col]]; 16 for(int i = 1; i <= n; i ++) 17 for(int j = 1; j <= n; j ++) 18 str[i][j] = ans[i][j]; 19 } 20 21 int main() 22 { 23 mp['>'] = 'v', mp['^'] = '>', mp['v'] = '<', mp['<'] = '^', mp['.'] = '.'; 24 25 scanf("%d%s", &n, s); 26 getchar(); 27 for(int i = 1; i <= n; i ++) 28 scanf("%s", str[i] + 1); 29 for(int i = 1; i <= n; i ++) 30 for(int j = 1; j <= n; j ++) 31 ans[i][j] = str[i][j]; 32 int len = strlen(s); 33 int rnum = 0, lnum = 0; 34 for(int i = 0; i < len; i ++) 35 { 36 if(s[i] == 'R') 37 rnum ++; 38 else 39 lnum ++; 40 } 41 if(rnum >= lnum) 42 { 43 rnum -= lnum; 44 rnum %= 4; 45 } 46 else 47 { 48 lnum -= rnum; 49 lnum %= 4; 50 rnum = 4 - lnum; 51 rnum %= 4; 52 } 53 for(int i = 1; i <= rnum; i ++) 54 move(); 55 for(int i = 1; i <= n; i ++) 56 printf("%s\n", ans[i] + 1); 57 return 0; 58 }