//这部分是蛇形的主要算法
int n = 3;
int[][] a = new int[n][n];
int count = 1;
int y = n-1;
int x = 0;
a[x][y] = 1;
while (count<n*n) {
while (x+1<n&&!(a[x+1][y]>0)) {//右边的数累计
a[++x][y] = ++count;
}
while (y-1>=0&&!(a[x][y-1]>0)) {//最下边
a[x][--y] = ++count;
}
while (x-1>=0&&!(a[x-1][y]>0)) {
a[--x][y] = ++count;
}
while (y+1<n&&!(a[x][y+1]>0)) {
a[x][++y] = ++count;
}
}
蛇形填数
最新推荐文章于 2024-05-26 11:37:30 发布