1 package s1;
2 import java.io.BufferedReader;
3 import java.io.*;
4 import java.util.*;
5
6 //WordSearch class interface:solve word search puzzle
7 //
8 //COnstruction:with no initializer
9 //*****************public operations*******************
10 //int sovePuzzle() --->Print all words found in the puzzle ;
11 // return number of matchers
12
13 public class WordSearch {
14 public WordSearch() throws IOException
15 {
16 /*WordSearch类的构造函数*/
17 puzzleStream=openFile("Enter puzzle file");
18 wordStream =openFile("Enter dictionary name");
19 System.out.println("Reading files.....");
20 readPuzzle();
21 readWords();
22 }
23
24 public int solvePuzzlew()
25 {
26 //从所有起点开始,搜索所有方向的solvePuzzle程序
27 /*routine to solve the word serach puzzle
28 * Performs checks in all eight directions
29 * @ return number of matchs */
30 int matches =0;
31
32 for(int r=0; r<rows;r++)
33 for(int c=0;c<columns;c++)
34 for(int rd =-1;rd<=1;rd++)
35 for(int cd=-1;cd<=1;cd++)
36 if(rd!=0||cd!=0)
37 matches+=solveDirection(r,c,rd,cd);
38 return matches;
39 }
40 private int rows;
41 private int columns;
42 private char theBoard[][];
43 private String [] theWords;
44 private BufferedReader puzzleStream;
45 private BufferedReader wordStream;
46 private BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
47 private static int prefixSearch(String [] a, String x)
48 {
49 int idx=Arrays.binarySearch(a, x);
50 if(idx<0)
51 return -idx-1;
52 else
53 return idx;
54 }
55 /*用于打开单词列表或者网格的openFile方法*/
56 private BufferedReader openFile(String message)
57 {
58 String fileName="";
59 FileReader theFile;
60 BufferedReader fileIn=null;
61 do
62 {
63 System.out.println(message+":");
64 try
65 {
66 fileName=in.readLine();
67 if(fileName==null)
68 System.exit(0);
69 theFile=new FileReader(fileName);
70 fileIn=new BufferedReader(theFile);
71
72 }
73 catch(IOException e)
74 {
75 System.err.println("Cannot open " +fileName);
76 }
77 }
78 while(fileIn==null);
79 System.out.println("Opened"+fileName);
80 return fileIn;
81 }
82
83 /*
84 * Routine to read the dictionary
85 * Error message is printed if dictionary is not sorted
86 * */
87 /*用于读取单词列表的readWords()方法*/
88
89 private void readWords () throws IOException
90
91 {
92 List<String>words=new ArrayList<String>();
93 String lastWord=null;
94 String thisWord;
95 while((thisWord=wordStream.readLine())!=null)
96 {
97 if(lastWord!=null&&thisWord.compareTo(lastWord)<0)
98 {
99 System.err.println("Dictionary is not sorted ....skipping");
100 continue;
101 }
102 words.add(thisWord);
103 lastWord=thisWord;
104 }
105 theWords=new String[words.size()];
106 theWords=words.toArray(theWords);
107
108 }
109
110 //读取网格的readPuzzle方法
111 /* Routine to read the grids
112 * Check to ensure that the grid is rectangular
113 * Checks to make sure that capacity is not exceeded is omitted
114 * */
115 private void readPuzzle() throws IOException
116 {
117 String oneLine;
118 List<String> puzzleLines=new ArrayList<String>();
119
120 if((oneLine=puzzleStream.readLine())==null)
121 {
122 throw new IOException("No lines in puzzle file");
123
124 columns=oneLine.length();
125 puzzleLines.add(oneLine);
126
127 while((oneLine=puzzleStream.readLine())!=null)
128 {
129 if(oneLine.length()!=columns)
130 {
131 System.out.println("Puzzle is not rectangular;skipping row");
132 }
133 else
134 {
135 puzzleLines.add(oneLine);
136 }
137 rows=puzzleLines.size();
138 theBoard=new char[rows][columns];
139
140 int r=0;
141 for(String theLine:puzzleLines)
142 {
143 theBoard[r++]=theLine.toCharArray();
144
145 }
146 }
147
148 }
149
150 }
151 private int solveDirection(int baseRow, int baseCol, int rowDelta ,int colDelta)
152 {
153 /* 一个搜索的实现*/
154 /*
155 * Search the grid form a starting point and direction
156 * @return number of matches*/
157
158 String charSequence="";
159 int numMatches=0;
160 int searchResult;
161 charSequence+=theBoard[baseRow][baseCol];
162 for(int i=baseRow+rowDelta,j=baseCol+colDelta;
163 i>=0&&j>=0&&i<rows&&j<columns;
164 i+=rowDelta,j+=colDelta
165 )
166 {
167 charSequence+=theBoard[i][j];
168 searchResult=prefixSearch(theWords,charSequence);
169
170 if(searchResult==theWords.length)
171 break;
172 if(!theWords[searchResult].startsWith(charSequence))
173 break;
174 if(theWords[searchResult].equals(charSequence))
175 {
176 numMatches++;
177 System.out.println("Found"+charSequence+"at"+baseRow+""+baseCol+"to"+i+""+j);
178 }
179 }
180 return numMatches;
181
182 }
183 public static void main(String [] args)
184 {
185 WordSearch p=null;
186 try
187 {
188 p=new WordSearch();
189 }
190 catch(IOException e)
191 {
192 System.out.println("IO Error");
193 e.printStackTrace(); //输出标准异常
194 return;
195 }
196 System.out.println("Solving..........");
197 p.solvePuzzlew();
198 }
199 }