【恐怖的数组模拟】Secret Poems Secret Poems - HihoCoder - 1632 

【恐怖的数组模拟】Secret Poems
Secret Poems - HihoCoder - 1632 

  图一                图二

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.InputThere 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.OutputFor 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

大致题意:

  就是先按照图一的方式进行遍历这个图——按照图一的顺序存成一个字符串;然后按照图二的“回字形 ”顺序进行调整,然后输出图二!

AC题解:

  没错,题意就是这么简单!写的时候一不留神就出了很多BUG!然后改来改去花费了不少的时间!还是得细心点点!

  没有想到简单的办法,只好进行If...else...的嵌套!

  1 #include<stdio.h>
  2 #include<math.h>
  3 #include<string.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<set>
  8 #include<vector>
  9 #define inf 0x3f3f3f3f   //Pangu and Stones ,I
 10 #define ll long long
 11 using namespace std;
 12 #define N 108
 13 char a[N][N];
 14 char ans[N][N];
 15 bool vis[N][N];
 16 int dir[6][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,-1},{-1,1} };
 17 void factans(string s,int n){
 18     int x=0,y=0;
 19     memset(vis,false,sizeof(vis));
 20     vis[0][0]=true;
 21     memset(ans,'6',sizeof(ans));
 22     ans[0][0]=s[0];
 23     int op=-1;
 24             //先按之前的进行,右下左上右————
 25    for(int i=1;i<s.length();i++){
 26         if(op!=-1){
 27                 int dx=x+dir[op][0];
 28                 int dy=y+dir[op][1];
 29             if(dx>=0&&dx<=n-1&&dy>=0&&dy<=n-1&&vis[dx][dy]==false){
 30                x=dx;y=dy;
 31                 ans[x][y]=s[i];
 32                 vis[x][y]=true;
 33                 continue;
 34             }
 35         }
 36         if(y+1<=n-1&&vis[x][y+1]==false){//右下左上右————
 37             ans[x][y+1]=s[i];
 38             y++;
 39             op=0;vis[x][y]=true;
 40         }else if(x+1<=n-1&&vis[x+1][y]==false){
 41             ans[x+1][y]=s[i];
 42             x++;
 43             op=1;vis[x][y]=true;
 44         }else if(y-1>=0&&vis[x][y-1]==false){
 45             ans[x][y-1]=s[i];
 46             y--;
 47             op=2;vis[x][y]=true;
 48         }else if(x-1>=0&&vis[x-1][y]==false){
 49             ans[x-1][y]=s[i];
 50             x--;
 51             op=3;vis[x][y]=true;
 52         }
 53    }
 54    for(int i=0;i<n;i++)
 55         ans[i][n]='