The example of this article describes Java to achieve word search maze games. Share it for everyone for your reference. The specific analysis is as follows:
In magazines, we can often see mini -games looking for words. In a two -dimensional table, there are various letters, and we can find words from eight directions. This computer is very convenient to process it, but the quality of the algorithm is important, because if it is implemented with a brute force algorithm, the time it takes is unimaginable.
This is the realization of data structure and problems solve the realization of the realization of the Java language description.
The full code is as follows, the comment is very clear
Import java.io.BuffRedReader; Import Java.Io.fileRereader; Import Java.Io.inputStreamReader; Import Java.io.ioException; Import Java.util.ArrayList ; Import java.util.arrays; Import java.util.list;/ ** * Word search maze * * * */public class wordsearch {/** * In the constructor, construct two input streams, input streams of words, and input streams of tables * * */public wordsearch () Throws IOEXCEPTION {PUZZLESTREA m = OpenFile ("Enter Form File Path:"); WordStream = OpenFile ("Enter the word file path:"); System.out.println ("file reading ..."); readPuzzle (); Readwords ();} /*** @Return Matches There are how many words in total matching* Search from eight directions in each position* RD to indicate the incremental yield, EG: RD = -1, indicating that the upper line* CD indicates that the incrementing EG on the column EG : CD = -1. Indicates a step to the left* so RD = 1, CD = 0 means South* RD = -1, CD = 0 means North,* RD = -1, CD = 1, indicating Northeast*/ PUBLIC Int Solvepuzzle () {int matches = 0; for (int R = 0; R <ROWS; R ++) for (int C = 0; C <COLUMNS; C ++) for (int Rd = -1; RD <= 1; RD ++) for (int cd = -1 ; CD <= 1; CD ++) if (RD! = 0 || CD! = 0) Matches+= SolveDirection (R, C, RD, CD); Return Matches;} /**** on the specified coordinates, according to Search for a given direction, return the number of words to match * @Return Number of Matches */ Private int SolveDirection (Int BaseRow, Int Basecol, Int Rowdelta) {String CharSequen CE = ""; int Nummatches = 0; int SearchResult; charsequence + = Theboard [BaseRow] [Basecol]; for (int i = Baserow + Rowdelta, J = Basecol + Coldlta; i> = 0 && J> = 0 & & & J <Clumns; += rowdelta, J += COLDELTA) {charsequence += Theboard [i] [J]; SearchResult = PrefixSearch (TheWords, Charsequence); /*** IF below h) * must be judged, otherwise there will be cross -border Dangerous, and when the last word matching prefix, returns the index -1 * * / if (searchResult == thewords.length); Even if you continue to search, what you do is useless * */ if (! Thewords [SearchResult] .startswith (Charsequence) Break; if (thewords [searchResult] .equals (Charsequence)) {nummatics ++; System.out.println ("discovered " + Charsequence +" in " + Basereow + 1 +" Line " + Basecol +" column " + I +" + J);} Return Nummatches;} /*** first explain arrays.binarysearch (object [], Object) * Use a binary search algorithm to search for the specified array to obtain the specified object. Before this call, * must be sorted by the array according to the natural order of the array elements (through the above sort (Object [] method). * If there is no sorting of the array, the result is unclear. Elements that cannot be compared with each other (for example, string and integer), * cannot sort the array according to the natural order of the array elements, so the result is not clear) *保证找到的是哪一个。 */ private static int prefixSearch( String [ ] a, String x ) { int idx = Arrays.binarySearch( a, x ); if( idx < 0 ) return -idx - 1; else return idx ;} / *** Read the content of the file and get the input stream* / private bufferedReader OpenFile (String Message) {string filename = "" "" "" "" "" "" " System.out.println (Message + ": "); Try {filename = in.readline (); if (filename == null) system.exit (0); thefile = new filereader (FILENAME); } Catch (IOEXception E) {{ System.err.println ("Cannot Open" + FILENAME);} While (filein == NULL); System.out.println ("Opened" + FILENAME); * / Private void ReadPuzzle () Throws IOEXCEPTION {String OneLine; List <string> PuzzleLines = New ArrayList <string> (); if (OnLine = PuzzleStream.readline () ) == NULL) Throw New IOEXception ("No Lines in Puzzle file "); columns = OneLine.length (); PuzzleLines.add (One); While ((OnLine = PuzzleStream.Readline ())! = Null) {if (oneline.Length ()!! STEM.ERR. Println ("Puzzle is not Rectangular; Skipping Row"); Else PuzzleLines.add (One);} ROWS = PuzzleLines.size (); TheBoard = New Char [ROWS] [Columns ];; for (string theLine : puzzleLines) Theboard [R ++] = TheLine.tocharray ();} / *** read the word list that has been sorted by the dictionary* / private void Readwords () Throws IOEXCEPTION {list <String> Words = New ArrayList <string> ( );; String thisword; While ((this = wordstream.readline ())! = Null) {if (Lastword! = Null &&Compareto (Lastword) <0) {System.err.println (" Not sorted in the order of the dictionary, this time, "); Continue;} words.add (thisword.trim ())); lastword = Thisword;} Thewords = New String [words.size ()]; Y (Y ( Thewords;} // check main public void main (string [] art) {wordsearch p = null; try {p = new wordsearch ();} catch (IOEXCEPTION E) {System.ou T.println ("IO error: "); E.printstacktrace (); Return;} System.out.println (" Search ... "); p.solvepuzzle ();} Private into; TheBoard; Private String [] Thewords; Private BufferedReadreader Puzzlestream; Private BufferedReader WordStream; Private bufferedReader in = new bufferedReader (New InputStream Reader (System.in));}It is hoped that this article is helpful to everyone's Java program design.