我的朋友给了我以下问题:
Input: A matrix of letters and a word.
Output: The frequency of the word in the matrix assuming
you can move left, right, up and down in the matrix to form the word.
例如:
Input:
S E X Y
A S E A
A A X A
A A Y A
And word is SEXY.
Output:
4 (four times in matrix of letters)
这是我解决问题的代码:
package backtracking;
public class CountFrequency {
private char[][] matrixOfLetter;
private String word;
private int n, m;
private int lengthOfWord;
private int[][] matrixCountFrequency;
public CountFrequency(int n, int m, String word) {
matrixOfLetter = new char[n][m];
this.word = word;
this.n = n;
this.m = m;
this.lengthOfWord = word.length();
matrixCountFrequency = new int[n][m];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
matrixCountFrequency[i][j] = 0;
}
public static void main(String[] args) {
CountFrequency countFrequency = new CountFrequency(4, 4, "SEXY");
countFrequency.addMatrixOfLetter(0, 0, 'S');
countFrequency.addMatrixOfLetter(0, 1, 'E');
countFrequency.addMatrixOfLetter(0, 2, 'X');
countFrequency.addMatrixOfLetter(0, 3, 'Y');
countFrequency.addMatrixOfLetter(1, 0, 'A');
countFrequency.addMatrixOfLetter(1, 1, 'S');
countFrequency.addMatrixOfLetter(1, 2, 'E');
countFrequency.addMatrixOfLetter(1, 3, 'A');
countFrequency.addMatrixOfLetter(2, 0, 'A');
countFrequency.addMatrixOfLetter(2, 1, 'A');
countFrequency.addMatrixOfLetter(2, 2, 'X');
countFrequency.addMatrixOfLetter(2, 3, 'A');
countFrequency.addMatrixOfLetter(3, 0, 'A');
countFrequency.addMatrixOfLetter(3, 1, 'A');
countFrequency.addMatrixOfLetter(3, 2, 'Y');
countFrequency.addMatrixOfLetter(3, 3, 'A');
countFrequency.process();
countFrequency.printResult();
}
public void addMatrixOfLetter(int i, int j, char c) {
matrixOfLetter[i][j] = c;
}
public void process() {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (word.indexOf(matrixOfLetter[i][j]) == -1) {
matrixCountFrequency[i][j] = -1;
continue;
}
if (matrixOfLetter[i][j] == word.charAt(lengthOfWord - 1))
processWithLastChar(lengthOfWord - 1, i, j);
}
}
public void processWithLastChar(int indexOfWord, int row, int col) {
matrixCountFrequency[row][col] += 1;
if (indexOfWord == 0)
return;
else {
if (row - 1 >= 0) {
if (matrixOfLetter[row - 1][col] == word
.charAt(indexOfWord - 1))
processWithLastChar(indexOfWord - 1, row - 1, col);
}
if (row + 1 < lengthOfWord) {
if (matrixOfLetter[row + 1][col] == word
.charAt(indexOfWord - 1))
processWithLastChar(indexOfWord - 1, row + 1, col);
}
if (col - 1 >= 0) {
if (matrixOfLetter[row][col - 1] == word
.charAt(indexOfWord - 1))
processWithLastChar(indexOfWord - 1, row, col - 1);
}
if (col + 1 < lengthOfWord) {
if (matrixOfLetter[row][col + 1] == word
.charAt(indexOfWord - 1))
processWithLastChar(indexOfWord - 1, row, col + 1);
}
}
}
public void printResult() {
int count = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (word.charAt(0) == matrixOfLetter[i][j])
count += matrixCountFrequency[i][j];
}
System.out.println("Frequency is : " + count);
}
}
我使用了回溯算法,但我只在看到单词的最后一个字母时回溯,当看到单词中最右边的字母时再次回溯。
我使用计数器矩阵来计算字母的计数频率。
这个问题可以通过动态规划算法解决吗?
还是有更好的主意?