zoukankan      html  css  js  c++  java
  • Word Search

    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.

    这道题虽然代码写得有点长,但是思路还是比较清晰的

    思路:

    遍历word从0-length - 1,找出在board中的位置i,j从i,j的上下左右开始递归查找下一个字符。由于字符在一次匹配中不能使用两次,这里用了一个isVisited[][]进行访问控制。

     1 public class Solution {
     2     private boolean isExist = false;
     3     private boolean isVisited[][];
     4     
     5     public boolean exist(char[][] board, String word) {
     6         if(word.length() == 0)
     7             return false;
     8         int position_x;
     9         int position_y;
    10         char firstChar = word.charAt(0);        
    11         isVisited = new boolean[board.length][board[0].length];
    12         
    13         for(int i = 0; i < board.length; i++){
    14             if(isExist == true){
    15                 break;
    16             }
    17             for(int j = 0; j < board[0].length; j++){
    18                 if(isExist == true){
    19                     break;
    20                 }
    21                 if(firstChar == board[i][j]){
    22                     position_x = i;
    23                     position_y = j;
    24                     String curWord = String.valueOf(firstChar);
    25                     isVisited[position_x][position_y] = true;
    26                     isExit(board, word, 0, position_x, position_y, curWord);
    27                     initVisited();
    28                 }//if
    29             }//for
    30         }//for
    31         
    32         return isExist;
    33     }
    34     private void isExit(char [][]board, String word, int i, int position_x, int position_y, String curWord){
    35 //        System.out.println("curWord = " + curWord);
    36         if(curWord.length() == word.length()){
    37             if(board[position_x][position_y] == word.charAt(word.length() - 1))
    38                 isExist = true;
    39             return;
    40         }else{
    41             if(isExist)
    42                 return;
    43             if(board[position_x][position_y] == word.charAt(i)){                //遍历上线左右满足条件的
    44                 if(position_x - 1 >= 0 && isVisited[position_x - 1][position_y] == false){
    45                     isVisited[position_x - 1][position_y] = true;
    46                     isExit(board, word, i + 1, position_x - 1, position_y, curWord + String.valueOf(word.charAt(i + 1)));        //
    47                     isVisited[position_x - 1][position_y] = false;
    48                 }
    49                 if(position_x + 1 < board.length && isVisited[position_x + 1][position_y] == false){
    50                     isVisited[position_x + 1][position_y] = true;
    51                     isExit(board, word, i + 1, position_x + 1, position_y, curWord + String.valueOf(word.charAt(i + 1)));        //
    52                     isVisited[position_x + 1][position_y] = false;
    53                 }
    54                 if(position_y - 1 >= 0 && isVisited[position_x][position_y - 1] == false){
    55                     isVisited[position_x][position_y - 1] = true;
    56                     isExit(board, word, i + 1, position_x, position_y - 1, curWord + String.valueOf(word.charAt(i + 1)));     //
    57                     isVisited[position_x][position_y - 1] = false;
    58                 }
    59                 if(position_y + 1 < board[0].length && isVisited[position_x][position_y + 1] == false){
    60                     isVisited[position_x][position_y + 1] = true;
    61                     isExit(board, word, i + 1, position_x, position_y + 1, curWord + String.valueOf(word.charAt(i + 1)));     //
    62                     isVisited[position_x][position_y + 1] = false;
    63                 }
    64             }
    65         }
    66     }
    67     private void initVisited(){
    68         for(int i = 0; i < isVisited.length;i++){
    69             for(int j = 0; j < isVisited[0].length; j++){
    70                 isVisited[i][j] = false;
    71             }
    72         }
    73     }
    74 }
  • 相关阅读:
    分子动力学中步长的选取
    提高编程能力刷题网站
    【18】如何把数据存储到MongoDB数据库
    【17】有关python面向对象编程的提高【多继承、多态、类属性、动态添加与限制添加属性与方法、@property】
    【16】有关python面向对象编程
    【15】杂记章节
    【14】文件读取并格式化处理
    【13】python time时间模块知识点备查
    【11】python 递归,深度优先搜索与广度优先搜索算法模拟实现
    6380. 【NOIP2019模拟2019.10.06】小w与最长路(path)
  • 原文地址:https://www.cnblogs.com/luckygxf/p/4214140.html
Copyright © 2011-2022 走看看