四分区矩阵搜索

时间:2016-03-25 17:41:38

标签: java search matrix netbeans

我所拥有的程序应该将矩阵连续分成四个象限,直到找到给定的值。使用当前程序,它将永远运行。如何让它发布我搜索的号码?

 public static void DivideAndConquerSearch(int target, int fromRow, int toRow, int fromCol, int toCol, int[][] matrix, int comparison_counter) {
    boolean found = false;

    while (!found) {

        int i = fromRow + (toRow - fromRow) / 2;
        int j = fromCol + (toCol - fromCol) / 2;

        if (matrix[i][j] == target) {
            found = true;
        } else if (matrix[i][j] < target) {
            if (i + 1 <= toRow) {
                found = false;
                DivideAndConquerSearch(target, i + 1, toRow, fromCol, toCol, matrix, comparison_counter);
            } else if (j - 1 >= fromCol) {
                found = false;
                DivideAndConquerSearch(target, fromRow, toRow, fromCol, j - 1, matrix, comparison_counter);
            }
        }
        comparison_counter++;
    }
    if (found == true) {
        System.out.println(target + " found in " + comparison_counter + " comparisons using recursive search");
    } else {
        System.out.println(target + " NOT found in " + comparison_counter + " comparisons using recursive search");
    }

}

1 个答案:

答案 0 :(得分:0)

我写了一些东西,只是使用递归方法搜索整个数组(暴力种类)。它将2D阵列分成4个象限并搜索NW,NE,SW,SE。我使用OR条件来检查是否在任何象限中找到它。因此,如果在NW中找到数字,则不会搜索其余的数据(是的!)。如果您有任何疑问,请告诉我。代码不是最漂亮或最优化的,我有点选择离开它,这样你就可以理解我做得更好了。全班,包括(非常基础)测试...

public class Divide_Conquer_2dArr {

    private static boolean divideAndConquer(int[][] arr, int target) {

        /* IDEA */
        // NORTH_WEST [Kanye West joke(?)]
        // need to search row-wise from [0,row-1], col-wise from [0, col-1]

        // NORTH_EAST
        // need to search row-wise from [0,row-1], col-wise from [col, maxCol]

        // SOUTH_WEST
        // need to search row-wise from [row, maxRow], col-wise from [0, col-1]

        // SOUTH_EAST
        // need to search row-wise from [row, maxRow], col-wise from [col, maxCol]

        return divideAndConquerHelper(arr, target, 0, arr[0].length-1, 0, arr.length-1);
    }

    private static boolean divideAndConquerHelper(int[][] arr, int target, int minCol, 
            int maxCol, int minRow, int maxRow) {
        // print relevant stuff
        printArr(arr,  minCol, maxCol, minRow, maxRow);

        if(minCol == maxCol || minRow == maxRow) {

            if(minCol == maxCol) {
                for(int i = minRow ; i <= maxRow ; i++)
                    if(arr[i][minCol] == target) {
                        System.out.println("Found!");
                        return true;
                    }
            }

            else if(minRow == maxRow) {
                for(int i = minCol ; i <= maxCol ; i++)
                    if(arr[minRow][i] == target) {
                        System.out.println("Found!");
                        return true;
                    }
            }

            return false;
        }

        int midRow = (maxRow + minRow)/2;
        int midCol = (maxCol + minCol)/2;


        return 
                // north-west quadrant
                divideAndConquerHelper(arr, target, minCol, midCol, minRow, midRow)
                ||
                // north-east quadrant
                divideAndConquerHelper(arr, target, midCol+1, maxCol, minRow, midRow)
                ||
                // south-west quadrant
                divideAndConquerHelper(arr, target, minCol, midCol, midRow+1, maxRow)
                ||
                // south-east quadrant
                divideAndConquerHelper(arr, target, midCol+1, maxCol, midRow+1, maxRow);
    }

    // prints arr[minRow..maxRow][minCol..maxCol] inclusive
    private static void printArr(int[][] arr, int minCol, 
            int maxCol, int minRow, int maxRow) {
        for(int i = minRow ; i <= maxRow ; i++ ) {
            for(int j = minCol ; j <= maxCol ; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
        System.out.println("==================================");
    }

    public static void main(String[] args) {
        int[][] arr = new int[][]
                {
                    {1,2,3,4},
                    {6,7,8,9},
                    {11,12,13,14},
                    {16,17,18,19},
                    {21,22,23,24}
                };

        boolean retVal = divideAndConquer(arr, 12);
        if(!retVal)
            System.out.println("Not found :(");
    }
}