ACM-ICPC北京赛区2017网络同步赛(题目6 : Secret Poems)

来源:互联网 发布:免费扫描软件 编辑:程序博客网 时间:2024/05/17 09:08

#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.

样例输入
5THSAD IIVOP SEOOH RGETI YMINK2ABCD4ABCDEFGHIJKLMNOP
样例输出
THISIPOEMSDNKIAOIHTVOGYREABDCABEIKHLFNPOCMJGD

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main(){

    int n,x,ok,y;
    char map1[101][101];
    char map2[101][101];
    char str[10001];
    while(~scanf("%d",&n)){
    for(int i=0;i<n;i++){
        scanf("%s",map1[i]);
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            map2[i][j]='@';
        }
        map2[i][n]='\0';
    }
   // printf("----------\n");
    str[0]=map1[0][0];
    ok=0,x=0,y=0;
    int len=1;
  while(len<n*n) {
   if(x==0&&ok==0) {
    if(y+1<n) {
    y++;
    ok=1;
    }
    else{
     x++;
     ok=1;
    }
   }
   else if(y==0&&ok==1) {
    if(x+1<n) {
    x++;
    ok=0;
    }
    else {
     y++;
     ok=0;
    }
   }
   else {
    if(ok==1) {
     if(x+1<n&&y-1>=0) {
     x++;
     y--;
     ok=1;
     }else {
      y++;
      ok=0;
     }
    }
    else if(ok==0) {
     if(x-1>=0&&y+1<n) {
     x--;
     y++;
     ok=0;
     }else {
      x++;
      ok=1;
     }
    }
   }
   //printf("%d %d %d\n",x,y,ok);
   str[len]=map1[x][y];
   len++;
        }
        str[len]='\0';
        x=y=0;
  int s=0;
//  for(int i=0;i<16;i++){
//            str[i]=char('A'+i);
//  }
  //printf("%s\n",str);
  for(int i=0;i<strlen(str);) {
   while(y<n && map2[x][y]=='@') {
    map2[x][y]=str[i++];
    y++;
   }
   y=y-1;
   x=x+1;
   while(x<n &&map2[x][y]=='@'){
    map2[x][y]=str[i++];
             x++;
   }
   y=y-1;
   x=x-1;
   while(y>=0 &&map2[x][y]=='@'){
    map2[x][y]=str[i++];
    y--;
   }
   x=x-1;
   y=y+1;
   while(x>=0&&map2[x][y]=='@'){
    map2[x][y]=str[i++];
    x--;
   }
   y=y+1;
   x=x+1;
  }

  for(int i=0;i<n;i++) {
   for(int j=0;j<n;j++) {
    printf("%c",map2[i][j]);
   }
   printf("\n");
  }
    }
    return 0;
}


阅读全文
0 0