2017 - ICPC 北京赛区 F SecretPoem 找规律 or 模拟

题目链接 :  https://hihocoder.com/problemset/problem/1632

The Yongzheng Emperor (13 December 1678 – 8 October 1735), was the fifth emperor of the Qing dynasty of China. He was a very hard-working ruler. He cracked down on corruption and his reign was known for being despotic, efficient, and vigorous.

Yongzheng couldn’t tolerate people saying bad words about Qing or him. So he started a movement called “words prison”. “Words prison” means literary inquisition. In the famous Zhuang Tinglong Case, more than 70 people were executed in three years because of the publication of an unauthorized history of the Ming dynasty.

In short, people under Yongzheng’s reign should be very careful if they wanted to write something. So some poets wrote poems in a very odd way that only people in their friends circle could read. This kind of poems were called secret poems.

A secret poem is a N×N matrix of characters which looks like random and meaning nothing. But if you read the characters in a certain order, you will understand it. The order is shown in figure 1 below:

            figure 1                                                           figure 2

Following the order indicated by arrows, you can get “THISISAVERYGOODPOEMITHINK”, and that can mean something.

But after some time, poets found out that some Yongzheng’s secret agent called “Mr. blood dripping” could read this kind of poems too. That was dangerous. So they introduced a new order of writing poems as shown in figure 2. And they wanted to convert the old poems written in old order as figure1 into the ones in new order. Please help them.

输入

There are no more than 10 test cases.

For each test case:

The first line is an integer N( 1 <= N <= 100), indicating that a poem is a N×N matrix which consist of capital letters.

Then N lines follow, each line is an N letters string. These N lines represent a poem in old order.

输出

For each test case, convert the poem in old order into a poem in new order.

样例输入

5
THSAD 
IIVOP 
SEOOH 
RGETI 
YMINK
2
AB
CD
4
ABCD
EFGH
IJKL
MNOP

样例输出

THISI
POEMS
DNKIA
OIHTV
OGYRE
AB
DC
ABEI
KHLF
NPOC
MJGD

按规律将全部字符存入字符数组。然后按给定的顺序将字符数组填入二维数组。

关键就是计算 (i,j) 这个坐标对应一维数组中哪个坐标。

以4为例。

(0,0) (0,1) (0,2) (0,3)          - >   1    2   6    7                

(1,0) (1,1) (1,2) (1,3)                  3   5   8   13

(2,0) (2,1) (2,2) (3,3)                  4   9  12  14

(3,0) (3,1) (3,2) (3,3)                  10 11 15  16

斜着划分行,每个点的 i+j 是其所在行号-1    比如 (2,1)  在第四行,则该点前面有3行,字符数为 1+2+3 = 6

接下来算出该点在其所在行是第几个,因为奇数行和偶数行情况不,同所以分开讨论,在奇数行,第一个点是在该行的最下端,

在偶数行,第一个点在该行最上端  , (2,1)在偶数行,所以 (2,1)应该排在第 6 + i +1 位。在一维数组中的下标则是 6 + i

在前 n 行, 每行的第一个点的 i,和 j都是 0,所以可以直接加。 但是在n行以后, 不仅等差数列发生了变化, 每行的第一个点的i  和 j也发生了变化,所以某个点在该行的位置就不是 i 或 j    而是  n - j - 1  或者 n - i - 1;

#include <stdio.h>
#include <cstring>
#include <iostream>
using namespace std;
#define fo(i,a,b) for(int i = a; i <= b; i++)
char map[102][102];
int vis[102][102]={0};
char s[102*102];
char m[102][102];
int n;
void init(){
	memset(vis, 0, sizeof vis);
	memset(m, 0, sizeof m);
	memset(s, 0, sizeof s);
	fo(i,0,n-1)
		fo(j,0,n-1){
			cin>>map[i][j];
		} 
}
int main(){
	while(cin>>n){
		init();
		s[0] = map[0][0];
		int maxn = n*(n+1)/2;
		int ans = 2;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			if((i+j)<n){ // 矩形上半部分 
				if((i+j)%2==0&&(i+j)!=0){                 
					int  x = i + j;						             
					int l = x * (x + 1) / 2 + j;
					s[l] = map[i][j];
				}
				else if((i+j)%2!=0){
					int x = i + j;
					int l = x * (x + 1) / 2 + i;
					s[l] = map[i][j];
				}
			}
			else{     // 矩形下半部分 
				if((i+j)%2==0){                     
					int l = maxn + (i+j-n)*(n-1) + (i+j-n)*(i+j-n-1)*(-1)/2 + j - (i+j-n+1); 
					s[l] = map[i][j];
				}
				else if((i+j)%2!=0){
					int l = maxn + (i+j-n)*(n-1) + (i+j-n)*(i+j-n-1)*(-1)/2 + i - (i+j-n+1); 
					s[l] = map[i][j];
				}
			}
		}
	}
	int i = 0, j = 0, sum = 0;
	m[0][0] = map[0][0];
	vis[0][0] = 1;
	while(sum < n*n - 1){
		while(j < n - 1 && !vis[i][j+1]) m[i][++j] = s[++sum], vis[i][j] = 1;
		while(i < n - 1 && !vis[i+1][j]) m[++i][j] = s[++sum],vis[i][j] = 1;
		while(j  > 0 && !vis[i][j-1] ) m[i][--j] = s[++sum],vis[i][j] = 1;
		while(i  > 0 && !vis[i-1][j]) m[--i][j] = s[++sum],vis[i][j] = 1;
	}
	fo(i, 0, n-1){ 
		fo(j, 0, n-1)
			cout<<m[i][j];
		cout<<endl; 
	} 
	}
	return 0;
}
//THSAD 
//IIVOP 
//SEOOH 
//RGETI 
//YMINK
发布了52 篇原创文章 · 获赞 114 · 访问量 6006

猜你喜欢

转载自blog.csdn.net/GD_ONE/article/details/102517276