描述
给定一个h行h列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序赋值整个数组。如图所示:
![图片描述][1]
输出结果:4 4
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int h = scanner.nextInt();//输入二维数组长度
int[][] arr = new int[h][h];//创建二维数组
setWay(arr,0,0);//赋值
for (int[] p : arr) {//遍历
for (int p1 : p) {
System.out.printf("%-4d",p1);
}
System.out.println();
}
System.out.println(t);
}
static int p = 1;//所赋的值
static int f = 0;//控制转向 0为右 1为下 2为左 3为上
static int t = 0;//纪录函数调用次数
public static boolean setWay(int[][]map,int i,int j){
if(i==map.length || j ==map.length || i == -1 || j == -1