求螺旋矩阵的最佳算法

qazxsw1982103 2004-07-17 02:22:29
求螺旋矩阵的最佳算法,这里指的最佳算法是相对复杂度而言的,
各位可以把好的解题思想留下。

以下是我写的:
import java.io.*;

class ScrewMatrix
{
public static void main(String[] args)
{
int width=10; int height=9; //初始化矩阵的行和列,可改变
int r=width*height+1;
int w=width; int h=height-1;
int n=0; int g=0;
int p=h-1; int m=width;
int count=1; int f=0;
int []arr=new int[r-1];

while(true)
{
for(int i=0;(i<w)&&(count!=r);++i,++count)
arr[width*n+i+(width-m)-f]=count;

for(int i=0;(i<h)&&(count!=r);++i,++count)
arr[width*(height-p+i)-g-1]=count;

for(int i=0;(i<(w-1))&&(count!=r);++i,++count)
arr[width*(h+g+1)-g-1-i-1]=count;

for(int i=0;(i<(h-1))&&(count!=r);++i,++count)
arr[width*(p-i)+g+1-1]=count;

if(count==r)
break;

--p; m-=2; ++n; ++g; h-=2; w-=2; ++f;

}

for(int i=0;i<arr.length;++i)
{
if(i%(width)==0)
System.out.println();
System.out.print(" "+arr[i]+'\t');
}

System.out.println();
}
}

感觉做的有些‘笨’,就是改变数组的坐标值来存储count

...全文
333 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qazxsw1982103 2004-07-17
  • 打赏
  • 举报
回复
good
darkula 2004-07-17
  • 打赏
  • 举报
回复
/*
* 基本思路:采用数学方法直接计算出矩阵元素P(x,y)的值.
* 将整个矩阵看成由外道内的矩形圈组成,矩阵中任意一点P(x,y)的值=
* 位于外圈的所有点数+位于本圈上所有点数+1
*/
public class Martrix
{
private int H;
private int L;

public static void main(String argv[])
{
Martrix m1=new Martrix(6,6);
m1.print();
}

public Martrix()
{
this.H=3;
this.L=3;
}
public Martrix(int x,int y)
{
this.H=x;
this.L=y;
}

//计算处于点p(x,y)前面的所有点数
public int getDotCount(int x,int y)
{
return getDotCount1(H,L,x,y) + getDotCount2(H,L,x,y) + 1;
}

//计算点P(x,y)外围矩形圈数
public static int getN(int H,int L,int x,int y)
{
return Math.min(Math.min(x,y),Math.min((H-x),(L-y)));
}

//计算点P(x,y)外围矩形圈上的点数 (等差数列 )
public static int getDotCount1(int H,int L,int x,int y)
{
int N=getN(H,L,x,y);
int S1=2*(H+L);
int S2=2*(H+L)-8*N + 8;
return (S1+S2)/2 * N;
}

//计算与点P(x,y)处于同一个矩形圈上,且在点P前面的所有点数 (分段函数)
public static int getDotCount2(int H,int L,int x,int y)
{
int N=getN(H,L,x,y);
int x1=x-N;
int y1=y-N;
int H1=H-2*N;
int L1=L-2*N;
int count;


if(L1==0) //特列:H >=L ,L为奇数,y=(L-1)/2 实例(H=6,L=5,x=3,y=3)
return x1 ;

if(H1==0) //特列: H < L ,H为奇数,x=(H-1)/2 实例(H=5,L=6,x=3,y=3)
return y1;

if(y1==0) //一般情况:
{
count=x1;
}
else if(x1==H1) //一般情况:
{
count=H1 + y1;
}
else if(y1==L1) //一般情况:
{
count=H1 + L1 + (H1 - x1);
}
else if(x1==0) //一般情况:
{
count=H1 + L1 + (H1 - x1) + (L1 - y1);
}
else //出错情况:
{
count= -1;
}

return count;
}

//计算并输出螺旋矩阵
public void print()
{
System.out.println("H = " + H +" , " + "L = " + L);
System.out.println("******************************************");
for(int j=0;j<=L;j++)
{
for(int i=0;i<=H;i++)
{
System.out.print(getDotCount(i,j)+" ");
}
System.out.println();
}
System.out.println("************************************");
}
}
darkula 2004-07-17
  • 打赏
  • 举报
