HihoCoder

来源:互联网 发布:种子软件哪个好 编辑:程序博客网 时间:2024/06/09 20:07

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
5THSAD IIVOP SEOOH RGETI YMINK2ABCD4ABCDEFGHIJKLMNOP
Sample Output
THISIPOEMSDNKIAOIHTVOGYREABDCABEIKHLFNPOCMJGD
题意:给你一个如图所示的图形,先还原。然后按照蛇形的方式输出。

#include<stdio.h>#include<string.h>char map[120][120];char s[12000];char pp[120][120];int n,k,st=0;void dfs(int x,int y,int k){    s[k]=map[x][y];    if(x==n-1&&y==n-1)return ;    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,k+1);    }    /*向右读数   1.n为奇数时: a:第一行,偶数列   y<n-1                                 b:最后一行·, 奇数列                2. n为偶数时: a:第一行,偶数列                               b:最后一行,偶数列  y<n-1*/    else if((y==0&&x%2&&(n%2!=0||n%2==0&&x<n-1))||(y==n-1&&(n%2==0&&x%2!=0&&x<n-1||n%2!=0&&x%2==0)))    {        dfs(x+1,y,k+1);    }     /*向下读数   1.n为奇数时: a:第一列,奇数行                                  b:最后一列·, 偶数行                  2. n为偶数时: a:第一列,奇数行  x<n-1                               b:最后一行,偶数列 x<n-1*/    else if((x+y)%2!=0)    {               dfs(x+1,y-1,k+1);    }     /*向左下读数  x与y的和是奇数*/    else if((x+y)%2==0)    {        dfs(x-1,y+1,k+1);    }    /*向右上读数  x与y的和是偶数*/}/*上述语句顺序不能更换,有可能符合两条的,输出就会出错,多试几次就会明白*/int print(int x,int y,int k){    pp[x][y]=s[k];    if(k==n*n-1)return 0;    if(pp[x][y+1]=='1'&&(x==0||pp[x-1][y]!='1'))//在第一行或者上边已被赋值时    向右        print(x,y+1,k+1);    else if(pp[x+1][y]=='1'&&(y==n-1||pp[x][y+1]!='1'))//在最后一列或右边已被赋值   向下        print(x+1,y,k+1);    else if(pp[x][y-1]=='1'&&(x==n-1||pp[x+1][y]!='1'))//在最后一行或下边已被赋值   向左        print(x,y-1,k+1);    else if(pp[x-1][y]=='1'&&(x==0||pp[x][y-1]!='1'))//在第一列或者左边已被赋值时    向上        print(x-1,y,k+1);}int main(){    while(~scanf("%d",&n))    {        for(int i=0; i<n; i++)            scanf("%s",&map[i]);        dfs(0,0,0);        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)                pp[i][j]='1';        print(0,0,0);        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)            {                if(j==n-1)printf("%c\n",pp[i][j]);                else printf("%c",pp[i][j]);            }    }    return 0;}