所以我正在尝试创建一个新对象以添加到列表中
public void findWord(char boggle[][], boolean visited[][], int i,
int j, String str)
{
visited[i][j] = true;
str = str + boggle[i][j];
if (hasWord(str)) {
for (char c : str.toCharArray()) {
Position<Character> pos = new Position<>(c, i, j);
list.add(pos);
}
}
for (int row = i - 1; row <= i + 1 && row < 4; row++)
for (int column = j - 1; column <= j + 1 && column < 4; column++)
if (row >= 0 && column >= 0 && !visited[row][column])
findWord(boggle, visited, row, column, str);
visited[i][j] = false;
}
问题在于,基本上每个位置(行和列)都将覆盖到此对象创建的最后一个实例,而元素本身不是。
我的职位类别:
public class Position<T> {
private T element;
private int row;
private int column;
Position () {
this(null,0,0);
}
Position (T element) {
this.element = element;
}
Position (T element, int row, int column) {
this.element = element;
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public T getElement() {
return element;
}
public String toString() {
return element + "(" + Integer.toString(row) + "," + column + ")";
}
就是说,一旦我打印出List输出,它就像:
a(2,2) a(2,2) r(2,2) o(2,2) n(2,2)
在Element和index中,最后一个位置都是正确的,但所有其他位置在矩阵索引中均失败。应该 :
a(0,0) a(1,0) r(1,1) o(1,2) n(2,2)
孔类:
import java.io.*;
public class Boogle {
LinkedList<Position<Character>> list = new LinkedList<>();
static QuadHashTable<String> table = new QuadHashTable<>();
String word;
static char matrix [] [] = {
{'a', '-', '-', '-'},
{'a', 'r', 'o', '-'},
{'-', '-', 'n', '-'},
{'-', '-', '-', '-'}
};
public Boogle () {
this(null);
}
public Boogle (char matrix [] []) {
this.matrix = matrix;
}
public boolean hasWord (String s) {
return s.equals(table.search(s));
}
public void findWordsUtil(char boggle[][], boolean visited[][], int i,
int j, String str)
{
visited[i][j] = true;
str = str + boggle[i][j];
if (hasWord(str))
for (char c : str.toCharArray()) {
Position<Character> pos = new Position<>(c, i, j);
list.add(pos);
}
for (int row = i - 1; row <= i + 1 && row < 4; row++)
for (int column = j - 1; column <= j + 1 && column < 4; column++)
if (row >= 0 && col >= 0 && !visited[row][column])
findWordsUtil(boggle, visited, row, column, str);
visited[i][j] = false;
}
public LinkedList<Position<Character>> solve () {
String s = "";
boolean visited[][] = new boolean[4][4];
String str = "";
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
findWordsUtil(matrix, visited, i, j, str);
return list;
}
public static void main (String args []) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/home/dsolipa/Desktop/allWords.txt"));
String line;
while((line = br.readLine()) != null) {
table.insert(line);
}
Boogle boogle = new Boogle(matrix);
System.out.println(boogle.solve());
//System.out.println(boogle.hasWord("or"));
}
}
最佳答案
似乎仅当字符串为aaron时才通过条件if (hasWord(str))
,并且for循环遍历字符串str并将所有字符添加到n的位置(即(2,2))。重新检查QuadHashTable类中的实现。
请进行以下更改:
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class Boogle {
LinkedList<Position<Character>> list = new LinkedList<>();
LinkedList<Position<Character>> table = new LinkedList<>();
Map indexMap = new HashMap();
static QuadHashTable<String> table = new QuadHashTable<>();
String word;
static char matrix [] [] = {
{'a', '-', '-', '-'},
{'a', 'r', 'o', '-'},
{'-', '-', 'n', '-'},
{'-', '-', '-', '-'}
};
public Boogle () {
this(null);
}
public Boogle (char matrix [] []) {
this.matrix = matrix;
}
public boolean hasWord(String s) {
return s.equals(table.search(s));
}
public void findWordsUtil(char boggle[][], boolean visited[][], int i,
int j, String str)
{
visited[i][j] = true;
str = str + boggle[i][j];
if (hasWord(str))
{
for (char c : str.toCharArray())
{
ArrayList coordinates = (ArrayList)indexMap.get(c);
String xy = (String)coordinates.get(0);
if(coordinates.size() !=1) // or check with flag in visited matrix
{
coordinates.remove(xy);
indexMap.put(c,coordinates);
}
String [] rowCol = xy.split(":");
int x = Integer.parseInt(rowCol[0]);
int y = Integer.parseInt(rowCol[1]);
Position<Character> pos = new Position<>(c, x, y);
list.add(pos);
}
return;
}
for (int row = i - 1; row <= i + 1 && row < 4; row++)
for (int column = j - 1; column <= j + 1 && column < 4; column++)
if (row >= 0 && column >= 0 && !visited[row][column])
findWordsUtil(boggle, visited, row, column, str);
visited[i][j] = false;
}
public LinkedList<Position<Character>> solve () {
String s = "";
boolean visited[][] = new boolean[4][4];
String str = "";
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
if(indexMap.get(matrix[i][j])!=null)
{
ArrayList list = (ArrayList) indexMap.get(matrix[i][j]);
list.add(i+":"+j);
indexMap.put(matrix[i][j],list);
}else if(matrix[i][j]!='-')
{
ArrayList list = new ArrayList();
list.add(i+":"+j);
indexMap.put(matrix[i][j],list);
}
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
findWordsUtil(matrix, visited, i, j, str);
return list;
}
public static void main (String args []) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/home/dsolipa/Desktop/allWords.txt"));
String line;
while((line = br.readLine()) != null) {
table.insert(line);
}
Boogle boogle = new Boogle(matrix);
System.out.println(boogle.solve());
//System.out.println(boogle.hasWord("or"));
}
}