HihoCoder - 1632—Secret Poems (模拟&&字符串)

版权声明:沃斯里德小浩浩啊 https://blog.csdn.net/Healer66/article/details/82528644

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

这题真的伤,赶脚有点对不起队友。

题意:

看图,给出figure1,,按顺序转化成figure2,然后蛇形填数,最后输出输出。

思路:

模拟呗。

参考大神代码:

https://blog.csdn.net/mjj1024/article/details/78634030

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 110;
char s[maxn][maxn];
char p[maxn][maxn];
int vis[maxn][maxn];
char c[maxn*maxn];
int x,y,tot,n;
//dfs参考大神代码
int dfs(int x,int y,int cnt)
{
    c[cnt]=s[x][y];
    if(x==n-1&&y==n-1)//找到最后一个时返回;
        return 0;
    if((y==0&&x%2!=0&&(n%2==0&&x<n-1||n%2!=0))||(y==n-1&&n%2==0&&x%2!=0&&x<n-1||y==n-1&&n%2!=0&&x%2==0))dfs(x+1,y,cnt+1);
  /*1、在第一列,行的下标为偶数时;(1)n为偶数,并且不在最后一行时;
                                   (2)n为奇数时;
    2、在最后一列时:(1)n为偶数,行的下标不为偶数,且不在最后一行时;
                     (2)n为奇数,行的下标为偶数时;
    以上情况都是(x,y)=>(x+1,y);
  */


    else if((x==0&&y%2==0&&(n%2!=0&&y<n-1||n%2==0))||x==n-1&&(n%2==0&&y%2==0&&y<n-1||n%2!=0&&y%2!=0))dfs(x,y+1,cnt+1);
/*  1、在第一行,列数为偶数时:(1)n为偶数;
                               (2)n为奇数,且不为最后一列时;
    2、在最后一行时:(1)n为偶数,列的下标为偶数,且不为最后一列时;
                     (2)n为奇数,列的下标为奇数时;
    以上情况都是(x,y)=>(x,y+1);
*/

//除以上情况外:
    else if((x+y)%2==0)//行和列的下标之和为偶数时:(x,y)=>(x-1,y+1);
        dfs(x-1,y+1,cnt+1);

    else if((x+y)%2!=0)//行和列的下标之和为奇数时:(x,y)=>(x+1,y-1);
        dfs(x+1,y-1,cnt+1);
}

int main()
{

    while(cin>>n)
    {
        memset(s,0,sizeof s);
        memset(p,0,sizeof p);
        memset(c,0,sizeof c);
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                cin>>s[i][j];
        p[0][0]=s[0][0];
        x=0;y=0;
        dfs(0,0,0);
        tot=0;
        while(tot<n*n-1)//紫书上的蛇形填数
        {
            while(y+1<n&&!p[x][y+1])//右
                p[x][++y]=c[++tot];
            while(x+1<n&&!p[x+1][y])//下
                p[++x][y]=c[++tot];
            while(y-1>=0&&!p[x][y-1])//左
                p[x][--y]=c[++tot];
            while(x-1>=0&&!p[x-1][y])//上
                p[--x][y]=c[++tot];
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                cout<<p[i][j];
            cout<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Healer66/article/details/82528644