如何在字符矩阵中查找字符串路径?

时间:2013-07-17 20:58:50

标签: matrix language-agnostic

如何实现一个函数来检查字符矩阵中是否有字符串的路径?它以矩阵形式向左,向右,向上和向下移动,以及用于移动的单元格。

路径可以从矩阵中的任何条目开始。如果一个单元格被路径上的字符串字符占用,则它不能再被另一个字符占用。

你将如何解决这个问题(如果你愿意,可以使用Psuedocode)?我最初的想法是将其解释为图形问题,矩阵位置作为图形中的顶点。

2 个答案:

答案 0 :(得分:1)

编辑:添加一个缓慢的半java半伪代码版本

class Pair {
    public Pair(int x, int y) { this.x = x; this.y = y; }
    public int x;
    public int y;
};

class Main {
    static Vector<Pair> singleton(Pair p) {
        Vector<Pair> v = new Vector<Pair>();
        v.insert(p);
        return v;
    }

    static void f(String str, int [][]matrix, int width, int height) {
        Vector<Vector<Pair>> v = new Vector<Vector<Pair>>();
        for (int i = 0; i < height; ++i)
            for (int j = 0; j < width; ++j)
                try(i, j, str, matrix, width, height, v);
    }

    static void try(int i, int j, String str, int [][]matrix, int width, int height, Vector<Vector<Pair>> v) {
        int old_v_size = v.length;
        if (i < 0 || i >= height || j < 0 || j >= width) return;
        if (str.length == 1) v.insert(singleton(new Pair(i,j));
        try(i+1,j,str.substr(1),matrix,width,height,v);
        try(i-1,j,str.substr(1),matrix,width,height,v);
        try(i,j+1,str.substr(1),matrix,width,height,v);
        try(i,j-1,str.substr(1),matrix,width,height,v);
        for (int k = old_v_size; k < v.length; ++k) v[k].insert(new Pair(i,j));
    }

}

旧代码如下:

这是Haskell中的一个解决方案(我在这里用一个获取两个整数并在矩阵中返回值的函数替换矩阵)。函数f接受字符串,矩阵函数,宽度,高度,并返回每个可能解决方案的元组列表列表。

f str matrix width height =
    concat [try i j str matrix width height | i <- [0..width-1], j <- [0..height-1] ]  


try i j (c:cs) matrix width height | i < 0 || 
                                     i >= height || 
                                     j < 0 || 
                                     j >= width || 
                                     matrix i j /= c = []
try i j [c] matrix width height = [(i,j)]
try i j (c:cs) matrix width height =
    concat  [map ((i,j):) $ try (i+1) j cs matrix width height,
             map ((i,j):) $ try (i-1) j cs matrix width height,
             map (c:) $ try i (j+1) cs matrix width height,
             map (c:) $ try i (j-1) cs matrix width height]

如果您想要另一种语言,请指定,我将发送另一种解决方案

答案 1 :(得分:0)

public class MatrixSearch {

    public static void main(String[] args) {

        String[] find = {"M", "I", "C", "R", "O", "S", "O", "F", "T"};

        String[][] matrix = {
            {"A", "C", "P", "R", "C"},
            {"M", "S", "O", "P", "C"},
            {"I", "O", "V", "N", "I"},
            {"C", "G", "F", "M", "N"},
            {"Q", "A", "T", "I", "T"}
        };
        // printing matrix...
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println("");
        }
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j].equals(find[0])) {
                    System.out.println(matrix[i][j]);
                    boolean found = recursiveMatrixFind(matrix, i, j, matrix.length, matrix.length, find, 1);                    
                    System.out.println(found);
                }
            }
        }
    }

    public static boolean recursiveMatrixFind(String[][] matrix, int row, int column, int rowLength, int columnLength, String[] find, int k) {

        if (k == find.length) {
            return true;
        }

        for (int neighbourRow = row - 1; neighbourRow <= row + 1; neighbourRow++) {
            if (neighbourRow >= 0 && neighbourRow < rowLength) {
                for (int neighbourColumn = column - 1; neighbourColumn <= column + 1; neighbourColumn++) {
                    if (neighbourColumn >= 0 && neighbourColumn < columnLength) {
                        if ((!(neighbourRow == row && neighbourColumn == column))) {
                            if (matrix[neighbourRow][neighbourColumn].equals(find[k])) {
                                System.out.println(matrix[neighbourRow][neighbourColumn]);
                                boolean found = recursiveMatrixFind(matrix, neighbourRow, neighbourColumn, rowLength, columnLength, find, ++k);
                                if (found) {
                                    return true;
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
}