题目链接:Secret Poems
题目大意:给你一个某种路径形成的字符串,还原后用另一种方法表示
题目思路:按题意模拟就好了,还原的时候分一下上下三角,然后上下三件移动的方向是成循环的,下半角需要分一下奇偶以确定初始方向
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6+10;
int main(){
int n;
char mp[200][200];
while(~scanf("%d",&n)){
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
cin>>mp[j][i];
int x = 1,y = 1;
string dir = "R";
string str = "";
str += mp[1][1];
int flag = 0;
while(1){
if(x == n&&y == n) break;
if(x == n&&y == 1){
flag = 1;
if(n&1) dir = "D";
else dir = "LD";
}
if(flag == 0){//shang
if(dir == "R"){
dir = "LD";
x++;
str += mp[x][y];
}
else if(dir == "LD"){
if(y + 1 > n||x - 1 <= 0) dir = "D";
else x--,y++,str += mp[x][y];
}
else if(dir == "D"){
dir = "RU";
y++;
str += mp[x][y];
}
else if(dir == "RU"){
if(x + 1 > n||y - 1 <= 0) dir = "R";
else x++,y--,str += mp[x][y];
}
}
else{
if(dir == "D"){
dir = "LD";
y++;
str += mp[x][y];
}
else if(dir == "LD"){
if(y + 1 > n||x - 1 <= 0) dir = "R";
else x--,y++,str += mp[x][y];
}
else if(dir == "R"){
dir = "RU";
x++;
str += mp[x][y];
}
else if(dir == "RU"){
if(x + 1 > n||y - 1 <= 0) dir = "D";
else x++,y--,str += mp[x][y];
}
}
}
x = 1,y = 1;
int ct = 1,cnt = 1;
memset(mp,'*',sizeof(mp));
mp[1][1] = str[0];
dir = "R";
while(1){
if(cnt == n*n) break;
if(dir == "R"){
if(x + 1 > n||mp[x+1][y] != '*') dir = "D";
else cnt++,x++,mp[x][y] = str[ct++];
}
else if(dir == "D"){
if(y + 1 > n||mp[x][y+1] != '*') dir = "L";
else cnt++,y++,mp[x][y] = str[ct++];
}
else if(dir == "L"){
if(x - 1 <= 0||mp[x-1][y] != '*') dir = "U";
else cnt++,x--,mp[x][y] = str[ct++];
}
else if(dir == "U"){
if(y - 1 <= 0||mp[x][y-1] != '*') dir = "R";
else cnt++,y--,mp[x][y] = str[ct++];
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
printf("%c",mp[j][i]);
}
puts("");
}
}
return 0;
}