私の友人は私に次の問題を与えました:
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);
}
}
バックトラックアルゴリズムを使用しましたが、単語の最後の文字が表示されたときにのみバックトラックし、単語の右端にある文字が表示されたときに再びバックトラックします。
文字の頻度をカウントするためにカウンターのマトリックスを使用します。
その問題は動的計画法アルゴリズムで解決できますか?
または、より良いアイデアはありますか?