1 2 3 8 9 4 7 6 5
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
package cn.dfeng;
import java.util.Arrays;
import java.util.Scanner;
public class Maze {
enum Direction{
UP, DOWN, RIGHT, LEFT;
}
public int[][] buidMaze( int n ){
int[][] maze = new int[n][n];
for( int[] a : maze ){
Arrays.fill(a, 0);
}
int col = 0;
int row = 0;
int counter = 1;
Direction d = Direction.RIGHT;
while( true ){
if( maze[row][col] == 0 ){
maze[row][col] = counter++;
switch (d) {
case RIGHT:
if( col + 1< n && maze[row][col + 1] == 0){
col ++;
}else{
d = Direction.DOWN;
row ++;
}
break;
case DOWN:
if( row + 1 < n && maze[row + 1][col] == 0){
row ++;
}else{
d = Direction.LEFT;
col --;
}
break;
case LEFT:
if( col - 1 >= 0 && maze[row][col-1] == 0){
col --;
}else{
d = Direction.UP;
row --;
}
break;
default:
if( row - 1 >= 0 && maze[row - 1][col] == 0){
row --;
}else{
d = Direction.RIGHT;
col ++;
}
break;
}
}else{
break;
}
}
return maze;
}
public void printMaze( int[][] maze ){
for( int[] row : maze ){
for( int i : row ){
System.out.printf("%3d", i);
}
System.out.println();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please input the size of the maze:");
int size = sc.nextInt();
Maze maze = new Maze();
int[][] m = maze.buidMaze( size );
maze.printMaze( m );
}
}
package cn.dfeng;
/**
* 该类能够用*打印大小的钻石图形
* @author dfeng
*
*/
public class Drawer {
/**
* 打印钻石图形
* @param n 钻石大小
*/
public void printDiamond( int n ){
System.out.println();
int i = 0;
boolean flag = true;
while( i >= 0 ){
if (i < n) {
for (int j = 0; j < n - i; j++) {
System.out.print(" ");
}
for (int j = n - i; j <= n + i; j += 2) {
System.out.print("* ");
}
System.out.println();
}
if (i == n) {
flag = false;
i--;
}
if (flag) {
i++;
} else {
i--;
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有