Private static arraylistltstringgt board new


public class WordSearch {

private static ArrayList board = new ArrayList();
private static Scanner scanner;

public static void readBoard() {
String line;
while (true) {
line = scanner.nextLine();
if (line.equals(""))

break;
board.add(line.replaceAll(" ","").toUpperCase());
}
}

public static void printBoard() {
int rows = getRows();
for (int row=0; rowSystem.out.println(board.get(row));
}

public static int getRows() {
return board.size();
}

public static int getCols() {
return board.get(0).length();
}

public static void createScanner() {
try {
scanner = new Scanner(new java.io.File("WordSearchInput.txt"));
} catch (Exception e) {
System.out.println();
System.exit(1);
}
}

public static void processWords() {
while (scanner.hasNext()) {
String word = scanner.next();
findWord(word);
}
}

public static void findWord(String word) {
int rows = getRows();
int cols = getCols();
for (int row=0; rowfor (int col=0; colfindWord(word,row,col);
}

public static void findWord(String word, int row, int col) {
for (int drow=-1; drow<=1; drow++)
for (int dcol=-1; dcol<=1; dcol++)
findWord(word,row,col,drow,dcol);
}

public static void findWord(String word, int row, int col, int drow, int dcol) {
int rows = getRows();
int cols = getCols();
for (int offset=0; offsetint targetRow = row + offset*drow;
int targetCol = col + offset*dcol;
if ((targetRow < 0) ||
(targetRow >= rows) ||
(targetCol < 0) ||
(targetCol >= cols))

return;
char boardChar = board.get(targetRow).charAt(targetCol);
char wordChar = word.charAt(offset);
if (boardChar != wordChar)
// mismatch, so we're done
return;
}
System.out.printf("%s at %d,%d direction %d,%dn",
word, row, col, drow, dcol);
}

public static void main(String[] args) {
createScanner();
readBoard();

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: Private static arraylistltstringgt board new
Reference No:- TGS01254816

Now Priced at $20 (50% Discount)

Recommended (94%)

Rated (4.6/5)