回复
记起了,好象是在www.chinajavaworld.net里看到过的,那里的精华区的,不过最近打不开
------------------------------------------------
/*
* Copyright (c) 2003 novemberfirst@hotmail.com
* Nov. 19, 2003 for ChinaJavaWorld.net
* Permission is hereby granted, free of charge, to any one for any purpose.
*/
public interface CircleMatrix
{
public int getHeight();
public int getWidth();
public int get(int x, int y);
}
/*
* Copyright (c) 2003 novemberfirst@hotmail.com
* Nov. 19, 2003 for ChinaJavaWorld.net
* Permission is hereby granted, free of charge, to any one for any purpose.
*/
public class CircleMatrixTester
{
static public void main(String[] args)
{
int h = Integer.parseInt(args[0]);
int w = Integer.parseInt(args[1]);

CircleMatrix cmOne = new CircleMatrixSmartestImpl(h, w);
printMatrix("CircleMatrixSmartestImpl", cmOne);

CircleMatrix cmTwo = new CircleMatrixShortestImpl(h, w);
printMatrix("CircleMatrixShortestImpl", cmTwo);
}

static public void printMatrix(String title, CircleMatrix matrix)
{
System.out.println();
System.out.println(title);

for(int row = 0, rowSize = matrix.getHeight(); row < rowSize; row++)
{
for(int col = 0, colSize = matrix.getWidth(); col < colSize; col++)
{
int n = matrix.get(col, row);

String strN;
if(n < 10)
strN = " " + n;
else if (n < 100)
strN = " " + n;
else
strN = String.valueOf(n);

System.out.print(strN);
System.out.print(' ');
}
System.out.println();
}
}
}

/*
* Copyright (c) 2003 novemberfirst@hotmail.com
* Nov. 19, 2003 for ChinaJavaWorld.net
* Permission is hereby granted, free of charge, to any one for any purpose.
*/
public class CircleMatrixSmartestImpl
implements CircleMatrix
{
private int height;
private int width;
private int[] circleSum;

public CircleMatrixSmartestImpl(int height, int width)
{
if(height <= 0 || width <= 0)
throw new IllegalArgumentException("negative height and/or width: " + height + ", " + width);

this.height = height;
this.width = width;

buildCircleSum(width, height);
}

public int getHeight()
{
return height;
}

public int getWidth()
{
return width;
}

public int get(int x, int y)
{
if(x < 0 || x >= width || y < 0 || y >= height)
return -1;

int startN = 1;
int startX = 0, endX = width - 1;
int startY = 0, endY = height - 1;

int c = Math.min(Math.min(Math.min(y - startY, endY - y), x - startX), endX - x);
if(c > 0)
{
startN += circleSum[c];
startX += c;
endX -= c;
startY += c;
endY -= c;
}

int n = startN;
if(y == startY)
return n + x - startX;

n += endX - startX;
if(x == endX)
return n + y - startY;

n += endY - startY;
if(y == endY)
return n + endX - x;

n += endX - startX;
if(x == startX)
return n + endY - y;

return -2; // never happen
}

private void buildCircleSum(int w, int h)
{
int size = (Math.min(h, w) + 1) / 2;
circleSum = new int[size];
if(size > 1)
{
circleSum[0] = 0;
for(int k = 1; k < size; k++, h -= 2, w -= 2)
circleSum[k] = circleSum[k - 1] + h + h + w + w - 4;
}
}
}

