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 ...
随机推荐
- C++ 定义全局数组
数组怎么用,全局数组就怎么用,只是他的作用域不一样才叫全局数组... 在A.h 或 A.cpp中定义char buf[10]; 如果在B.cpp要用,就在其开头中写成 extern char buf[ ...
- 比管理员(administrator)更高权限的TrustedInstaller
http://www.gezila.com/tutorials/9664.html 什么是TrustedInstaller管理权限 ?好多朋友都在使用Windows7系统.在使用过程中,有些朋友在删除 ...
- iOS--UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】
#import "ViewController.h" #define IMAGENUMBER 5 #define SIZE self.view.bounds.size @inter ...
- BZOJ4712 : 洪水
首先不难列出DP方程: $dp[x]=\min(w[x],h[x])$ $h[x]=\sum dp[son]$ 当$w[x]$增加时,显然$dp[x]$不会减少,那么我们求出$dp[x]$的增量$de ...
- VR的世界里没有雾霾!暴风魔镜发布Matrix一体机
在2016年接近尾声的时候,暴风魔镜给VR行业带来一波暖流.12月20日,暴风魔镜宣布推出最新VR一体机--暴风魔镜"3K屏概念机"MATrix及VR眼镜S1两大产品. ...
- js简单放羊式单元测试-上
这是看了很多js单元测试资料后第一次自己做单元测试,因为资料都在介绍工具怎么使用,js单元测试的工具是在是太多了,各种风格,各种支持的,新的旧的,so 还是自己动手来体验一次 简单 是我给自己的需求很 ...
- Windows程序内部运行机制 转自http://www.cnblogs.com/zhili/p/WinMain.html
一.引言 要想熟练掌握Windows应用程序的开发,首先需要理解Windows平台下程序运行的内部机制,然而在.NET平台下,创建一个Windows桌面程序,只需要简单地选择Windows窗体应用程序 ...
- 关于ps中的锯齿
1.1 索引透明颜色与Alpha透明通道 要说索引颜色透明,首先要讲讲什么是索引颜色,百度百科上有对索引颜色的解释,我觉得很关键的一句是“挑选一副图片中最有代表性的若干种颜色(通常不超过256种) ...
- DOS 命令For精解示例
最基本形态: 在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for %%I in (command1) do command2 在批处理中,FOR ...
- 几款不错的VisualStudio2010插件
1.Gradient Selection 这个插件能使VisualStudio的高亮文本看起来是类似Blend的那样的效果,看起来更加舒服 2.LineAdornments 这个插件可以高亮光标所在的 ...