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
就是先按照图一的方式进行遍历这个图——按照图一的顺序存成一个字符串;然后按照图二的“回字形 ”顺序进行调整,然后输出图二!
没错,题意就是这么简单!写的时候一不留神就出了很多BUG!然后改来改去花费了不少的时间!还是得细心点点!
没有想到简单的办法,只好进行If…else…的嵌套!
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f //Pangu and Stones ,I
#define ll long long
using namespace std;
#define N 108
char a[N][N];
char ans[N][N];
bool vis[N][N];
int dir[][]={ {,},{,},{,-},{-,},{,-},{-,} };
void factans(string s,int n){
int x=,y=;
memset(vis,false,sizeof(vis));
vis[][]=true;
memset(ans,'',sizeof(ans));
ans[][]=s[];
int op=-;
//先按之前的进行,右下左上右————
for(int i=;i
ans[x][y-]=s[i];
y--;
op=;vis[x][y]=true;
}else if(x->=&&vis[x-][y]==false){
ans[x-][y]=s[i];
x--;
op=;vis[x][y]=true;
}
}
for(int i=;i<n;i++)
ans[i][n]='\0';
for(int i=;i<n;i++)
printf("%s\n",ans[i]);
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
//int num=1;
for(int i=;i<n;i++)
scanf("%s",a\[i\]);
string s;
s.clear();
int i=,j=;
int op=-;
memset(vis,false,sizeof(vis));
while(){
s+=a\[i\]\[j\];//存储当前位置
if(i==n-&&j==n-){
// cout<<"end: "<<s<<endl;
break;
}
vis\[i\]\[j\]=true;
if(i==){//在第一行,可右0,可左下4,可下1
if(j+<=n-&&op!=){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
else if(i+<=n-&&j->=&&vis\[i+\]\[j-\]==false){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
else{
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
}
else if(j==){//在第一列,先下1,后右上5,后右0
if(op!=&&i+<=n-){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}else if(i->=&&j+<=n-&&vis\[i-\]\[j+\]==false){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
else{
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
}
else if(i==n-){//最后一行,先右上5,后右0
if(i->=&&j+<=n-&&vis\[i-\]\[j+\]==false){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
else{
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
}
else if(j==n-){//最后一列,先左下4,后下1
if(i+<=n-&&j->=&&vis\[i+\]\[j-\]==false){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
else if(op!=){
i+=dir\[\]\[\];
j+=dir\[\]\[\];
op=;
}
}
else{//其余的照旧就可以了
i+=dir\[op\]\[\];
j+=dir\[op\]\[\];
}
}
factans(s,n);
}
return ;
}
(有详细的解释呦!)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章