Java的创建螺旋 [英] Java creation of a spiral

查看:127
本文介绍了Java的创建螺旋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  在螺旋循环

Possible Duplicate:
Looping in a spiral

我要创建一个程序一个3×3的矩阵来填充。我想导致一些看起来像这样

I'm creating a program to populate a 3 by 3 matrix. I want to result in something looking like this

5 4 3
6 1 2
7 8 9

正如你可能已经注意到这是一个恶性循环。 现在,我使用的算法是这样的:我有一个二维数组,其中值重新present数量的坐标。首先,我分配的每个数字在这个数组中的坐标将有10数值,然后开始在9我缩小x坐标,并指定的坐标currentnum的价值 - 1,直到达到结束或者其值不为10;然后,我做同样的事情,除非我增加Y的值;然后减少x的值;然后,ÿ;

As you have probably noticed it is a spiral. Now the algorithm I'm using is this: I have a 2-d array where the values represent the coordinates of the number. First I assign that every number coordinate in this array will have a value of 10. Then starting at 9 I decrease my x coordinate and assign the value of the coordinate to currentnum - 1 until it reaches the end or its value is not 10; Then I do the same thing except I increase the value of Y; Then decrease the value of x; Then of Y;

我给你10的原因,每一个号码是太喜欢它作为一个道路为我的计划。由于当前NUM将不会超过9。如果一个正方形的值是10它就像一个绿灯。如果不是10含义值已被分配给该方它打破了它。

The reason I assign 10 to every number is so like it acts as a road for my program. Since current num will never exceed 9. If the value of a square is 10 it is like a green light. If it is not 10 meaning a value has been assigned to that square it breaks out of it.

下面是我的code,请注意是用Java编写的

Here is my code, please note it is written in Java

public class spiral {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int spiral [] [] = new int[3][3];
        for(int i = 0; i <= 2; i++){
            for(int j = 0; j <= 2; j++){
                spiral[i][j] = 10;
            }
        }
        //0 is x value, 1 is y value
        spiral[0][0] = 9;
        int x = 1;
        int y = 1;
        int counter = 1;
        int currentnum = 9;
        int gridsquare  = 3;
        for(int i = 0; i <= 8; i++){

            if(counter == 5){
                counter = 1;
            }
            if(counter == 1){
                System.out.println(x + " " + y);
                for(int j = 0;j <= 1;j++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        x += 1;
                    }
                    else{
                        y += 1;
                        break;
                    }
                }
            }
            if(counter == 2){
                for(int k = 0; k <= 0; k++){
                    System.out.print(x + " " + y);
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        y += 1;
                    }
                    else{
                        x -= 1;
                        break;
                    }
                }
            }
            if(counter == 3){
                for(int z = 0; z <= 0; z++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        x -= 1;
                    }
                    else{
                        y -= 1;
                        break;
                    }
                }
            }
            if(counter == 4){
                for(int b = 0; b <= 0; b++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        y -= 1;
                    }
                    else{
                        x += 1;
                        break;
                    }
                }
            }
            counter++;
        }
        System.out.print(currentnum);
    }
}

我得到这个错误

I'm getting this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at spiral.main(spiral.java:44)

由于我是新来的Java会有人提出此一更多钞票修复。此外,如果你看到我的算法任何问题,请不要告诉我。

Since I'm new to Java would someone please suggest a posible fix for this. Also if you see any problems with my algorithm please do inform me.

推荐答案

您不必pre-充满10:零的作品一样好

You do not need to pre-fill with 10: zero works just as well.

我觉得解决螺旋最好的办法是想你怎么做手工:开始在一个角落里,去水平,直到你打不为零或阵列的边缘。然后右转。当停止当前一些去通过N *ñ。

I think the best approach to solving the spiral is to think of how you do it manually: start in a corner, and go horizontally until you hit non-zero or an edge of the array. Then you turn right. Stop when the current number goes past N*N.

现在让我们来看看什么是算法的每个部分的含义:

Now let's look at what each part of the algorithm means:

  • 开始在角落机构将x = 0和Y = 0。
  • 去在一条直线上装置X = X + DX,Y = Y + DY,其中DX任或镝是零,和dy或dx为1或-1。
  • 在谈到DX分配到Dy和-dy到DX正确的方式。

下面是如何看起来在code:

Here is how it looks in the code:

int current = 1;
// Start in the corner
int x = 0, y = 0, dx = 1, dy = 0;
while (current <= N*N) {
    // Go in a straight line
    spiral[x][y] = current++;
    int nx = x + dx, ny = y + dy;
    // When you hit the edge...
    if (nx < 0 || nx == N || ny < 0 || ny == N || spiral[nx][ny] != 0) {
        // ...turn right
        int t = dy;
        dy = dx;
        dx = -t;
    }
    x += dx;
    y += dy;
}

这篇关于Java的创建螺旋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