2013-10-15 67 views
1

问题已经在计算器之前讨论的那样,我专门询问意见或回答关于我的代码和它是否能与不平衡的2D阵列,而无需大修。无法打印某些平衡阵列的末端的原因必定是一些小问题。 更新在底部的Java 2D阵列螺旋/顺时针遍历

基本上我们有由命令行驱动的文本文件中提供的2D阵列。该文件每次尝试都用换行符隔开,如下所示:rows; columns; values(white space delimited)

示例:4; 4; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

输出:1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package spiralprinting; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

/** 
* 
* @author Paul 
*/ 
public class SpiralPrinting { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws FileNotFoundException, IOException { 
     // TODO code application logic here 
     File file = new File(args[0]); 
     BufferedReader in = new BufferedReader(new FileReader(file)); 
     String line; 
     while ((line = in.readLine()) != null) { 

      String[] lineArray = line.split(";"); 
      if (lineArray.length > 0) {//ignore blank line inputs 
       //Process line of input Here 

       //Max ,minimum, and current indexes in our matrix. 
       int maxX = Integer.parseInt(lineArray[0]) - 1; 
       int maxY = Integer.parseInt(lineArray[1]) - 1; 
       int minX = 0; 
       int minY = 0; 
       int x = 0; 
       int y = 0; 

       //Build our matrix 
       String[] valueArray = lineArray[2].split("\\s"); 
       String[][] matrix = new String [Integer.parseInt(lineArray[0])][Integer.parseInt(lineArray[1])]; 
       int count = 0; 

       for (int j = 0; j <= maxY; j++){ 
        for (int i = 0; i <= maxX; i++){ 
         matrix[i][j] = (valueArray[count]); 
         count++; 
        } 
       } 

       StringBuilder printString = new StringBuilder(); 
       //Traverse and print our matrix in a spiral! 
       while (maxX > minX && maxY > minY){ 
        //Leaving this in and commented so you can see my train of thought. 

        if (x != maxX){ 
         while (x < maxX){ 
          printString.append(matrix[x][y]).append(" "); 
          x++; 
         }maxX--; 
        } 
        if (y != maxY){ 
         while (y < maxY){ 
          printString.append(matrix[x][y]).append(" "); 
          y++; 
         }maxY--; 
        } 
        if (x != minX){ 
         while (x > minX){ 
          printString.append(matrix[x][y]).append(" "); 
          x--; 
         }minX++; 
        } 
        if (y != minY){ 
         while (y > minY){ 
          printString.append(matrix[x][y]).append(" "); 
          y--; 
         }minY++; 
        } 
        //One border done (4 passes). Next iteration of while-loop begins. 
        x = minX; 
        y = minY; 
       }//end of our traversal loop 
       //Print it ! 
       System.out.println(printString.toString().trim()); 
      } 
     }//end of input line analysis 
    } 
}//end of class 

样品输入和电流输出:

4; 4; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ---> 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 好的

3; 3; 1 2 3 4 5 6 7 8 9 ---> 1 2 3 6 9 8 7 4 这无法打印5

3; 4; 1 2 3 4 5 6 7 8 9 10 11 12 ---> 1 2 3 6 9 12 11 10 7 4 ..无法打印5,8末端...

4; 3; 1 2 3 4 5 6 7 8 9 10 11 12 ---> 1 2 3 4 8 12 11 10 9 5 ..不能打印最后2个:6,7“

2; 10; 1 ... ... 20 ---> 1,2,4,6,8 .... goo d

一些快速的修改之后,我的问题似乎是,它不是为一些集打印最后2。我相信这是一个特殊的情况,我要睡觉就可以了:)

任何帮助仍然是赞赏,特别是如果你认为这个问题是大于我目前认为。我困了大脑认为我需要2个特殊情况与我在while循环4个检查一起去......

谢谢=]

回答

3

当你调试的东西,你真的不能换你的头脑周围怎么回事错......将其分解成易于调试的东西,抛出你的硬测试用例,并尝试一些非常简单的东西,然后移动到更难的地方,找到它破坏的地方,继承人如何找到它。

我注释掉了所有的代码,你输入的文件,并提出您的输入固定字符串:

String[] lineArray = ("3;2;" + 
          "1 2 3 " + 
          "6 5 4 ").split(";"); 
// see how the output should be 123456...very easy to see and debug 

