码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-12-01 11:05:10      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:code   ons   ==   main   for   using   模拟题   coder   r++   

#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-打印蛇形类似模拟题

标签:code   ons   ==   main   for   using   模拟题   coder   r++   

原文地址:https://www.cnblogs.com/--zz/p/10048040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!