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
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
思路:第一次转换时,我们可以发现它总是以一个为左下角,然后斜着到右上;要么就是以一个为右上角,然后斜着到左下。我们就每次都从左下开始,第二种情况时就倒着拿出来就好。
(PS:还是太弱了,写了两个不是dfs的函数,太过沙雕。。。)
1.精简代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define ll long long
using namespace std;
char mp[110][110],s[1100],ans[110][110],tmp[110];
bool vis[110][110];
int n;
//d:
//右:0 下:1 左:2 上:3
void dfs1(int x,int y,int in,int d)
{
if(in==n*n+1)
{
return ;
}
else
{
vis[x][y]=1;
ans[x][y]=s[in];
if(d==0)
{
if(vis[x][y+1]||y==n)
dfs1(x+1,y,in+1,(d+1)%4);
else
dfs1(x,y+1,in+1,d);
}
else if(d==1)
{
if(vis[x+1][y]||x==n)
dfs1(x,y-1,in+1,(d+1)%4);
else dfs1(x+1,y,in+1,d);
}
else if(d==2)
{
if(vis[x][y-1]||y==1)
dfs1(x-1,y,in+1,(d+1)%4);
else dfs1(x,y-1,in+1,d);
}
else
{
if(vis[x-1][y]||x==1)
dfs1(x,y+1,in+1,(d+1)%4);
else dfs1(x-1,y,in+1,d);
}
}
}
int main(void)
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
vis[i][j]=0;
for(int i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
}
//flag表示是从左下到右上,还是从右上到左下
int x=1,y=1,flag=1,cnt=0;
while(x+y!=2*n+1)
{
int xx=x,yy=y,in=0;
//每次都从左下到右上
while(xx>=1&&yy<=n)
{
tmp[++in]=mp[xx][yy];
xx--;
yy++;
}
for(int i=1;i<=in;i++)
if(flag) s[++cnt]=tmp[i];
else s[++cnt]=tmp[in-i+1];
if(flag) flag=0;
else flag=1;
//如果到了最后一行,就往右走
if(x==n) y++; else x++;
}
//s[++cnt]=mp[n][n];
//printf("%s\n",s+1);
dfs1(1,1,1,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
printf("%c",ans[i][j]);
printf("\n");
}
}
return 0;
}
2.本人垃圾代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define ll long long
using namespace std;
char mp[110][110],s[1100],ans[110][110];
bool vis[110][110];
int n;
int getx(int x,int dir)
{
if(dir==0)
return x+1;
else if(dir==1)
return x+1;
else if(dir==2)
return x-1;
else return x;
}
int gety(int y,int dir)
{
if(dir==0)
return y-1;
else if(dir==1)
return y;
else if(dir==2)
return y+1;
else return y+1;
}
//dir
//左下;0 下:1 右上:2 右:3
int getd(int x,int y,int dir)
{
if(y==1)
{
if(dir==0)
{
if(x==n) return 3;
else return 1;
}
else if(dir==1)
{
return 2;
}
}
else if(x==1)
{
if(dir==2)
{
if(y==n) return 1;
else return 3;
}
else if(dir==3)
{
return 0;
}
}
else if(y==n)
{
if(dir==2) return 1;
else if(dir==1) return 0;
}
else if(x==n)
{
if(dir==0) return 3;
else if(dir==3) return 2;
}
else
return dir;
}
void dfs(int x,int y,int dir,int cnt)
{
if(cnt==n*n+1)
{
return ;
}
else
{
s[cnt]=mp[x][y];
int nx=getx(x,dir),ny=gety(y,dir),nd=getd(nx,ny,dir);
dfs(nx,ny,nd,cnt+1);
}
}
//d:
//右:0 下:1 左:2 上:3
void dfs1(int x,int y,int in,int d)
{
if(in==n*n+1)
{
return ;
}
else
{
vis[x][y]=1;
ans[x][y]=s[in];
if(d==0)
{
if(vis[x][y+1]||y==n)
dfs1(x+1,y,in+1,(d+1)%4);
else
dfs1(x,y+1,in+1,d);
}
else if(d==1)
{
if(vis[x+1][y]||x==n)
dfs1(x,y-1,in+1,(d+1)%4);
else dfs1(x+1,y,in+1,d);
}
else if(d==2)
{
if(vis[x][y-1]||y==1)
dfs1(x-1,y,in+1,(d+1)%4);
else dfs1(x,y-1,in+1,d);
}
else
{
if(vis[x-1][y]||x==1)
dfs1(x,y+1,in+1,(d+1)%4);
else dfs1(x-1,y,in+1,d);
}
}
}
int main(void)
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
vis[i][j]=0;
for(int i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
}
if(n==1)
{
printf("%c\n",mp[1][1]);
continue;
}
s[1]=mp[1][1];
dfs(1,2,0,2);
//printf("%s\n",s+1);
dfs1(1,1,1,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
printf("%c",ans[i][j]);
printf("\n");
}
}
return 0;
}