CSUOJ 1341 String and Arrays

Description

    There is an N * N character matrix, which is recorded as row 1, row 2, ..., row N from top to bottom , and column 1, column 2, ..., row from left to right. N columns.
    A series of operations will be performed on this matrix, but there are only two types of operations:
    (1) R : Rotate the matrix 90 degrees counterclockwise;
    (2) P xy : Print the characters in the xth row and the yth column at this time, where 1 <= x , y <= N .

Input

    The first line of the input data contains an integer T (1 <= T <= 20), indicating that there are a total of T sets of test data.
    For each set of test data, the first row contains an integer N (1 <= N <= 300) with the same meaning as above. Next, there are a total of N rows, each row contains N uppercase letters, describing the initial situation of this matrix. The next line contains an integer M (1 <= M <= 10000), indicating a total of M operations on the matrix . The next M lines, each of which contains an operation conforming to the above format, describe the M operations in turn.

Output

    For each type (2) operation, print out the character at the specified position on one line.
    Adjacent two sets of test data are separated by a blank line.

Sample Input

3
2
AWAY
CD
3
P 1 1
R
P 1 1
2
AWAY
CD
4
R
R
P 2 1
P 1 2
3
ABC
DEF
TAKE NOTE
5
P 3 3
R
P 3 3
R
P 3 3

Sample Output

A
B

B
C

I
G
A

Hint

    This topic mainly exercises how to read strings and use arrays.

    The initial matrix can be read directly by reading the string, but how to read each operation? First use getchar(x) or scanf(“%c”, &x) to read in a character, and then judge whether to read two more integers according to the read characters? 

    其实getchar(x)或者scanf(“%c”, &x)有的时候很令人头疼(也许只有你亲自尝试过才能体会到), 因为他不仅可以读入一个字母,也可以读入空格、换行等等,因此如果我们不细加控制的话,scanf(“%c”, &x)很可能读到的未必是我们想要的那个字符。但scanf(“%s”, s)就不一样了,会忽略掉空格、换行等等,就像我们使用scanf(“%d”, &x)读整数那样。因此即使只有一个字母,我们也更推荐使用scanf(“%s”, s)的形式去读入,然后s[0]自然就是那个字符了。

    这个题目具体要怎么做呢?看似直接模拟两种操作就好了,打印字符最简单了,而旋转矩阵也不是很麻烦,不过可能要借助一个“中间矩阵”来完成旋转更好一些。但问题来了,如果每个R我们都进行旋转的话,实际上代价是很大的,对于这个题目而言会超时。也许你会机智地想到把相邻的R操作合并成一个,这样代价会小一点,是的,但是这样并不能解决根本问题,比如R和P是交替出现的。

    但其实我们可以做到只要旋转3次矩阵,你想到了吗?

    或者也许我们根本没必要按题意来去旋转矩阵?是否可以直接找到要打印的字符在初始矩阵中的位置呢?

    最后注意相邻两组测试数据之间要打印一个空行,就像在“A Sample Problem”中说的那样,OJ对于输出的格式要求是很严格的。

    同样的,我会给出示例代码,如果你觉得编写代码的时候有些困难的话可以参考一下示例代码,但最后一定要按自己的思路一气呵成一份完整的代码并获得“Accept”哟~


思路:在旋转时只需要找到x,y旋转前后改变的规律,通过改变x,y来输出结果     另外注意输出格式

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; 
char map[305][305],s[2];
int main()
{
	int T;
	while(~scanf("%d",&T))
	{ 
		int flag=0;
		while(T--)
		{	
			if(flag)
				puts("");
			int N,M,ans=0,x,y;
			scanf("%d",&N);
			for(int i=0;i<N;i++)
				scanf("%s",map[i]);
			scanf("%d",&M);
			while(M--)
			{
				scanf("%s",s);
				if(s[0]=='P')
				{
					scanf("%d%d",&x,&y);
					if(ans==1)
						printf("%c\n",map[y-1][N-x]);
					else if(ans==2)
						printf("%c\n",map[N-x][N-y]);
					else if(ans==3)
						printf("%c\n",map[N-y][x-1]);
					else
						printf("%c\n",map[x-1][y-1]);
				}
				else if(s[0]=='R')
				{
					ans=(ans+1)%4;
				}
			}
			flag=1;
			
		}
	}
	return 0;
}
/**********************************************************************
	Problem: 1341
	User: leo6033
	Language: C++
	Result: AC
	Time:112 ms
	Memory:1208 kb
**********************************************************************/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325997200&siteId=291194637