地址:http://poj.org/problem?id=3752
代码:
import java.util.Scanner;
public class Main {
static int[][] num;
static int count = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M, N;// 行和列
while (in.hasNext()) {
M = in.nextInt();
N = in.nextInt();
num = new int[M][N];
count = 0;
// // 初始化
// for (int i = 0; i < M; i++) {
// for (int j = 0; j < N; j++) {
// num[i][j] = 0;
// }
// }
int k = 0;
int m = M, n = N;
while (M > 0) {
keyMethod(M, N, k);
M -= 2;
N -= 2;
k++;
}
show(m, n);
}
}
public static void keyMethod(int m, int n, int k) {// m行和n列k为每次初始顶点
if (1 == m && 1 == n) {
num[k][k] = count;
return;
}
for (int i = 0; i < n; i++) {
num[k][k + i] = count;
count++;
}
for (int i = 1; i < m; i++) {
num[k + i][k + n - 1] = count;
count++;
}
if (1 != m) {
for (int i = k + n - 2; i > k - 1; i--) {
num[k + m - 1][i] = count;
count++;
}
}
if (1 != n) {
for (int i = k + m - 2; i > k; i--) {
num[i][k] = count;
count++;
}
}
}
public static void show(int M, int N) {
char c;
//
// for (int i = 0; i < M; i++) {
// for (int j = 0; j < N; j++) {
// if (j != N - 1) {
// System.out.print(num[i][j]);
// System.out.print(" ");
// } else {
// System.out.println(num[i][j]);
// }
// }
// }
// // System.out.println("\n\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
c = (char) ((num[i][j] % 26) + 'A');
if (j != N - 1) {
//System.out.print(c);
System.out.print(" "+c);
} else {
System.out.println(" "+c);
}
}
}
}
}