【问题标题】:Finding biggest area of adjacent numbers in a matrix查找矩阵中相邻数字的最大区域
【发布时间】:2009-08-11 14:57:15
【问题描述】:

这不是家庭作业。我是编程初学者,这也是我在这里的第一篇文章 - 请多多包涵。

我在这里找不到类似的问题。

在一本初学者的书中,我发现了以下问题:

# 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

这是我目前的代码,使用来自http://www.algolist.net/Algorithms/Graph_algorithms/Undirected/Depth-first_search 的 DFS 实现。到处都是“幻数”,方法是“公共静态”等 - 我打算在算法工作后修复这些东西......

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);
    }
}  

在调试了几天后,每次调试会话代码都会变得更加复杂......任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 嗯...你的问题到底是什么?您是否需要有关算法的建议、特定错误的帮助或代码设计的 cmets?
  • 关于算法的建议会很好,谢谢。我非常清楚,代码设计并不好(而且我什至没有试图在算法工作之前让它变得好)。但是,上面显示的代码一直给我“1”,无论我从哪个矩阵开始 - 我更喜欢在这里询问而不是再次调试它。
  • 另外,在我发现的所有书籍/文章中,DFS/BFS 的矩阵用 1/0 表示,表示有/没有边缘。我不确定我是否为这个矩阵使用了正确的数据表示(我之前没有写过 DFS)。

标签: java algorithm matrix depth-first-search


【解决方案1】:

我认为问题在于您每次都在重置 tempElementsCount。想象一下您的代码将如何在给定的矩阵上工作,您会看到在 runDFS() 方法中,您总是从元素 (0, 0) 开始搜索,其中 if 子句将为 false,因此您在之前重置 tempElementsCount您可以使用其他(可能是相邻的)元素继续搜索。希望我足够清楚......

【讨论】:

  • 谢谢,你是对的。从 (1,1) 开始而不重置 tempElemetsCount 得到 12,这很清楚,可以使算法正常工作。非常感谢!
  • 对不起,但我无法投票赞成这个答案,因为我没有足够的声誉这样做。再次感谢您的帮助。
猜你喜欢
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2016-07-12
  • 1970-01-01
相关资源
最近更新 更多