hihocoder 1632 Secret Poems

题目链接: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值