时差爆发,睡不着,闲的蛋疼。。。思路写在注释里,直接撸代码:
class="java" name="code">import java.util.*;
public class WordPuzzle {
private char[][] puzzle;
private List<String> wordList;
public WordPuzzle(char[][] puzzle, List<String> wordList) {
this.puzzle = puzzle;
this.wordList = wordList;
}
/**
* Solve the puzzle.<br/>
* result: {beginIndex(row, column);endIndex(row, column);matchWord}<br/>
* temp tuple: {beginIndex(row,column);endIndex(row,column);direction;composedChars2String}<br/>
* Procedure:<br/>
* for each element in the puzzle {<br/>
* construct a tuple;<br/>
* check word list and see whether there is a match;<br/>
* }
*/
public List<String> solvePuzzle() {
List<String> resultList = new LinkedList<String>();
List<String> tupleList = null;
String tempResult = null;
String[] strings = null;
for(int i = 0; i < puzzle.length; i++) {
for(int j = 0; j < puzzle[i].length; j++) {
tupleList = findAllString(i, j);
for(String temp : tupleList) {
strings = temp.split(";");
if(wordList.contains(strings[1])) {
tempResult = "(" + i + "," + j + ");" + strings[0] + ";" + strings[1];
resultList.add(tempResult);
tempResult = null;
}
}
}
}
return resultList;
}
/**
* Find all possible composed strings and return as a list along with the end index.<br/>
* eg. {(row,column);string}
*/
private List<String> findAllString(int beginRow, int beginColumn) {
List<String> resultList = new LinkedList<String>();
List<String> tempList = null;
tempList = findAllRow(beginRow, beginColumn);
Collections.copy(resultList, tempList);
tempList = findAllColumn(beginRow, beginColumn);
Collections.copy(resultList, tempList);
tempList = findAllDiagonal(beginRow, beginColumn);
Collections.copy(resultList, tempList);
return resultList;
}
private List<String> findAllRow(int i, int j) {return null;}
private List<String> findAllColumn(int i, int j) {return null;}
private List<String> findAllDiagonal(int i, int j) {return null;}
}
?
?