zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    android 圆角图片的实现
    navigationView 的使用和布局文件的绑定
    android listview 的监听事件
    android第三方框架 xlistview 的使用
    android Baseadapter 和 ViewHolder的使用
    android 调用电话功能
    android 颜色对照
    Android_menu_SubMenu
    Android_menu_optionMenu
    Android_Menu_contextMenu
  • 原文地址:https://www.cnblogs.com/--zz/p/10048040.html
Copyright © 2011-2022 走看看