while (maxX > minX || maxY > minY)把一个断点,我看着矩阵阵列,看到该基质的大小为2x3而不是3x2,我的号码没有存储,我认为他们应该是。并发现瞧问题。

/*if (maxY >= maxX){*/ 
    // This for loop is what you want 
    for (int j = 0; j <= maxY; j++){ 
     for (int i = 0; i <= maxX; i++){ 
      matrix[i][j] = (valueArray[count]); 
      count++; 
     } 
    } 
/*}/*delete this junk/ else if (maxX > maxY){ 
    for (int i = 0; i <= maxX; i++){ 
     for (int j = 0; j <= maxY; j++){ 
      matrix[i][j] = (valueArray[count]); 
      count++; 
     } 
    } 
}*/ 

这是一个有趣的递归。我假设你已经为你的项目提交了你的代码,所以我对它进行了刺探。继承人什么我结束了:(我留在printlns所以你可以看到程序流程)

// called by System.out.println(getSpiral(matrix,0,0,0,0)); 

public static String getSpiral(String[][] array, int x, int y, double direction, int turnCount) { 

    int [] velocity = getDirection(direction); 
    if(x+velocity[0] >= array.length || y+velocity[1] >= array[x].length || 
      x+velocity[0] < 0 || y+velocity[1] < 0 || 
      array[x+velocity[0]][y+velocity[1]].equals("done")) { 
     System.out.println("turn"); 
     if(turnCount>=3) 
      return array[x][y]; 
     return getSpiral(array, x, y, direction+Math.PI/2,turnCount+1); 
    } 
    String value = array[x][y].toLowerCase(); 
    array[x][y]="done"; 
    System.out.println(value); 
    return value + " " + getSpiral(array, x+velocity[0], y+velocity[1], direction,0); 
} 

public static int[] getDirection(double angle) { 
    return new int[] {(int)Math.round(Math.cos(angle)), (int)Math.round(Math.sin(angle))}; 
} 
+0

感谢您考虑看看。看起来好像我在想着初始化。我编辑了我的帖子以反映当前问题。我当然会花更多的时间进行调试,只要我能解决问题并进行更新,但我仍然希望有人会看到什么是一个大问题。它仍然无法在3乘4阵列上打印最后一个数字'5',并且当我提交给codeeval自动分级器时,它会得到10%:-(。 – spacecadet

+1

@spacecadet如果你有一个3x3的方块并且完成了整个你的minX从0开始,maxX从2开始。快进1个边框循环,你的miX = 1,maxX = 1,x = 1 ...你的while循环会再次运行吗?不,不会,这只是正方形的情况下,只有当它到达中心,因为它已经缩小到1x1的正方形。修正?将这些代码拼凑在一起,如果它的方矩阵是中心数字,那么最好是运气好,上帝的速度。 –

+0

这肯定有点帮助!那里有一个问题,起源于最大等于最小值时。基本上它在阵列平衡时触发,但似乎有奇数个元素。我的解决方法是保留一个附加元素的计数器,并且如果它小于最后的总数,则附加在中值X + 1中值Y + 1上。这很好地解决了我的测试案例。虽然它在自动平地机上只做了一点点改进,从10%上升到了30%。叹息@ _ @ – spacecadet

0

顺时针遍历

public static String matrixTraverse(int[][] matrix, int startX, int startY){ 
    String result = ""; 
    boolean baseCase = startX + 1 == Math.ceil(matrix[0].length/2.0) 
      || startY + 1 == Math.ceil(matrix.length/2.0); 

    for (int i = startX ; i < matrix[0].length - startX ; i++) { 
     result += " " + matrix[startY][i]; 
    } 

    for (int i = startY + 1 ; i < matrix.length - startY - 1 ; i++){ 
     result += " " + matrix[i][matrix[0].length - 1 - startX]; 
    } 

    for (int i = startX ; (matrix.length - 1 != 2 * startY) && (i < matrix[0].length - startX) ; i++){ 
     result += " " + matrix[matrix.length - 1 - startY][matrix[0].length - 1 - i]; 
    } 

    for (int i = startY ; (matrix[0].length - 1 != 2 * startX) && (i < matrix.length - startY - 2) ; i++){ 
     result += " " + matrix[matrix.length - 2 - i][startX]; 
    } 

    if (!baseCase) { 
     result += matrixTraverse(matrix, ++startX, ++startY); 
    } 

    return result; 

}