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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  4. 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 ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

随机推荐

  1. IOS真机测试

    学习了两天的Android开发,我感觉Android开发跟IOS开发和.NET平台下的开发有点不同,Android开发我更觉得跟web(Html)倒是有类似的地方,都是节点标签显示的,当然个人理解,感 ...

  2. Eclipse - 常用插件介绍

    1.MyBatis Generator 2.FindBugs Feature http://findbugs.cs.umd.edu/eclipse 待完善.

  3. SwipeRefreshLayout下拉刷新简单用例

    自己的下拉刷新组件 下拉刷新并自动添加数据 MainActivity package com.shaoxin.myswiperefreshlayout; import android.graphics ...

  4. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  5. css position的使用

    css position的使用 css 的 position 属性是用来设置元素的位置的,它还能设置一个元素出现在另一个元素的下层元素能用 top,bottom,left 和 right 属性设置位置 ...

  6. ios 单例设计模式

    单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类.单例可用性非常高,用于登录用户管理等可供全局调用. + (AccountMa ...

  7. Intellij如何设置编译后自动重新加载class文件?

    前段时间突然发现Intellij不能自动重新加载类了,每次编译后都要重新启动项目,才能显示更新效果,后来网上查询Intellij下如何配置热部署,都说是要配置构件,然后在web容器的编辑页面选择upd ...

  8. python技巧 之文件读取

    对于数据分析而言,我们通常需要将文件内容读取到列表中来进行后续的操作. np.array(dataFrame)能将dataFrame类型转换成数组类型. 1.pandas下的文本文件读取(推荐)

  9. 完成对数据库的CRUD操作

    PS:查询相对复杂,要处理结果集,增删改则不用. package it.cast.jdbc; import java.sql.Connection; import java.sql.ResultSet ...

  10. USACO 2014 FEB 银组

    1.自动打字{Silver题1} [问题描述] 贝西新买了手机,打字不方便,请设计一款应用,帮助她快速发消息. 字典里有W(W<=30000)个小写字母构成的单词,所有单词的字符总数量不超过1, ...