word search puzzle
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();
}
}
word search puzzle的更多相关文章
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- Word Search I & II
Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 51. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 79. 212. Word Search *HARD* -- 字符矩阵中查找单词
79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
随机推荐
- Logistic回归的使用
Logistic回归的使用和缺失值的处理 从疝气病预测病马的死亡率 数据集: UCI上的数据,368个样本,28个特征 测试方法: 交叉测试 实现细节: 1.数据中因为存在缺失值所以要进行预处理,这点 ...
- 系统吞吐量(TPS)、用户并发量
PS:下面是性能测试的主要概念和计算公式,记录下: 一.系统吞度量要素: 一个系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联. 单个reqeust 对CPU消耗越高 ...
- 逗号分割符--字段中含逗号等情况的解析方法Java实现
最近在处理文本字符串时,没一行数据都是按照逗号分割的,每个字段值一般情况是带有双引号的,但是有的字段值里面还包含逗号,甚至有的字段就没有双引号,这个分割起来就有点麻烦了 下面说一下我解决方法,如果谁有 ...
- 树链剖分+线段树 BZOJ 1036 [ZJOI2008]树的统计Count
题目链接 题意: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节 ...
- Objective-C 中self.与_访问方式的区别
Objective-C中属性self.a与_a访问的区别: 在OC中我们可以通过指令@property定义属性. OC对属性封装了许多方法,同时也会自动实现一些方法,相比实例变量,感觉更加面向对象些. ...
- InstallShield Limited Edition制作安装文件
由于InstallShield Limited Edition for Visual Studio的教程.资料太少,所以我今天才决定写这个文章,专门针对C#项目打包,包括打包集成Microsoft . ...
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- mysql like 贪婪匹配 同时匹配多个值
LIKE "%a%b%c%",这样匹配出的就是包含a,b,c三个关键词的记录 (三个关键词不在一起时) 不好用 mysql> select count(1) from dm ...
- MSSQL 死锁查询
SELECT SYS.DM_EXEC_REQUESTS.SESSION_ID,TEXT AS '执行SQL',CLIENT_NET_ADDRESS AS '请求IP',SYS.DM_EXEC_CONN ...