hihoCoder-1632-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

题意:就是把一个蛇形矩阵转换成回型矩阵,大模拟。。。AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char str[105][105];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            scanf("%s",str[i]+1);
        int x,y,cnt=0;
        char aim[10005];
        char a[105][105];
        aim[++cnt]=str[1][1];
        x=1;y=1;
        int tag=0;
        while(y<=n)
        {
            //cout<<x<<' '<<y<<endl;
            if(n%2&&y==n) break;
            if(x==n&&n%2==0) break;
            if(tag==0){
                y++;
                aim[++cnt]=str[x][y];
            }
            else if(tag==1){
                while(y>1){
                    x++;y--;
                    aim[++cnt]=str[x][y];
                }
            }
            else if(tag==2){
                x++;
                aim[++cnt]=str[x][y];
            }
            else if(tag==3){
                while(x>1){
                    y++;x--;
                    aim[++cnt]=str[x][y];
                }
            }
            tag++;
            tag%=4;
        }
        if(x==1) tag=0;
        else tag=2;
        while(cnt<n*n)
        {
            if(tag==0){
                x++;
                aim[++cnt]=str[x][y];
            }
            else if(tag==1){
                while(x<n){
                    x++;y--;
                    aim[++cnt]=str[x][y];
                }
            }
            else if(tag==2){
                y++;
                aim[++cnt]=str[x][y];
            }
            else if(tag==3){
                while(y<n){
                    x--;y++;
                    aim[++cnt]=str[x][y];
                }
            }
            tag++;
            tag%=4;
        }
        int i,j;
        int t=1;
        int m=n;
        for (i = 0; i < n / 2; i++)
        {
            for (j = i; j < m - i - 1; j++)
                 a[i][j] = aim[t++];

            for (j = i; j < n - i - 1; j++)

                a[j][m - i - 1] = aim[t++];

            for (j = m - i - 1; j > i ; j--)

                a[n - i - 1][j] = aim[t++];

            for (j = n - i -1; j >i; j--)

                a[j][i] = aim[t++];

        }
        if (n%2!=0)
            for (j=n/2;j<m-n/2;j++)
            a[n/2][j]=aim[t++];
        for(i=0;i<n;i++){
            for(j=0;j<n;j++)
                cout<<a[i][j];
            cout<<endl;
        }

    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/78612868