Secret Poems HihoCoder - 1632
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:
[f.png]
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
题意:
将题目给的第一种形式的矩阵转变成第二种输出
分析:
费劲的模拟
code:
#include <bits/stdc++.h>
using namespace std;
int n;
char G[110][110];
char ans[110][110];
char ans2[110][110];
int main(){
while(~scanf("%d",&n)){
for(int i = 0; i < n; i++){
scanf("%s",G[i]);
}
int x = 0,y = 0;
int row = 0,col = 0;
int flag = 0;
int flag2 = 0;
while(1){
ans[row][col] = G[x][y];
if((x == 0 || y == 0 || x == n-1 || y == n-1) && flag2 == 0){
if(x == n-1 && y == n-1) break;
if(x == 0 && y == 0){
y++;
flag = 1;
flag2 = 1;
}
else if(x == 0 || y == n-1){
if(x == 0 && y == n-1) x++;
else if(x == 0) y++;
else if(y == n-1) x++;
flag = 1;
flag2 = 1;
}
else if(x == n-1 || y == 0){
if(x == n-1) y++;
else if(y == 0) x++;
flag = 2;
flag2 = 1;
}
}
else{
flag2 = 0;
if(flag == 1){
x++;
y--;
}
else if(flag == 2){
x--;
y++;
}
}
col++;
if(col == n){
col = 0;
row++;
}
if(row == n) break;
}
row = 0;
col = 0;
flag = 0;
memset(ans2,'#',sizeof(ans2));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
ans2[row][col] = ans[i][j];
if(flag == 0){
col++;
if(col == n-1 || ans2[row][col+1] != '#') flag = 1;
}
else if(flag == 1){
row++;
if(row == n-1 || ans2[row+1][col] != '#') flag = 2;
}
else if(flag == 2){
col--;
if(col == 0 || ans2[row][col-1] != '#') flag = 3;
}
else if(flag == 3){
row--;
if(ans2[row-1][col] != '#') flag = 0;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
putchar(ans2[i][j]);
}
putchar('\n');
}
}
return 0;
}