zoukankan      html  css  js  c++  java
  • 2017 ICPC beijing F

    #1632 : Secret Poems

    时间限制:1000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    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.

    输入

    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.

    输出

    For each test case, convert the poem in old order into a poem in new order.

    样例输入
    5
    THSAD 
    IIVOP 
    SEOOH 
    RGETI 
    YMINK
    2
    AB
    CD
    4
    ABCD
    EFGH
    IJKL
    MNOP
    样例输出
    THISI
    POEMS
    DNKIA
    OIHTV
    OGYRE
    AB
    DC
    ABEI
    KHLF
    NPOC
    MJGD
    /*
       模拟
    */
    
    #include <bits/stdc++.h>
    
    #define MAXN 123
    
    using namespace std;
    
    int n;
    char str[MAXN][MAXN];
    string s;
    int x,y;
    int pos;
    int dir[4][2]={0,1,1,-1,1,0,-1,1};
    int dir2[4][2]={0,1,1,0,0,-1,-1,0};
    
    bool vis[MAXN][MAXN];
    
    bool ok(int x,int y){
        if(x<0||x>=n||y<0||y>=n||vis[x][y]==true)
            return false;
        return true;
    }
    
    inline void init(){
        memset(str,'',sizeof str);
        memset(vis,false,sizeof vis);
        pos=0;
        s="";
        x=0;
        y=0;
    }
    
    int main(){
    //    freopen("in.txt","r",stdin);
        while(scanf("%d",&n)!=EOF){    
            init();
            for(int i=0;i<n;i++)
                scanf("%s",str[i]);
            while(true){    
                vis[x][y]=true;
                s+=str[x][y];
                bool flag=false;
                int i;
                for(i=0;i<4;i++){
                    int fx=x+dir[(pos+i)%4][0];
                    int fy=y+dir[(pos+i)%4][1];
                    if(ok(fx,fy)==true){
                        flag=true;
                        break;
                    }
                }
                if(flag==false)
                    break;
                pos=(pos+i)%4;
                x+=dir[pos][0];
                y+=dir[pos][1];
                if(pos==0||pos==2)
                    pos=(pos+1)%4;
            }
            memset(vis,false,sizeof vis);
            x=0;y=0;
            pos=0;
            int cur=0;
            while(true){
                vis[x][y]=true;
                str[x][y]=s[cur++];
                bool flag=false;
                int i;
                for(i=0;i<4;i++){
                    int fx=x+dir2[(pos+i)%4][0];
                    int fy=y+dir2[(pos+i)%4][1];
                    if(ok(fx,fy)==true){
                        flag=true;
                        break;
                    }
                }
                if(flag==false)
                    break;
                pos=(pos+i)%4;
                x+=dir2[pos][0];
                y+=dir2[pos][1];    
            }
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    printf("%c",str[i][j]);
                }
                printf("
    ");
            }    
        }
        return 0;
    }
  • 相关阅读:
    AJPFX总结mysql复制表结构,表数据
    AJPFX总结IO流中的缓冲思想
    AJPFX学习Java函数知识总结
    AJPFX总结面向对象特征之一的继承知识
    AJPFX学习笔记JavaAPI之String类
    AJPFX:学习JAVA程序员两个必会的冒泡和选择排序
    AJPFX:关于面向对象及java的一些机制的思考
    ES6 入门系列
    Android studio工具介绍
    获得 LayoutInflater 实例的三种方式
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7861633.html
Copyright © 2011-2022 走看看