我的朋友給我以下的問題:動態編程或回溯?
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);
}
}
我用了一個回溯算法,但只有我當我看到最後一個單詞的時候回到原點,當看到最右側的那個單詞時,再次回到原點。
我使用計數矩陣計數字母的頻率。
動態規劃算法能解決這個問題嗎?
或者有什麼更好的想法?
非常感謝!我想通過動態規劃解決問題的三維矩陣,但我無法實現。 – 2013-05-09 16:44:24
@TrungHuynh我已經糾正了我的回答,遺憾地證明我錯了。請看看它。 – 2013-05-09 22:20:34
我認爲回溯的複雜度是O(mn),就像動態規劃一樣。實際上,我可以通過一些標誌變量爲我訪問的標記元素加快速度。我會仔細看看你的解決方案。 – 2013-05-10 01:33:11