2009-08-11 94 views
2

這不是一項家庭作業。我是編程初學者,這也是我的第一篇文章 - 請耐心等待。查找矩陣中相鄰數字的最大面積

我無法找到這裏發佈的類似問題。

在初學者的書,我發現了以下問題:

# Find the biggest area of adjacent numbers in this matrix: 
1 3 2 2 2 4 
3 3 3 2 4 4 
4 3 1 2 3 3 #--> 13 times '3' 
4 3 1 3 3 1 
4 3 3 3 1 1 

這裏是我到目前爲止的代碼,使用DFS實現從http://www.algolist.net/Algorithms/Graph_algorithms/Undirected/Depth-first_search。有「幻數」無處不在,這些方法是「公共靜態」等等 - 我正打算工作的算法後,以修復這些東西......

public class AdjacentAreaInMatrix { 
    /* 
    * Enums for the state of the Nodes, for use in DFS/BFS 
    */ 
    private enum NodeState { 
     Visited, InProgress, Unvisited 
    }; 

    /* 
    * These 2 'magic' numbers come from the hardcoded 'matrix' below, 
    * cause it has 5 rows and 6 columns 
    */ 
    public static final int ROWSCOUNT = 5; 
    public static final int COLUMNSCOUNT = 6; 

    /* 
    * Two variables for counting the maximum sequence 
    * of numbers (as required by the problem definition) 
    */ 
    private static int tempElementsCount = 0; 
    private static int maxElementsCount = 1; // except if the matrix is empty, then it should be 0 

    /* 
    * The hardcoded matrix 
    */ 
    private static final int[][] matrix = new int[][] { 
      { 1, 3, 2, 2, 2, 4 }, 
      { 3, 3, 3, 2, 4, 4 }, 
      { 4, 3, 1, 2, 3, 3 }, 
      { 4, 3, 1, 3, 3, 1 }, 
      { 4, 3, 3, 3, 1, 1 } }; 

    /* 
    * Create an auxiliary matrix 'state' to implement DFS. 
    * Initialize the whole matrix as 'unvisited' and 
    * start DFS at the first element of the matrix 
    */ 
    public static void DFS() { 
     NodeState state[][] = new NodeState[ROWSCOUNT][COLUMNSCOUNT]; 
     // clear the state of the matrix 
     for (int i = 0; i LT ROWSCOUNT; i++) { 
      for (int j = 0; j LT COLUMNSCOUNT; j++) { 
       state[i][j] = NodeState.Unvisited; 
      } 
     } 
     runDFS(0, 0, state);  
    } 

    /* 
    * Using the auxiliary matrix "state[][]", use DFS to traverse the 
    * 'real' matrix[][] 
    */ 
    public static void runDFS(int i, int j, NodeState state[][]) { 
     state[i][j] = NodeState.InProgress; 
     // traverse the whole matrix state[][] and recursively run runDFS() from the needed elements. 
     for (int rows = 0; rows LT ROWSCOUNT; rows++) { 
      for (int columns = 0; columns LT COLUMNSCOUNT; columns++) { 
       /* 
       * ---------------------------------------------------------------------- 
       * For the logic in the 'if' statement regarding the adjacent elements: 
       * i0j0 i1j0 i1j0 
       * i0j1 i1j1 i2j1 
       * i0j2 i1j2 i2j2 
       * It uses the thing, that the sum of (i+j) for the coordinates of 
       * the elements above, below, on the left and on the right of i1j1 
       * are exactly +1/-1 of the sum of the coordinates of i1j1 
       * -> i1j2 to 1+2 = 3 
       * -> i2j1 to 1+2 = 3 
       * -> i1j1 to 1+1 = 2 (the current element) -> matrix[i][j] 
       * -> i1j0 to 1+0 = 1 
       * -> i0j1 to 1+0 = 1 
       * ---------------------------------------------------------------------- 
       */ 
       if ((matrix[i][j] == matrix[rows][columns]) // if the values are equal 
         && ((((i+j) - (rows + columns)) == 1) || (((i+j) - (rows + columns)) == -1))// and if the element is adjacent 
         && (state[rows][columns] == NodeState.Unvisited)) { // and if the element is still not visited 
        tempElementsCount++; 
        if (tempElementsCount > maxElementsCount) { 
         maxElementsCount = tempElementsCount; 
        } 
        runDFS(rows, columns, state); // recursively run DFS for each element, that "isEdge" 
       } else { 
        // if the elements aren't [adjacent, equal and not visited], start the count again from '0' 
        tempElementsCount = 0; 
       } 
      } 
     } 
     state[i][j] = NodeState.Visited; 
    } 

    public static void go() { 
     AdjacentAreaInMatrix.DFS(); 
     System.out.println(maxElementsCount); 
    } 
} 

調試了好幾天之後,每調試會話代碼變得更加複雜...任何幫助將不勝感激。提前致謝。

+0

嗯......你的問題到底是什麼?你是否想要關於算法的建議,幫助解決特定的錯誤,或者對代碼設計的評論? – 2009-08-11 15:01:55

+0

關於算法的建議很好,謝謝。我非常清楚,代碼設計不好(我甚至沒有試圖在算法運行前做好它)。但是,上面顯示的代碼總是給我'1',不管從哪個矩陣開始 - 我喜歡在這裏問這裏,而不是再次調試它。 – 2009-08-11 15:04:18

+0

另外,在我發現的所有書籍/文章中,DFS/BFS的矩陣由1/0表示有/無邊緣。我不確定我是否正在使用此矩陣的正確數據表示(我以前沒有寫過DFS)。 – 2009-08-11 15:12:11

回答

2

我認爲問題在於您每次都重置tempElementsCount。試想一下你的代碼如何在給定的矩陣上工作,你會發現你在runDFS()方法中總是用元素(0,0)開始搜索,if元素將是false,所以你重置之前tempElementsCount您可以繼續使用其他(也可能是相鄰的)元素進行搜索。希望我已經清楚了......

+0

謝謝,你是對的。從(1,1)開始而不重置tempElemetsCount得到12,這足夠清晰以便使算法正常工作。非常感謝! – 2009-08-11 15:22:11

+0

對不起,但我不能投這個答案,因爲我沒有足夠的聲譽做這件事。再次感謝您的幫助。 – 2009-08-11 15:34:35