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;
}
posted @ 2018-12-01 02:01  zz2108828  阅读(186)  评论(1编辑  收藏  举报