word search puzzle

时间:2023-12-28 16:45:14
package WordSearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.io.*; public class WordSearch { private ArrayList<String> input = new ArrayList<String>(); // to hold input file
private char[][] grid; // to hold NxM grid
private ArrayList<String> wordList = new ArrayList<String>(); // to hold word list
private String mode; // NO_WRAP or WRAP
private HashMap<String, OutputFormat> output = new HashMap<String, OutputFormat>(); //to hold output data private class OutputFormat { //hold output information
boolean flag = false; //indicate word exist or not in the grid
int si = 0; //row index of the start
int sj = 0; //col index of the start
int ei = 0; //row index of the end
int ej = 0; //col index of the end
public boolean getFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getSi() {
return si;
}
public void setSi(int si) {
this.si = si;
}
public int getSj() {
return sj;
}
public void setSj(int sj) {
this.sj = sj;
}
public int getEi() {
return ei;
}
public void setEi(int ei) {
this.ei = ei;
}
public int getEj() {
return ej;
}
public void setEj(int ej) {
this.ej = ej;
}
} public WordSearch(String path) { //constructor
this.readInputFile(path);
this.init();
} // read input file into ArrayList
private void readInputFile(String path) {
File file = new File(path);
String line;
if (!file.exists()) {
System.out.println("file does not exist!");
System.exit(1);
} try {
BufferedReader rd = new BufferedReader(new FileReader(file));
while ((line = rd.readLine()) != null) {
this.input.add(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
} /*
* initial grid&wordList
* Example Input: 3 3; ABC; DEF; GHI; NO_WRAP; 5; FED; CAB; GAD; BID; HIGH
* Note: ";" indicate newline
*/
private void init() {
int offset = 0; // indicate offset in inputFile
if (this.input.size() == 0) {
System.out.println("failed to initial data!");
return;
}
String[] dim = this.input.get(offset++).split(" "); // read first row to get dimension of the grid
if (dim[0].matches("\\d+") && dim[1].matches("\\d+")) {
int row = Integer.parseInt(dim[0]);
int col = Integer.parseInt(dim[1]);
this.grid = new char[row][col];
for (int i = 0; i < row; i++) { // initial grid
String str = this.input.get(i + 1);
for (int j = 0; j < col; j++) {
this.grid[i][j] = str.charAt(j);
}
offset++;
}
} else {
System.out.println("failed to initial data!");
return;
} this.mode = this.input.get(offset++); // initial mode
if (!"WRAP".equals(this.mode) && !"NO_WRAP".equals(this.mode)) {
System.out.println("failed to initial data!");
return;
} for (int i = ++offset; i < this.input.size(); i++) { //skip the row which holds the number of the word
//the index of the word in output should be same in wordList
this.wordList.add(this.input.get(i));
this.output.put(this.input.get(i), new OutputFormat());
}
} public void search() {
int rows = this.grid.length - 1; // number of rows, subtract 1 for convenience
int cols = this.grid[0].length - 1; // number of columns for (int rowIdx = 0; rowIdx <= rows; rowIdx++) {
for (int colIdx = 0; colIdx <= cols; colIdx++) {
for (int rd = -1; rd <= 1; rd++){ //loop through 8 directions
for (int cd = -1; cd <= 1; cd++) {
if (rd != 0 || cd != 0) { //skip 0,0
searchWord(rowIdx, colIdx, rd, cd);
}
}
}
}
} this.printOutput();
} private void searchWord(int row, int col, int rd, int cd) {
StringBuffer buf = new StringBuffer(); //new StringBuffer to hold word
int rowBoundry = this.grid.length - 1;
int colBoundry = this.grid[0].length - 1;
int i = row;
int j = col;
if ("NO_WRAP".equals(this.mode)) { //NO WRAP
while (true) {
if (i < 0 || j < 0 || i > rowBoundry || j > colBoundry) {
break;
}
buf.append(this.grid[i][j]);
if (this.wordList.contains(buf.toString())) { //set output
this.output.get(buf.toString()).setFlag(true);
this.output.get(buf.toString()).setSi(row);
this.output.get(buf.toString()).setSj(col);
this.output.get(buf.toString()).setEi(i);
this.output.get(buf.toString()).setEj(j);
}
i = i + rd;
j = j + cd;
}
}
if ("WRAP".equals(this.mode)) { //WRAP
int ri = i;
int rj = j;
int loopFlag = 0;
while (true) {
ri = i;
rj = j;
while (ri < 0) {
ri = ri + rowBoundry + 1;
}
while (rj < 0) {
rj = rj + colBoundry + 1;
}
while (ri > rowBoundry) {
ri = ri - rowBoundry - 1 ;
}
while (rj > colBoundry) {
rj = rj - colBoundry - 1;
}
if (ri == row && rj == col && loopFlag != 0) {
break;
}
buf.append(this.grid[ri][rj]);
if (this.wordList.contains(buf.toString())) { //set output
this.output.get(buf.toString()).setFlag(true);
this.output.get(buf.toString()).setSi(row);
this.output.get(buf.toString()).setSj(col);
this.output.get(buf.toString()).setEi(ri);
this.output.get(buf.toString()).setEj(rj);
}
i = i + rd;
j = j + cd;
loopFlag++;
}
}
} private void printOutput() {
for(int i = 0; i < this.wordList.size(); i++) {
OutputFormat o = this.output.get(this.wordList.get(i));
if(o.getFlag()) {
System.out.println("("+o.getSi()+","+o.getSj()+")"+"("+o.getEi()+","+o.getEj()+")");
} else {
System.out.println("NOT FOUND");
}
}
} public static void main(String[] args) { WordSearch a = new WordSearch(args[0]);
a.search();
}
}