本帖最后由 肖瑞军 于 2011-11-22 16:03 编辑
public class ScrewSquares {
public static void main(String[] args) {
int w =4;
int h =4;
int[][] array = new int[w][h];
fill(array, 0, 0, w-1, h-1, 1);
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
System.out.printf("%2d ", array[x][y]);
}
System.out.println();
}
}
public static void fill(int[][] array, int left, int top, int right, int bottom, int start) {
if (left > right || top > bottom) return;
for (int x=left; x<=right; x++)
array[x][top] = start++;
for (int y=top+1; y<=bottom; y++)
array[right][y] = start++;
for (int x=right-1; x>=left; x--)
array[x][bottom] = start++;
for (int y=bottom-1; y>=top+1; y--)
array[left][y] = start++;
fill(array, left+1, top+1, right-1, bottom-1, start);//递归调用
}
}
|