您的位置:首页 > 其它

蛇形矩阵

2016-05-24 16:14 519 查看
生成并打印如下蛇形矩阵:



可以把矩阵想像成若干个圈,从外到内依次生成并打印蛇形矩阵。

对于一个4×4的矩阵,最后一圈有4个数字,其左上角的坐标是(1,1),我们发现4>1×1;

对于一个5×5的矩阵,最后一圈只有一个数字,对应坐标为(2,2),我们发现5>2×2依然成立。

所以可以得出让循环的继续条件为:维数dime > start * 2。

打印可以分为4步:从左到右生成一行,从上到下生成一列,从右到左生成一行,从下到上生成一行。

测试程序中设置了矩阵可以接受的最大维数和非法值判断(负值、0、1)。

Java源代码如下:

package array;

/**
* @author WuPing
* @version 2016年4月28日 上午9:59:14
*/

public class SnakeMatrix {

public static int[][] CreateSnakeMatrix(int dime) {
int[][] snakeMatrix = new int[dime][dime];

int count = 1; // 蛇形矩阵赋值计数
int start = 0;

while (dime > start * 2) {
int endX = dime - 1 - start;
int endY = dime - 1 - start;

// 从左到右生成一行
for (int i = start; i <= endX; ++i) {
snakeMatrix[start][i] = count;
count++;
}

// 从上到下生成一列
for (int i = start + 1; i <= endY; ++i) {
snakeMatrix[i][endX] = count;
count++;
}

// 从右到左生成一行
for (int i = endX - 1; i >= start; --i) {
snakeMatrix[endY][i] = count;
count++;
}

// 从下到上生成一行
for (int i = endY - 1; i >= start + 1; --i) {
snakeMatrix[i][start] = count;
count++;
}

++start;
}
return snakeMatrix;
}

public static void PrintSnakeMatrix(int[][] snakeMatrix, int dime) {
for (int i = 0; i < dime; i++) {
for (int j = 0; j < dime; j++) {
System.out.print(" " + snakeMatrix[i][j] + " ");
}
System.out.println();
}
}

public static void Test(int dime) {
int DimeMax = 50;   //设置矩阵最大维数,防止二维数组存储空间溢出
if (dime < 2 || dime > DimeMax) {
System.out.println("矩阵维数非法!");
return;
}
int[][] snakeMatrix = CreateSnakeMatrix(dime);
System.out.println(dime +"维蛇形矩阵:");
PrintSnakeMatrix(snakeMatrix, dime);
System.out.println();
}

public static void main(String[] args) {
Test(1);   //非法值测试,1
Test(0);   //非法值测试,0
Test(-1);   //非法值测试,负值
Test(100);   //非法值测试,超过上界

Test(5);   //中间值测试
Test(8);   //中间值测试
Test(2);   //下界值测试
Test(50);   //上界值测试
}
}


结果截图:



附录:

来个好玩的螺旋矩阵。

来源博文:螺旋队列算法分析/article/8060430.html

螺旋矩阵示例如下:



Java实现代码:

package array;

/**
* @author WuPing
* @version 2016年4月28日 下午3:46:44
*/

public class SpiralqMatrix {

private static int Max(int a1, int a2) {
return a1 < a2 ? a2 : a1;
}

private static int Abs(int x) {
return x < 0 ? -x : x;
}

public static void PrintSpiralMatrix(int x, int y) {
int c = Max(Abs(x), Abs(y));    // 当前坐标所在圈
int max = (c * 2 + 1) * (c * 2 + 1);// 当前圈上最大值
int number = 0;
if (y == -c) {  //上边
number = max + (x + y);
} else if (x == -c) {  //左边
number = max + (3 * x - y);
} else if (y == c) {  // 下边
number = max + (-x - 5 * y);
} else {    // 右边
number = max + (-7 * x + y);
}

System.out.print(" " + number + " ");
}

public static void Test(int dime) {
int DimeMax = 20;   //设置矩阵最大维数,防止二维数组存储空间溢出
if (dime < 1 || dime > DimeMax) {
System.out.println("矩阵维数非法!");
return;
}
System.out.println(dime*2+1 +"维螺旋矩阵:");
for(int y=-dime; y<=dime; y++) {
for(int x=-dime; x<=dime; x++) {
PrintSpiralMatrix(x, y);
}
System.out.println();
}
}

public static void main(String[] args) {
Test(0);   //非法值测试,0
Test(-1);   //非法值测试,负值
Test(60);   //非法值测试,超过上界

Test(1);   //下界值测试
Test(2);   //中间值测试
Test(8);   //中间值测试
Test(20);   //上界值测试
}
}


程序运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: