我正在尝试调试我的课程。这门课正在通过一块拼图板并找到每条可用的路径。路径包括水平、垂直和对角线。你不能重复一个步骤。我目前有很多打印语句试图找出我哪里出错了,但我似乎找不到它。我把这些留在了。我认为这与我的 currentPath[][] 有关。任何指针提示将不胜感激。
public class FindWords {
String [][] board;
final int LAST_LETTER = 2;
final int BEEN_THERE = 1;
final int AVAILABLE = 0;
BoggleDictionary Dictionary;
private StackADT<SearchWords> searchStack = new ArrayStack<SearchWords>();
public StackADT<SearchWords> foundStack = new ArrayStack<SearchWords>();
public FindWords (String [][] board) throws Exception {
this.board = board;
this.Dictionary = new BoggleDictionary();
//this.foundStack = null;
}
public void startSearch(){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[0].length; j++){
System.out.println(board[i][j]);
String firstLetter = "";
int [][] pathBoard = makeBlankBoard();
pathBoard[i][j] = LAST_LETTER;
System.out.println(">>" + Arrays.deepToString(pathBoard));
firstLetter = board[i][j];
System.out.println("first letters : " + firstLetter + " <====");
SearchWords thisPath = new SearchWords(pathBoard, firstLetter);
searchStack.push(thisPath);
}
}
Search();
}
private boolean Search(){
while (!searchStack.isEmpty())
{
SearchWords searchObj = searchStack.pop();
int [][] currentPath = searchObj.getPath();
int [][] array = new int [currentPath.length][currentPath[0].length];
for (int i = 0; i <currentPath.length; i++){
for (int j = 0; j <currentPath[0].length; j++){
array[i][j] = currentPath[i][j];
}
}
String makingString = searchObj.getString();
if (makingString.length() > 2){
if (Dictionary.contains(makingString)){
foundStack.push(searchObj);
}
}
for (int i = 0; i < array.length; i ++){
for (int j = 0; j < array[0].length; j++){
if (array[i][j] == LAST_LETTER){ //finding the last position in the string
int x = i;
int y = j;
//array[i][j] = BEEN_THERE;
pushPosition(searchObj, x+1, y+1, i, j); //lower left then going counter-clockwise
pushPosition(searchObj, x, y+1, i, j);
pushPosition(searchObj, x-1, y+1, i, j);
pushPosition(searchObj, x-1, y, i, j);
pushPosition(searchObj, x-1, y-1, i, j);
pushPosition(searchObj, x, y-1, i, j);
pushPosition(searchObj, x+1, y-1, i, j);
pushPosition(searchObj, x+1, y, i, j);
}
}
}
}
return true;
}
private void pushPosition (SearchWords obj, int x, int y, int i, int j){
int [][] currentPath = obj.getPath();
String makingString = obj.getString();
System.out.println("In push method: " + Arrays.deepToString(currentPath) +x+y+i+j);
if (validPosition(x, y, currentPath)){
currentPath[x][y] = LAST_LETTER;
currentPath[i][j] = BEEN_THERE;
System.out.println("after valid ch: "+ Arrays.deepToString(currentPath));
makingString = makingString + board[x][y];
SearchWords newPath = new SearchWords(currentPath, makingString);
System.out.println("is string getting longer: " + makingString);
System.out.println("Stack size: " + searchStack.size());
System.out.println("pushing back on stack"+ Arrays.deepToString(currentPath));
searchStack.push(newPath);
}
}
private boolean validPosition (int x, int y, int [][] path){
boolean result = false;
if (x >= 0 && x < board.length && y >= 0 && y < board[x].length){
if (path[x][y] == AVAILABLE){
System.out.println("Checked position : " + x + y);
result = true;
}
}
return result;
}
private int [][] makeBlankBoard(){
int row = board.length;
int col = board[0].length;
int [][] blankBoard = new int [row][col];
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[0].length; j++){
blankBoard[i][j] = AVAILABLE;
}
}
return blankBoard;
}
}
更新了有效的课程。在调用 push 方法之前需要制作一个新的 pathBoard 副本。谢谢杀屏。
private void Search(){
while (!searchStack.isEmpty())
{
System.out.println("stack size in search: " + searchStack.size());
SearchWords searchObj = searchStack.pop();
int lastLetterRow = searchObj.getRow();
int lastLetterCol = searchObj.getCol();
String stringSoFar = searchObj.getString();
int [][] pathBoard = searchObj.getPath();
System.out.println ("row then Col: " + lastLetterRow + lastLetterCol);
System.out.println ("string so far: " + stringSoFar);
System.out.println("Path board so far in search: " + Arrays.deepToString(pathBoard)+ "\n");
if (stringSoFar.length() > 2){
if (Dictionary.contains(stringSoFar)){
foundStack.push(searchObj);
System.out.println("Found word!! " + stringSoFar);
}
}
System.out.println("made it past if dict");
//lower left then going counter-clockwise
pushPosition (pathBoard, stringSoFar, lastLetterRow+1, lastLetterCol+1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition (pathBoard, stringSoFar, lastLetterRow, lastLetterCol+1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow, lastLetterCol+1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow-1, lastLetterCol+1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow-1, lastLetterCol, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow-1, lastLetterCol-1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow, lastLetterCol-1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow+1, lastLetterCol-1, lastLetterRow, lastLetterCol);
pathBoard = makeCopy(pathBoard);
pushPosition(pathBoard, stringSoFar, lastLetterRow+1, lastLetterCol, lastLetterRow, lastLetterCol);
}
System.out.println("FOUND WORDS:");
while(!foundStack.isEmpty()){
SearchWords foundWords = foundStack.pop();
System.out.println(foundWords.getString());
}
}