B - Secret Poems (2017年ICPC中国大陆区域赛真题)

 

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.

Input

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.

Output

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

Sample Input
5
THSAD 
IIVOP 
SEOOH 
RGETI 
YMINK
2
AB
CD
4
ABCD
EFGH
IJKL
MNOP
Sample Output
THISI
POEMS
DNKIA
OIHTV
OGYRE
AB
DC
ABEI
KHLF
NPOC
MJGD

题意:按照图片所示。

思路:先将给出的n*n的矩阵转换成一个字符串,在转换成矩阵。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int mod=1e9+10;
const int M=1e4 + 10;
char mapp[110][110];
char mapp2[110][110];
//string s;
char s[11100];
int main()
{
    int n;
    while (cin >> n)
    {
        memset(mapp,0,sizeof(mapp));
        memset(mapp2,0,sizeof(mapp2));
        memset(s,0,sizeof(s));
        int len=0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                cin >> mapp[i][j];
            }
        }
        for (int i = 1; i <= n; i++)
        {
            if (i % 2 != 0)
            {
                int p=i,q=1;
                for (int j = 1; j <= i; j++){
                    s [len++]= mapp[p][j];
                    p--;
                }
            }
            else
            {
                int p=i,q=1;
                for (int j = 1; j <= i; j++)
                {
                    s [len++]= mapp[j][p];
                   p--;
                }
            }
        }
        for (int i = n - 1; i >= 1; i--)
        {
            int p = i, q = n;
            if (i % 2 != 0)
            {
                for (int j = n-i+1; j <= n; j++)
                {
                    s [len++]= mapp[q][j];
                    q--;
                }
            }
            else
            {
                for (int j = n-i+1; j <= n; j++)
                {
                    s [len++]=mapp[j][q];
                    q--;
                }
            }
        }
        for(int i=1; i<=n; i++)
        {
            mapp2[1][i]=s[i-1];
        }
        int e=n;
        int f=n-1,column1=n,line1=2,line2=n,column2=n-1;
        int line3=n-1,column3=1,line4=2,column4=2;
        while(1)
        {
            if(f<0)
                break;
            for(int i=line1; i<=line1+f-1; i++)
            {
                mapp2[i][column1]=s[e];
                e++;
            }
            int r=f;
            for(int i=column2; r>0; i--,r--)
            {
                mapp2[line2][i]=s[e];
                e++;
            }
            f--;
            r=f;
            for(int i=line3;r>0 ; i--,r--)
            {
                mapp2[i][column3]=s[e];
                e++;
            }
            for(int i=column4; i<=column4+f-1; i++)
            {
                mapp2[line4][i]=s[e];
                e++;
            }
            line1++;column1--;
            line2--;column2--;
            line3--;column3++;
            line4++;column4++;
            f--;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cout<<mapp2[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值