zoukankan      html  css  js  c++  java
  • Secret Poems

    Secret Poems

    原题链接
    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:

    alt text

    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

    [思路]:题意是给出一个图一的矩阵,你要先解码成一句话,再按照图二的方法加密,大力模拟即可
    首先我们可以先发现规律,只有到第一行或者第一列 或者最后一行或者最后一列才会变化方向,那么
    我们总结下规律可以弄出一句话,然后再dfs出答案,附上代码
    
    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 105;
    char maps[MAXN][MAXN];
    char results[MAXN][MAXN];
    int moves[4][2] = {{0, 1},{1, 0},{0, -1},{-1, 0}};
    int vids[MAXN][MAXN], n;
    vector<char> vec;
    void dfs(int step, int fx, pair<int, int>p){
        if(vids[p.first][p.second] == 0)
        results[p.first][p.second] = vec[step];
        vids[p.first][p.second] = 1;
        if(step == vec.size()){
            return ;
        }
        if(p.first + moves[fx][0] < 0 || p.first + moves[fx][0] >= n || p.second + moves[fx][1] < 0 || p.second + moves[fx][1] >= n || vids[p.first + moves[fx][0]][p.second + moves[fx][1]] == 1){
            pair<int, int>x = p;
            x.first += moves[(fx + 1) % 4][0], x.second += moves[(fx + 1) % 4][1];
            dfs(step + 1, (fx + 1) % 4, x);
        }
        else{
            pair<int, int>x = p;
            x.first += moves[fx][0], x.second += moves[fx][1];
            dfs(step + 1, fx, x);
        }
    }
    int main() {
      while (~scanf("%d", &n)) {
        for (int i = 0; i < n; i++) {
          scanf("%s", maps[i]);
        }
        memset(vids, 0, sizeof(vids));
        memset(results, 0, sizeof(results));
        int x = 3 + 4 * (n - 2);
        vec.clear();
        pair<int, int> p;
        p.first = 0, p.second = 0;
        vec.push_back(maps[p.first][p.second]);
        int flag = 0, maxs = 1, f = 0, xx = 1;
        for (int i = 0; i < x; i++) {
          if ((flag % 2 == 1) && f % 2 == 0) {
            for (int j = 0; j < maxs; j++) {
              p.first += 1, p.second -= 1;
              vec.push_back(maps[p.first][p.second]);
            }
            if (maxs == n - 1) {
              xx = -1;
            }
            maxs += xx;
            f++;
            flag++;
            continue;
          }
          if ((flag % 2 == 1) && f % 2 == 1) {
            for (int j = 0; j < maxs; j++) {
              p.first -= 1, p.second += 1;
              vec.push_back(maps[p.first][p.second]);
            }
            if (maxs == n - 1) xx = -1;
            maxs += xx;
            f++;
            flag++;
            continue;
          }
          if (p.first == 0 && p.second == n - 1 && (flag % 2) == 0) {
            p.first = p.first + 1;
            vec.push_back(maps[p.first][p.second]);
            flag++;
            continue;
          }
          if (p.first == 0 && (flag % 2) == 0) {
            p.second = p.second + 1;
            vec.push_back(maps[p.first][p.second]);
            flag++;
            continue;
          }
          if (p.first == n - 1 && (flag % 2) == 0) {
            p.second = p.second + 1;
            vec.push_back(maps[p.first][p.second]);
            flag++;
            continue;
          }
          if ((p.second == 0 || p.second == n - 1) && (flag % 2) == 0) {
            p.first = p.first + 1;
            vec.push_back(maps[p.first][p.second]);
            flag++;
            continue;
          }
        }
        pair<int, int>be;
        be.first = 0, be.second = 0;
        dfs(0, 0, be);
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                cout << results[i][j];
            }
            cout << endl;
        }
      }
      return 0;
    }
    
  • 相关阅读:
    类的加载机制与反射 笔记
    json
    人人都是产品经理 笔记
    java IO
    jcrop+jquery+javaweb 实现头像裁剪、上传
    C++学习-3
    C++学习-2
    C++学习-1
    Django高级部分
    Django---模板
  • 原文地址:https://www.cnblogs.com/qq136155330/p/11717287.html
Copyright © 2011-2022 走看看