Secret Poems 2017ACM-ICPC北京賽區/hihoCoder1632

題意: 題目意思就是將兩個不同規則的字符數組進行相互轉換。

思路: 將題目的解答分成兩部分,第一部分是將 figure 1 的數組轉換成一維數組,第二部分是將一維數組中的字符填入 figure 2 的數組中。

針對第一部分,將 figure 1 的二維數組旋轉四十五度再觀察,能夠發現奇數行是從左往右,偶數行是從右往左;上半部分中,每行邊上的結點移動是右、下不斷更替,下半部分中,每行邊上的結點移動是下、右不斷更替。

對於第二部分,通過對當前位置的判斷進行移動,直到填滿爲止。

代碼:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;

int N;
char G[110][110];
char ch[10010];
char ans[110][110];



int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%d", &N) == 1){
        for(int i=1; i<=N; i++){
            scanf("%s", &G[i][1]);
        }

        int x=1, y=1;
        int tot = 0;
        bool right = true;
        bool bhalf = true;
        for(int i=1; i<=2*N-1; i++){
            if(i == N) bhalf = false;
            ch[tot++] = G[x][y];
            for(int j=1; j<min(i, 2*N-i); j++){
                if(i & 1){
                    x--; y++;
                }
                else{
                    x++; y--;
                }
                ch[tot++] = G[x][y];
            }

            if(right){
                if(bhalf) y++;
                else x++;
            }
            else{
                if(bhalf) x++;
                else y++;
            }
            right = !right;
        }

        ch[tot] = '\0';
        //printf("%s\n", ch);

        memset(ans, 0, sizeof(ans));
        x = 1, y = 1;
        tot = 0;
        int mov = 0;  //0 表示向右,1 表示向下,2 表示向左,3 表示向上
        while(true){

            ans[x][y] = ch[tot++];
            if(tot == N*N) break;

            if(x==1&&y==N || isupper(ans[x-1][y])&&!isupper(ans[x+1][y])&&isupper(ans[x][y-1])&&isupper(ans[x][y+1])) mov = 1;
            else if(x==N&&y==N || isupper(ans[x-1][y])&&isupper(ans[x+1][y])&&!isupper(ans[x][y-1])&&isupper(ans[x][y+1])) mov = 2;
            else if(x==N&&y==1 || !isupper(ans[x-1][y])&&isupper(ans[x+1][y])&&isupper(ans[x][y-1])&&isupper(ans[x][y+1])) mov = 3;
            else if(y==1&&isupper(ans[x-1][y]) || isupper(ans[x-1][y])&&isupper(ans[x+1][y])&&isupper(ans[x][y-1])&&!isupper(ans[x][y+1])) mov = 0;

            if(mov == 0) y++;
            else if(mov == 1) x++;
            else if(mov == 2) y--;
            else if(mov == 3) x--;
        }

        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                putchar(ans[i][j]);
            }
            putchar('\n');
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章