/*
* Copyright (c) 2003 novemberfirst@hotmail.com
* Nov. 19, 2003 for ChinaJavaWorld.net
* Permission is hereby granted, free of charge, to any one for any purpose.
*/
public class CircleMatrixShortestImpl
implements CircleMatrix
{
static private final int[][] incParams = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
static private final int[][] sizeParams = new int[][]{{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

private int[][] matrix;

public CircleMatrixShortestImpl(int height, int width)
{
if(height <= 0 || width <= 0)
throw new IllegalArgumentException("negative height and/or width: " + height + ", " + width);

buildMatrix(height, width);
}

private void buildMatrix(int h, int w)
{
matrix = new int[h][w];

int[][] points = new int[][]{{0, 0}, {w - 1, 0}, {w - 1, h - 1}, {0, h - 1}};
int index = 0, index2 = 1, number = 0, totalNumber = h * w;

while(number < totalNumber)
{
for(int x = points[index][0], y = points[index][1], incX = incParams[index][1], incY = incParams[index][0],
endX = points[index2][0] + incX, endY = points[index2][1] + incY
; x != endX || y != endY; x += incX, y += incY)
{
matrix[y][x] = ++number;
}

points[index][0] += sizeParams[index][0];
points[index2][0] += sizeParams[index][0];
points[index][1] += sizeParams[index][1];
points[index2][1] += sizeParams[index][1];

index = index2;
index2 = (index + 1) & 3;
}
}

public int getHeight()
{
return matrix.length;
}

public int getWidth()
{
return matrix[0].length;
}

public int get(int x, int y)
{
if(x < 0 || x >= getWidth() || y < 0 || y >= getHeight())
return -1;

return matrix[y][x];
}
}

-------------------------------------------------------------
darkula 2004-07-17
  • 打赏
  • 举报
回复
手里有几个螺旋算法的代码,贴过来你看下吧,不知帮的上不
-----------------------
public class HelicalVector {
private int x;
private int y;
private int width;
private int height;
private String[][] value = null;

public HelicalVector(int width, int height) {
this.width = width;
this.height = height;
value = new String[height][width];
}

public String getVlaue(int x, int y) {
StringBuffer sb = new StringBuffer(6);
for (int i = 0, n = 6 - value[x][y].length(); i < n; i++) {
sb.append(" ");
}
sb.append(value[x][y]);
return sb.toString();
}

public void setValue(String val) {
value[x][y] = val;
}

public void buildVector() {
int direct = 0; // 以4为基数
for (int val = 1; val <= width*height; val++) {
setValue(String.valueOf(val));
direct %= 4;
switch (direct) {
case 0:
if (y == width-1 || value[x][y+1] != null) {
direct++; //换方向
x++;
} else
y++;
break;
case 1:
if (x == height-1 || value[x+1][y] != null) {
direct++; //换方向
y--;
} else
x++;
break;
case 2:
if (y == 0 || value[x][y-1] != null) {
direct++; //换方向
x--;
} else
y--;
break;
case 3:
if (x == 0 || value[x-1][y] != null) {
direct++; //换方向
y++;
} else
x--;
break;
}
}
}

public void printVector() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(getVlaue(i, j));
}
System.out.println();
}
}

public static void main(String[] args) {
HelicalVector vector = new HelicalVector(7, 8);
vector.buildVector();
vector.printVector();
}

}
--------------------------------------------
qazxsw1982103 2004-07-17
  • 打赏
  • 举报
回复
好像是在压缩方面

1 2 3 4 5
18 19 20 21 6
17 28 29 22 7
16 27 30 23 8
15 26 25 24 9
14 13 12 11 10
zhengy2003 2004-07-17
  • 打赏
  • 举报
回复
螺旋矩阵用在哪个方面?
qazxsw1982103 2004-07-17
  • 打赏
  • 举报
回复
顶顶顶顶顶顶
qazxsw1982103 2004-07-17
  • 打赏
  • 举报
回复
顶顶顶顶
qazxsw1982103 2004-07-17
  • 打赏
  • 举报
回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