1. 程式人生 > >ACM-ICPC北京賽區2017網路同步賽 題目6 : Secret Poems

ACM-ICPC北京賽區2017網路同步賽 題目6 : Secret Poems

題目6 : 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:
這裡寫圖片描述

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>
using namespace std;
//兩個矩陣
char A[105][105], B[105][105];
//按順序存取第一個矩陣
char C[10005];
int cnt;
//插入到第二個矩陣時判斷轉向
bool visit[105][105];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(visit, false, sizeof(visit));
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                cin>>A[i][j];
        cnt = 0;
        int x = 0;
        int y = 0;
        int fax = 1;//1為向上,-1為向下
        int s = 0;//是否橫向還是斜向
        //左上部矩陣儲存
        while(x != n-1 && y != n-1)
        {
            C[cnt++] = A[x][y];
            if(x == 0)
            {
                if(s == 0)
                {
                    y++;
                    s = 1;
                    continue;
                }
                else
                {
                    y--;
                    x++;
                    fax = -1;
                    s = 0;
                    continue;
                }
            }
            if(y == 0)
            {
                if(s == 0)
                {
                    x++;
                    s = 1;
                    continue;
                }
                else
                {
                    x--;
                    y++;
                    fax = 1;
                    s  = 0;
                    continue;
                }
            }
            if(fax == 1)
            {
                x--;
                y++;
            }
            if(fax == -1)
            {
                x++;
                y--;
            }
        }
        s = 1;
        fax = 1;
        //右下部矩陣儲存
        while(x != n-1 || y != n-1)
        {
            C[cnt++] = A[x][y];
            if(x == n-1)
            {
                if(s == 0)
                {
                    y++;
                    s = 1;
                    continue;
                }
                else
                {
                    y++;
                    x--;
                    fax = 1;
                    s = 0;
                    continue;
                }
            }
            if(y == n-1)
            {
                if(s == 0)
                {
                    x++;
                    s = 1;
                    continue;
                }
                else
                {
                    x++;
                    y--;
                    fax = -1;
                    s  = 0;
                    continue;
                }
            }
            if(fax == 1)
            {
                x--;
                y++;
            }
            if(fax == -1)
            {
                x++;
                y--;
            }
        }
        C[cnt++] = A[n-1][n-1];
        x = y = 0;
        fax = 1;
        //轉換
        for(int i=0; i<cnt; i++)
        {
            B[x][y]  = C[i];
            visit[x][y] = true;
            if(fax == 1)
            {
                if((y+1) <= (n-1) && !visit[x][y+1])
                    y++;
                else
                {
                    fax = 2;
                    x++;
                }
                continue;
            }
            if(fax == 2)
            {
                if((x+1) <= (n-1) &&  !visit[x+1][y])
                    x++;
                else
                {
                    y--;
                    fax = 3;
                }
                continue;
            }
            if(fax == 3)
            {
                if((y-1) >= 0 && !visit[x][y-1])
                    y--;
                else
                {
                    x--;
                    fax = 4;
                }
                continue;
            }
            if(fax == 4)
            {
                if((x-1) >= 0 && !visit[x-1][y])
                    x--;
                else
                {
                    y++;
                    fax = 1;
                }
                continue;
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                cout<<B[i][j];
            cout<<"\n";
        }
    }
    return 0;
}

相關推薦

ACM-ICPC北京賽區2017網路同步 題目6 : Secret Poems

