1

我试图找出给定的第二个数组将位于第一个数组中的位置,但似乎无法将其转换为字符串以找出它们的位置:

public static void main(String[] args) {
    char i [][] = new char[][]{
            {'d', 's', 'l', 'e', 'i', 'g', 'h', 'e', 'i', 'j', 'a', 's', 'l', 'd', 'k', 'j'},
            {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'W', 'w', 'Z', 'Z', 'Z', 'W', '1', 'l', 'k'},
            {'h', 'i', 'j', 'k', 'l', 'm', 'n', 'Z', 'A', 'a', 'Z', 'a', 'Z', '2', 'i', 'n'},
            {'o', 'p', 'q', 'r', 's', 't', 'u', 'Z', 'Z', 'L', 'l', 'Z', 'Z', '3', 'i', 'v'},
            {'v', 'w', 'x', 'y', 'z', '1', '2', 'Z', 'd', 'Z', 'D', 'd', 'Z', '4', 'q', 'i'},
            {'3', '4', '5', '6', '7', '8', '9', 'o', 'Z', 'Z', 'o', 'O', 'Z', '5', 'b', 'v'},
            {'k', 'e', '8', '7', '8', '4', 'j', 'f', 'l', 'k', 'a', 'v', '8', '8', 'h', 'j'}
    };

    char w [][] = new char[][]{
            {'W', 'w', '.', '.', '.', 'W', '1'},
            {'.', 'A', 'a', '.', 'a', '.', '2'},
            {'.', '.', 'L', 'l', '.', '.', '3'},
            {'.', 'd', '.', 'D', 'd', '.', '4'},
            {'o', '.', '.', 'o', 'O', '.', '5'}
    };

    find(i, w);
}

    public static int[] find(char [][] image, char [][] waldo) {
    for (int i = 0; i < waldo.length; i++) {
        char[] largerCharArray= large[i];
        String largerString = new String(largerCharArray);

        //used for debug purposes
        char[] array = largerCharArray;

        char [] smallCharArray = small[i];
        String smallString = new String(smallCharArray);

        char[] array1 = smallCharArray;

        //beginning comparison
        if (largerString.indexOf(smallString) >= 0) {

           int temp = largerString.indexOf(smallString);
        }

        //debug purposes
        System.out.println(largerString.indexOf(smallString));
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array1));
    }
    //for debug purposes
    return null;
}
4

2 回答 2

0

试试我刚刚完成的这段代码,它可能有一些错误,它会尝试改进它。它不使用字符串。

private static int[] find(char[][] image, char[][] waldo) {
    int row = -1;
    int column = -1;
    char first_waldo = waldo[0][0]; // first char of waldo
    boolean continue1 = true; // this is used to avoid looping when the indexes tried are not a possible answer
    int size = waldo.length * waldo[0].length; // number of elements of waldo
    int cont = 0;

    for (int i = 0; i < image.length; i++) {
        for (int j = 0; j < image[i].length; j++) { // Looping over chars in image array
            char current = image[i][j]; // Current char of image array
            if (current == first_waldo) { // If true, this indexes (i, j) are a possible answer
                row = i; // Current image row
                column = j; // Current image column
                cont = 0; // Count of how many chars are matched
                for (int k = row; k < row + waldo.length; k++) {
                    for (int l = column; l < column + waldo[0].length; l++) { // Looping over
                                                       // the possible matching array in image
//                          System.out.println("Comparing " + image[k][l] + " with " + waldo[k - row][l - column]);
//                          System.out.println(k + " " + l);
                        if (waldo[k - row][l - column] == '.') { // If its a point, count as matching characters
                            cont++;
                            continue; 
                        }
                        if (image[k][l] != waldo[k - row][l - column]) { // If chars are different, it's not the answer
                            row = -1;
                            column = -1;
                            continue1 = false; // So it doesn't continue looping
                            break;
                        } else {
                            cont++; // If they are equals, count as matching characters
                        }
                    }
                    if (continue1 == false) { // To avoid overlooping when it's not the answer
                        continue1 = true; // Reset value
                        break;
                    }
                }
                if (cont == size) { // Is the number of matched charactes equal to the size of waldo?
                    int[] res = {row, column};
                    return res; // Return indexes
                }

            } else {
                row = -1;
                column = -1;
            }

        }
    }
    int[] res = {-1, -1};
    //      System.out.println("\n" + row +" , " +  column);
    return res; // Not answer

}
于 2013-10-27T00:35:52.153 回答
0

据我了解,任务是找到匹配的子矩阵。如果用通配符替换点,这是有道理的。所以较小的矩阵位于从 1 开始计数的位置(行 = 2,列 = 8)。

这是搜索子矩阵的代码。我使用正则表达式将行匹配为字符串:

public static int[] find(char [][] large, char [][] small) {
    outer: for (int i = 0; i < large.length - small.length; i++) {

        int column = -1;

        for (int j = 0; j < small.length ; j++) {
            String largerString = new String(large[i + j]);

            String smallString = new String(small[j]);

            Matcher matcher = Pattern.compile(smallString).matcher(largerString);

            if(!matcher.find()){
                continue outer;
            }

            int thisColumn = matcher.start();

            if(column != -1 && column != thisColumn){
                continue outer;
            }

            column = thisColumn;
        }

        System.out.printf("found it: %d, %d!", i, column);
    }

    //for debug purposes
    return null;
}
于 2013-10-27T00:22:20.870 回答