079-word Search (Word search)
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or V Ertically neighboring. The same letter cell is used more than once.
For example,
Given board =
[ ["ABCE"], ["SFCS"], ["ADEE"]]
Word = "ABCCED"
, returns True,
Word = "SEE"
, returns True,
Word = "ABCB"
, returns FALSE.
Main Topic
Given a board character matrix, you can start at any point through the way up and down, each point can only walk once, if there is a path through the character equal to the given string, then return True
Thinking of solving problems
Search using backtracking as a starting point for each point
Code Implementation
Algorithm implementation class
Public class solution { Public Boolean exist(Char[] board, String Word) {//"Note that we assume that the input parameters are legal" //Access tag matrix, initial value set to False by default Boolean[] visited =New Boolean[Board.length] [board[0].length];//Search at each location as a starting point, and find a path to stop for(inti =0; i < board.length; i++) { for(intj =0; J < board[0].length; J + +) {if(Search (board, visited, I, J, Word,New int[]{0})) {return true; } } }return false; }/** * @param Board character Matrix * @param visited access tag matrix * @param row number of row access *< c9> @param Col Access column number * @param Word matches the string * @param idx matches the position, the array is the updated value can be other cited Use what you see * @return * * Private Boolean Search(Char[] board,Boolean[] visited,intRowintCol, String Word,int[] idx) {//If the location of the search equals the length of the string, it indicates that a match has been found if(idx[0] = = Word.length ()) {return true; }BooleanHaspath =false;//Current position valid if(Check (board, visited, row, col, Word, idx[0])) {//Mark location has been accessedVisited[row][col] =true; idx[0]++;search on the top, right, bottom, and left four directionsHaspath = Search (board, visited, row-1, col, Word, idx)//On|| Search (board, visited, row, col +1, Word, IDX)//Right|| Search (board, visited, Row +1, col, Word, idx)//Down|| Search (board, visited, row, col-1, Word, IDX);//Left //Backtracking if no path is found if(!haspath) {Visited[row][col] =false; idx[0]--; } }returnHaspath; }/** * Determine if the location of the access is valid * * @param Board character Matrix * @param visited access tag matrix * @param
row number for row access * @param Col Access column number * @param Word matches the string * @param idx Matching location * @return * / Public Boolean Check(Char[] board,Boolean[] visited,intRowintCol, String Word,intIDX) {returnRow >=0&& Row < Board.length//Line number legal&& Col >=0&& Col < board[0].length//Column number legal&&!visited[row][col]//have not been visited&& Board[row][col] = = Word.charat (idx);//character typeface, etc.}}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47248121"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "079-word Search (Word search)"