題目6 : Secret Poems 時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 The Yongzheng Emperor (13 December 1678 – 8 October 173

ACM-ICPC北京賽區2017網路同步 題目5 : Cats and Fish【模擬】

時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 There are many homeless cats in PKU campus. They are all happy because the students in

ACM-ICPC北京賽區2017網路同步 E+F

目錄 E - Cats and Fish(模擬+思維) F - Secret Poems(模擬+字串處理+蛇形填數) E - Cats and Fish(模擬+思維) There are many homeless cats in PKU campus. They ar

ACM-ICPC北京賽區(2017)網絡1【模擬+枚舉+數組操作】

fine enter who bit some title head efi cat 題目1 : Visiting Peking University 時間限制:1000ms 單點時限:1000ms 內存限制:256MB 描述 Ming

hihocoder 1586 ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡-題目9 : Minimum【線段樹】

ont min bsp 判斷 網絡 線段 絕對值 pre mic https://hihocoder.com/problemset/problem/1586 線段樹操作,原來題並不難。。。。。 要求乘積最小只要求區間內最大值、最小值和絕對值小的數,再判斷min,max正負就

hihoCoder-1633 ACM-ICPC北京賽區2017 G.Liaoning Ship’s Voyage 線段與三角形規範相交

dcm problem ems can 方向 class oss acm-icpc 相交 題面 題意:給你一個20*20的地圖,起點(0,0),終點(n-1,n-1),有障礙的點為‘#’,每次可以向8個方向走一步,還給了一個三角形,除了障礙以外,到

ACM-ICPC國際大學生程式設計競賽北京賽區(2017)網路(A+E+F+G+I)

目錄 A - Visiting Peking University(模擬) E - Territorial Dispute(數學幾何+思維) F - Cake(思維) G - Bounce(找規律) I - Minimum(線段樹模板題) A - Visiting

ACM-ICPC國際大學生程式設計競賽北京賽區(2017)網路 題解彙總 Territorial Dispute

#include <iostream> #include<algorithm> #include<cstdio> #include <cstring> #include <cmath> using

ACM-ICPC國際大學生程式設計競賽北京賽區(2017)網路 Bounce

題目7 : Bounce 時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 For Argo, it is very interesting watching a circle bouncing in a rectangle. A

ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡 i題 Minimum(線段樹)

hellip each pri ger out ont amp define void 描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two typ

hihoCoder 1578 Visiting Peking University 【貪心】 (ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡

stdout ins nts coo csdn another head under sting #1578 : Visiting Peking University 時間限制:1000ms 單點時限:1000ms 內存限制:256MB 描述 Ming is

hihoCoder 1584 Bounce 【數學規律】 (ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡

media while def closed edge hiho problem reserve acm #1584 : Bounce 時間限制:1000ms 單點時限:1000ms 內存限制:256MB 描述 For Argo, it is very in

hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡

span per 則無 方案 plane ber != java res #1582 : Territorial Dispute 時間限制:1000ms 單點時限:1000ms 內存限制:256MB 描述 In 2333, the C++ Empire an

ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡 hihocoder #1586 : Minimum-區間查詢最值求區間兩數最小乘積+單點更新-線段樹(結構體版)

ns2 edit AD memory body bmi json ffffff inf #1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Descripti

hihocoder 1873 ACM-ICPC北京賽區2018重現 D Frog and Portal

http://hihocoder.com/problemset/problem/1873 時間限制:1000ms 單點時限:1000ms 記憶體限制:512MB 描述 A small frog wants to get to the other s

2015 ACM/ICPC北京賽區現場G

題意: 給你四個矩形,從中選出3個矩形【讀題的時候沒有看出只選三個矩形,交了三發罰時】,判斷是否可以組成一個大的矩形 分析: 暴力深搜,先全排列判斷順序。。然後判斷前兩個是否可以組成一個新的,然後把這個矩形傳遞下去 程式碼: #include<bits/s

2015 ACM/ICPC 北京賽區 現場 K —— A Math Problem【規律+數位dp】

題意: 給你兩個數n(n<=1e18),mod(mod=3,5,7,257,65537) 定義f(x): f(1)=1; 已知:3*f(n)*f(2*n+1)=f(2*n)*(1+3*f(n));            f(2*n)<6*f(n)

ACM-ICPC北京賽區2018重現 A題

具體思路:dfs,判斷矛盾就可以了。 AC程式碼: #include<iostream> #include<string> #include<cstring>

【區間DP】2017 ACM-ICPC北京賽區 - J - Pangu and Stones

題目連結<http://hihocoder.com/contest/icpcbeijing2017/problem/10> 題意: 石子歸併,每次能歸併[l,r]堆石子,每一次的歸併代價是所有要合併的石子數之和,問最小的代價是多少。 題解: 開一個數組dp[i

ACM-ICPC北京賽區2018重現I題Palindromes

記錄一下自己的思路等有空了再用程式碼實現。根據打表找到的規律. 1-10    9個 10-99    9個 100 999    90個 1000 9999   90個 10000 99999   900個 100000 999999   900個 也就是說,