实现以下形式的输出
[1, 2, 3, 4, 5, 6]
[20, 21, 22, 23, 24, 7]
[19, 32, 33, 34, 25, 8]
[18, 31, 36, 35, 26, 9]
[17, 30, 29, 28, 27, 10]
[16, 15, 14, 13, 12, 11]]
源代码:
import java.util.*;
public class A {
static int numMove = 1;
static int x = 0;
static int y = 0;
static int[][] a = new int[6][6];
//初始化
public static void f(){
int i,j;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
a[i][j] = 0;
}
}
}
private static void moveForward() {
// TODO Auto-generated method stub
if(numMove==36){
a[x][y] = 36;
int i;
for(i=0;i<6;i++){
System.out.println(Arrays.toString(a[i]));
}
System.exit(0);
}
if((y==5)){
moveDown();
}
if (a[x][y+1]!=0) {
moveDown();
}
a[x][y] = (numMove);
System.out.println(x + " " + y+" "+numMove);
y = y+1;
numMove=numMove+1;
moveForward();
}
private static void moveDown() {
// TODO Auto-generated method stub
if(numMove==36){
a[x][y] = 36;
int i;
for(i=0;i<6;i++){
System.out.println(Arrays.toString(a[i]));
}
System.exit(0);
}
if((x==5)){
moveBack();
}
if (a[x+1][y]!=0) {
moveBack();
}
a[x][y] = (numMove);
System.out.println(x + " " + y+" "+numMove);
x = x+1;
numMove=numMove+1;
moveDown();
}
private static void moveBack() {
// TODO Auto-generated method stub
if(numMove==36){
a[x][y] = 36;
int i;
for(i=0;i<6;i++){
System.out.println(Arrays.toString(a[i]));
}
System.exit(0);
}
if((y==0)){
moveUp();
}
if (a[x][y-1]!=0) {
moveUp();
}
a[x][y] = (numMove);
System.out.println(x + " " + y+" "+numMove);
y = y-1;
numMove=numMove+1;
moveBack();
}
private static void moveUp() {
// TODO Auto-generated method stub
if(numMove==36){
a[x][y] = 36;
int i;
for(i=0;i<6;i++){
System.out.println(Arrays.toString(a[i]));
}
System.exit(0);
}
if((x==0)){
moveForward();
}
if (a[x-1][y]!=0) {
moveForward();
}
a[x][y] = (numMove);
System.out.println(x + " " + y+" "+numMove);
x = x-1;
numMove=numMove+1;
moveUp();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
f();
moveForward();
}
}