1. 程式人生 > >【恐怖的數組模擬】Secret Poems - HihoCoder - 1632

【恐怖的數組模擬】Secret Poems - HihoCoder - 1632

com mea target intro bubuko 就是 display efi spa

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]=\0; 56 for(int i=0;i<n;i++) 57 printf("%s\n",ans[i]); 58 59 } 60 int main(){ 61 int n; 62 63 while(scanf("%d",&n)!=EOF){ 64 //int num=1; 65 for(int i=0;i<n;i++) 66 scanf("%s",a[i]); 67 string s; 68 s.clear(); 69 int i=0,j=0; 70 int op=-1; 71 memset(vis,false,sizeof(vis)); 72 while(1){ 73 74 s+=a[i][j];//存儲當前位置 75 76 if(i==n-1&&j==n-1){ 77 // cout<<"end: "<<s<<endl; 78 break; 79 } 80 81 vis[i][j]=true; 82 83 if(i==0){//在第一行,可右0,可左下4,可下1 84 if(j+1<=n-1&&op!=0){ 85 i+=dir[0][0]; 86 j+=dir[0][1]; 87 op=0; 88 } 89 else if(i+1<=n-1&&j-1>=0&&vis[i+1][j-1]==false){ 90 i+=dir[4][0]; 91 j+=dir[4][1]; 92 op=4; 93 } 94 else{ 95 i+=dir[1][0]; 96 j+=dir[1][1]; 97 op=1; 98 } 99 } 100 else if(j==0){//在第一列,先下1,後右上5,後右0 101 if(op!=1&&i+1<=n-1){ 102 i+=dir[1][0]; 103 j+=dir[1][1]; 104 op=1; 105 }else if(i-1>=0&&j+1<=n-1&&vis[i-1][j+1]==false){ 106 i+=dir[5][0]; 107 j+=dir[5][1]; 108 op=5; 109 } 110 else{ 111 i+=dir[0][0]; 112 j+=dir[0][1]; 113 op=0; 114 } 115 } 116 else if(i==n-1){//最後一行,先右上5,後右0 117 if(i-1>=0&&j+1<=n-1&&vis[i-1][j+1]==false){ 118 i+=dir[5][0]; 119 j+=dir[5][1]; 120 op=5; 121 } 122 else{ 123 i+=dir[0][0]; 124 j+=dir[0][1]; 125 op=0; 126 } 127 } 128 else if(j==n-1){//最後一列,先左下4,後下1 129 if(i+1<=n-1&&j-1>=0&&vis[i+1][j-1]==false){ 130 i+=dir[4][0]; 131 j+=dir[4][1]; 132 op=4; 133 } 134 else if(op!=1){ 135 i+=dir[1][0]; 136 j+=dir[1][1]; 137 op=1; 138 } 139 } 140 else{//其余的照舊就可以了 141 i+=dir[op][0]; 142 j+=dir[op][1]; 143 } 144 } 145 factans(s,n); 146 147 } 148 return 0; 149 }
View Code(有詳細的解釋呦!)

【恐怖的數組模擬】Secret Poems - HihoCoder - 1632