hihocoder 1632-打印蛇形类似模拟题

Posted --zz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hihocoder 1632-打印蛇形类似模拟题相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <algorithm> 
using namespace std;
const int MAXN = 101;
    int N;
    char poem[MAXN][MAXN];
    char trans[MAXN*MAXN];
    int p1DirR[] = {+0,+1,+1,-1};
    int p1DirD[] = {+1,-1,+0,+1};
    int p2DirR[] = {+0,+1,+0,-1};
    int p2DirD[] = {+1,+0,-1,+0};
    
int main(){
    while(cin >> N){
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cin >> poem[i][j];
            }
        }
        int x=0,y=0;
        int toX,toY;
        int nowDir = 0;
        trans[0] = poem[0][0];
        for(int i=1;i<N*N;i++){
            toX = x + p1DirR[nowDir];
            toY = y + p1DirD[nowDir];
            while(toX<0 || toX>=N || toY<0 || toY>=N || poem[toX][toY] == '0' ){
                nowDir = (nowDir+1)%4;
                toX = x + p1DirR[nowDir];
                toY = y + p1DirD[nowDir];
            }
            trans[i] = poem[toX][toY];
            poem[toX][toY] = '0';
            x = toX;
            y = toY;
            if(nowDir == 0 || nowDir == 2){
                nowDir++;
            }
        }
    
        //-------------------
        x = y = nowDir =0;
        poem[0][0] = trans[0];
        for(int i=1;i<N*N;i++){
            toX = x + p2DirR[nowDir];
            toY = y + p2DirD[nowDir];
            while(toX<0 || toX>=N || toY<0 || toY>=N || poem[toX][toY]!= '0'){
                nowDir = (nowDir+1)%4;
                toX = x + p2DirR[nowDir];
                toY = y + p2DirD[nowDir];
            }
            poem[toX][toY] = trans[i];
            x = toX;
            y = toY;
        }
        
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cout << poem[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}

以上是关于hihocoder 1632-打印蛇形类似模拟题的主要内容,如果未能解决你的问题,请参考以下文章

hihoCoder 1632 Secret Poems(ACM-ICPC北京赛区2017网络同步赛)

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

蛇形数组和“之”字形打印

蛇形填数和蛇形取数(基础模拟练习)

打印矩阵1--蛇形矩阵

[算法]循环打印矩阵,蛇形矩阵专题