(Java) Leetcode 79. Word search--Word Search

Source: Internet
Author: User

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.

Example:

Board =[  [' A ', ' B ', ' C ', ' e '],  [' s ', ' F ', ' C ', ' s '],  [' A ', ' D ', ' e ', ' e ']]given word = ' abcced ', return true. Given Word = "See", return True. Given Word = "ABCB", return false.

At a glance is a DFS problem, but in the process of doing it found that some situation made it into an atypical DFS. The essence of this problem is to find the path, that is, to look for the specified path in the graph--each element as a node in the graph, the upper and lower left and right elements as nodes adjacent nodes. That is, you need to verify in the diagram whether there are lines along a given word path. It is important to note that the traditional DFS only needs to complete the access of all the nodes to know if there is a certain path that meets the requirements, and the problem is to allow repeated access to the "Fork node" in the process of finding the path. The reason is that when Dfs goes to a node, if the node has more than one contiguous node that satisfies the next node requirement for a given path, then if one of the paths does not meet the final total path requirement, we also need to backtrack to the fork node to verify that another fork from this node satisfies the remaining path requirements. Therefore, this is equivalent to adding backtracking in Dfs, that is, we want to record each fork node, and then verify the path after the completion of the verification of the fork node back to the original state, in order to make other fork judgment. Since we want to record the fork node, we can actually set the fork node to a special character, thus eliminating a two-dimensional visited Boolean array, saving some space.

Java

classSolution {Public boolean exist (char[][] board, String word) {if(board = = NULL | | board.length = = 0 | | board[0].length = = 0)returnfalse; if(word.length () = = 0)returntrue;  for(int i = 0; i < board.length; i++) {             for(int j = 0; J < Board[0].length; J + +) {int P=0; if(Word.charat (0) = =Board[i][j])if(DFS (board, I, J, Word, p))returntrue; }         }        returnfalse; } Private Boolean DFS (char[][] board, int i, int J, String Word, int p) {if(p = = Word.length ())returntrue; if(I >= board.length | | I < 0 | | J >= Board[0].length | | J < 0 | | board[i][j]! = Word.charat (P))returnfalse; Char temp=Word.charat (P); BOARD[I][J]='*'; Boolean flag= DFS (board, I, j+1, Word, p+1) | | DFS (board, I, j-1, Word, p+1) | | DFS (board, i+1, J, Word, p+1) | | DFS (board, I-1, J, Word, p+1); BOARD[I][J]=temp; returnFlag; }}

(Java) Leetcode 79. Word search--Word Search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.