[LeetCode-interview algorithm classic-Java implementation] [079-Word Search], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [079-Word Search], leetcode -- java
[079-Word Search )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]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 are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =

[  ["ABCE"],  ["SFCS"],  ["ADEE"]]

Word ="ABCCED",-> Returns true,
Word ="SEE",-> Returns true,
Word ="ABCB",-> Returns false.

Theme

Given a board character matrix, you can start from any point to go up or down the way, each point can only go once, if there is a path that matches the given string, then return true

Solutions

Start with each vertex and use backtracking to search

Code Implementation

Algorithm Implementation class

Public class Solution {public boolean exist (char [] [] board, String word) {// [Note that all input parameters are valid] // access tag matrix, by default, the initial value is set to false boolean [] [] visited = new boolean [board. length] [board [0]. length]; // start with each position. If you find a path, stop for (int I = 0; I <board. length; I ++) {for (int j = 0; j <board [0]. length; j ++) {if (search (board, visited, I, j, word, new int [] {0}) {return true ;}}} return false;}/*** @ par Am board character matrix * @ param visited access tag matrix * @ param row access row number * @ param col access column number * @ param word matching string * @ param idx matched position, the updated array value can be seen by other references * @ return */private boolean search (char [] [] board, boolean [] [] visited, int row, int col, String word, int [] idx) {// if the search position is equal to the String length, it indicates that the matched if (idx [0] = word has been found. length () {return true;} boolean hasPath = false; // The current position is valid if (check (board, visited, row, col, Word, idx [0]) {// indicates that the position has been accessed by visited [row] [col] = true; idx [0] ++; // on, right, bottom, search hasPath = search (board, visited, row-1, col, word, idx) in the left direction | search (board, visited, row, col + 1, word, idx) // right | search (board, visited, row + 1, col, word, idx) // | search (board, visited, row, col-1, word, idx); // left // trace back if (! HasPath) {visited [row] [col] = false; idx [0] --;} return hasPath ;} /*** determine whether the access location is valid ** @ param board character matrix * @ param visited access tag matrix * @ param row access row number * @ param col access column number * @ param word matched string * @ param idx matched position * @ return */public boolean check (char [] [] board, boolean [] [] visited, int row, int col, String word, int idx) {return row> = 0 & row <board. length // The row number is valid & amp; col & gt; = 0 & amp; col <board [0]. Length // The column number is valid &&! Visited [row] [col] // not accessed & board [row] [col] = word. charAt (idx); // equal character }}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47248121]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.