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. js冒泡排序

    今天面试了家公司,最后要写个js的简单数组排序,很久都写不出来,好尴尬,随着语言的发展,这些简单方法越来越不被重视了... <html> <head> <script t ...

  2. Go语言 使用内置Http组件

    package main import ( "net/http" ) func SayHello(w http.ResponseWriter, req *http.Request) ...

  3. Webpack 入门指迷--转载(题叶)

    最近看到这个东西,一头雾水.看了一些资料了解了Webpack概念,大体是webpack 是一个模块绑定器,主要目的是在浏览器上绑定 JavaScript 文件. 看到题叶写的一篇介绍,写的很好,转载连 ...

  4. 实现携程X分钟前有人预定功能

    实现携程X分钟前有人预定功能 原理:利用cookie与计时器两部分: 首先,进入页面,x会被随机数赋值,赋值后x会一分钟加1,直到加到60,再从1开始累加. 页面是否相同是根据页面的url后的id值判 ...

  5. word-wrap: break-word;和word-break: break-all;的区别

    详细查看以下链接.(转载自张鑫旭大神空间) http://www.zhangxinxu.com/wordpress/2015/11/diff-word-break-break-all-word-wra ...

  6. JavaScript、tabel切换完整版—自动切换—鼠标移入停止-移开运行

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Android四大组件之actiivity

    1.Acitivity Activity是Android一个非常重要的用户接口(四大组件之一),是可见的,主要是用户和应用程序之间进行交互的接口.在每个Activity中都可以放很多控件,所以也可以把 ...

  8. 安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)

    package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import ...

  9. CSS 学习笔记

    0.CSS概念层叠样式表(Cascading Style Sheets),CSS的来历就不必多说了.可以简单的理解为万维网联盟(w3c)为了丰富HTML页面的布局和外观而指定的一种标准. 1.CSS实 ...

  10. HDU 5965 枚举模拟 + dp(?)

    ccpc合肥站的重现...一看就觉得是dp 然后强行搞出来一个转移方程 即 根据第i-1列的需求和i-1 i-2列的枚举摆放 可以得出i列摆放的种类..加了n多if语句...最后感觉怎么都能过了..然 ...