[英]Boggle solver implementation
我正在努力實現一個解決方案,以便在隨機的5x5字母板中查找所有單詞。 目前它正在返回幾個字,但不是幾乎完整列表。 我很確定我的問題存在於帶有for循環的findWords方法中,但我無法弄清楚是什么讓if語句繼續遍歷所有8個方向。
import java.io.File;
import java.util.*;
public class RandomWordGame {
private static char[][] board = new char[5][5];
private static Random r = new Random();
private static ArrayList<String> dictionary = new ArrayList<String>();
private static char[][] createBoard()
{
for (int i=0; i<board.length; i++)
{
for (int j=0; j<board.length; j++)
{
board[i][j] = (char) (r.nextInt(26) + 'a');
System.out.print(board[i][j]);
}
System.out.println("");
}
System.out.println();
return board;
}
public static ArrayList<String> solver(char[][] board)
{
if(board == null)
System.out.println("Board cannot be empty");
ArrayList<String> words = new ArrayList<String>();
for(int i=0; i<board.length; i++)
{
for(int j=0; j<board[0].length; j++)
{
findWords(i, j, board[i][j] + "");
}
}
return words;
}
public static void findWords(int i, int j, String currWord)
{
try
{
Scanner inputStream = new Scanner(new File("./dictionary.txt"));
while(inputStream.hasNext())
{
dictionary.add(inputStream.nextLine());
}
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
for(i=0; i>=0 && i<board.length; i++)
{
for(j=0; j>=0; j++)
{
currWord += board[i][j];
if(currWord.length()>5)
return;
if(dictionary.contains(currWord))
System.out.println(currWord);
}
}
}
public static void main(String[] args)
{
board = createBoard();
ArrayList<String> validWords = RandomWordGame.solver(board);
for(String word : validWords)
System.out.println(word);
}
}
這段代碼有一些有趣的東西。 首先,你總是從求解器方法中返回一個空的ArrayList,但這並不是什么因為你從findWords打印每個結果而殺了你。
問題是findWords方法只是從拼圖的左上角開始不斷添加字母。
//i=0 and j=0 means it will always start at the top left tile
for(i=0; i>=0 && i<board.length; i++)
{
for(j=0; j>=0; j++)
{
//currWord is never reset, so it just keeps getting longer
currWord += board[i][j];
if(currWord.length()>5)
return;
if(dictionary.contains(currWord))
System.out.println(currWord);
}
}
現在,您只能找到以您選擇的拼貼開頭的單詞,其余單詞的選擇順序與從左上角開始添加到拼圖中的順序相同。
我建議花一些時間用鉛筆和紙,並深入了解二維數組索引與它們在網格中的位置之間的關系。
使用DFS方法實現Java
import java.util.Arrays;
public class WordBoggle {
static int[] dirx = { -1, 0, 0, 1 };
static int[] diry = { 0, -1, 1, 0 };
public static void main(String[] args) {
char[][] board = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };
String word = "ABFSADEESCCEA";
System.out.println(exist(board, word));
}
static boolean exist(char[][] board, String word) {
if (board == null || board.length == 0 || word == null || word.isEmpty())
return false;
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
resetVisited(visited);
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(i)) {
return DFS(board, word, i, j, 1, visited);
}
}
}
return false;
}
static void resetVisited(boolean[][] visited) {
for (int l = 0; l < visited.length; l++) {
Arrays.fill(visited[l], false);
}
}
static boolean DFS(char[][] board, String word, int i, int j, int k, boolean[][] visited) {
visited[i][j] = true;
if (k >= word.length())
return true;
for (int z = 0; z < 4; z++) {
if (isValid(board, i + dirx[z], j + diry[z], visited)) {
if (word.charAt(k) == board[i + dirx[z]][j + diry[z]]) {
return DFS(board, word, i + dirx[z], j + diry[z], k + 1, visited);
}
}
}
return false;
}
static boolean isValid(char[][] board, int i, int j, boolean[][] visited) {
return (i >= 0 && i < board.length && j >= 0 && j < board[0].length && !visited[i][j]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.