2017北京网络赛 F Secret Poems 蛇形回路输出

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

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

题意:原始数据按图一方式保存,现在要你将原始数据按图二方式保存并输出

题解:
先观察图一,提取原始数据。观察知道,数据都是按照副对角线,要么向上,要么向下的顺序保存的,所以for(i=0;i<2*n;i++)循环模拟。
在观察,发现当i为奇数时方向向下,偶数时方向向上,所以if判断即可
再看图二,是一个蛇形回路,每次走一圈都是正方形,每一圈的起点都是在主对角线上,起点坐标(x,y)每走完一圈都会加一,并且正方形的边长都会减小2。
走一圈分四个方向模拟一遍即可

大佬的代码
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
#define maxn 105
using namespace std;
int ans,n,m,t,x;
char str[maxn][maxn];
string s;
void dfs(int x,int y,int n,int t)//(x,y)是起点坐标,n是要走正方形的边长
{
    if(n<1)
        return;
    for(int i=y; i<y+n; i++)//向右走
        str[x][i]=s[t++];
    for(int i=x+1; i<x+n; i++)//向下走
        str[i][y+n-1]=s[t++];
    for(int i=y+n-2; i>=y; i--)//向左走
        str[x+n-1][i]=s[t++];
    for(int i=x+n-2; i>x; i--)//向上走
        str[i][y]=s[t++];
    dfs(x+1,y+1,n-2,t);//走完一圈之后,起点沿着主对角线+1,下一圈的边长-2
}
int main()
{
    while(cin>>n)
    {
        s.clear();
        for(int i=0; i<n; i++)
            cin>>str[i];
        for(int i=0; i<2*n; i++)//对角线上循环
        {
            if(i%2)//奇数的时候向下走
            {
                for(int j=i; j>=0; j--)
                    if(i-j<n&&j<n)
                        s+=str[i-j][j];
            }
            else//偶数向上走
            {
                for(int j=0; j<=i; j++)
                    if(i-j<n&&j<n)
                        s+=str[i-j][j];
            }
        }
        dfs(0,0,n,0);
        for(int i=0; i<n; i++)
            cout<<str[i]<<endl;
    }
    return 0;
}

 


posted @ 2019-08-14 11:05  知道了呀~  阅读(313)  评论(0编辑  收藏  举报