package com.hcysoul;
import java.util.Scanner;
public class Main{
static int n;
static int[][] target;
static char direction = 'r';
static int column = 0, row = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
n = input.nextInt();
target = new int[n][n];
fillAll();
show();
}
private static void fillAll() {
// TODO Auto-generated method stub
int max = n * n;
target[0][0] = 1;
for (int num = 2; num <= max; num++) {
setLocation();
fill(num);
}
}
private static void setLocation() {
// TODO Auto-generated method stub
if (direction == 'r') {
column++;
if (column < n && target[row][column] == 0) {
return;
} else {
column--;
row++;
direction = 'd';
return;
}
}
if (direction == 'd') {
row++;
if (row < n && target[row][column] == 0) {
return;
} else {
row--;
column--;
direction = 'l';
return;
}
}
if (direction == 'l') {
column--;
if (column >= 0 && target[row][column] == 0) {
return;
} else {
column++;
row--;
direction = 'u';
return;
}
}
if (direction == 'u') {
row--;
if (row >= 0 && target[row][column] == 0) {
return;
} else {
direction = 'r';
row++;
column++;
return;
}
}
}
private static void fill(int num) {
// TODO Auto-generated method stub
target[row][column] = num;
}
private static void show() {
// TODO Auto-generated method stub
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(target[i][j] + " ");
}
System.out.println();
}
}
}
转载于:https://my.oschina.net/hollowj/blog/729227