java 迷宫1

package com.my.test;

import java.util.Arrays;
import java.util.Scanner;
/**
 * @author Administrator
 *   4       3         2
 * 1111111 11111
 * 1222221 12221    
 * 1233321 12321  -> -> 111 
 * 1234321 12221        121
 * 1233321 11111        111
 * 1222221
 * 1111111
 * 的算法
 */
public class Test1 {

	private enum direct{
		left,right,up,down
	}
	public void init(){
		
		Scanner sc  = new Scanner(System.in);
		System.out.print("请输入回旋的阶数:");
		String level = "1";
		Integer level_no = 1; 
		try {
			level = sc.next();
			level_no = Integer.parseInt(level);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		
		next_step1(level_no);
		
	}
	private void next_step1(Integer level_no) {
		int high = level_no * 2 -1;
		String[][] nums = new String[high][high];
		if(high == 1){
			for (int i = 0; i < nums.length; i++) {
				for(int j = 0;j<nums[i].length;j++){
					nums[i][j] = add_zero(1, level_no);
				}
			}
		}else{
			int temp = 1;
			for (int i = 0; i < nums.length; i++) {
				
				if(i < (high / 2  )){
					//上半部分
					int t1 = 1;
					for(int j = 0;j<nums[i].length;j++){
						
						if(i == 0){
							nums[i][j] = add_zero(t1,level_no);
						}else{
							if(j < (high / 2 + 1)){
								
								nums[i][j] = add_zero(t1,level_no);
								t1++;
								if(t1>=temp){
									t1 = temp;
								}
							}else{
								if(t1>=temp){
									t1=temp;
								}
								nums[i][j] = add_zero(t1,level_no);
								if(j >= (high - temp)){
									t1--;
								}
							}
						}
					}	
					temp++;
				}else if(i == (high / 2 )){
					int t1 = 1;
					for(int j = 0;j<nums[i].length;j++){
						if(j < (high / 2 + 1)){
							
							nums[i][j] = add_zero(t1,level_no);
							t1++;
							if(t1>=temp){
								t1 = temp;
							}
						}else{
							if(t1>=temp){
								t1=temp;
							}
							t1--;
							nums[i][j] = add_zero(t1,level_no);
						}
					}
					
				}else{
					//下半部分
					temp--;
					int t1 = 1;
					for(int j = 0;j<nums[i].length;j++){
						if(j < (high / 2 + 1)){
							
							nums[i][j] = add_zero(t1,level_no);
							t1++;
							if(t1>=temp){
								t1 = temp;
							}
						}else{
							if(t1>=temp){
								t1=temp;
							}
							nums[i][j] = add_zero(t1, level_no);
							if(j >= (high - temp)){
								t1--;
							}
						}
					}	
				}
				
			}
		}
		for (int i = 0; i < nums.length; i++) {
			
			System.out.println(Arrays.toString(nums[i]));
		}
	}
	
	private String add_zero(Integer t1,int level_no) {
		 Integer temp = level_no;  
	        String s = temp.toString();   
	        Integer len = s.length();  
	        Integer l = t1.toString().length();  
	        String zero = "";  
	        for(int i = 1;i<=len -l;i++ ){  
	            zero = zero + "0";  
	        }  
	        return zero + t1;   
	}
	public static void main(String args[]){
		new Test1().init();
	}
}

你可能感兴趣的:(java,算法,J#,UP)