zoukankan      html  css  js  c++  java
  • [leetcode]79.Search Word 回溯法

    /**
     * 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 =
    
     [
     ['A','B','C','E'],
     ['S','F','C','S'],
     ['A','D','E','E']
     ]
     word = "ABCCED", -> returns true,
     word = "SEE", -> returns true,
     word = "ABCB", -> returns false.
     */
    /*
    * 回溯法:递归加循环,找准时机回溯状态
    * 定义一个数组记录当前位置有没有已经访问过,这个数组就是需要回溯的状态
    * 把每个元素当做开头循环一遍,每次判断当前元素的上下左右,如果有符合条件的就再进入递归判断上下左右
    * 直到判断到target的最后一个字符符合就返回true,每次把递归放在if的判断条件里,这样只要有一个true就会
    * return出去,返回false就再换个位置继续判断
    * 注意回溯的位置,是确定当前位置的上下左右都不可用时再回溯*/
    public class Q79WordSearch {
        public static void main(String[] args) {
            char[][] board = new char[][]{{'a','b','c','e'},{'s','f','c','s'},{'a','d','e','e'}};
            String word = "abcced";
            Q79WordSearch q = new Q79WordSearch();
            System.out.println(q.exist(board,word));
        }
        public boolean exist(char[][] board, String word) {
            if (word.length() == 0)
                return true;
            //记录是否访问过的表格
            int[][] flag = new int[board.length][board[0].length];
            boolean res = false;
            //数组中的每个元素作为开头,遍历一次
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if (gobacking(board,word,flag,i,j,0))
                        return true;
                }
            }
            return false;
        }
        public boolean gobacking(char[][] board,String word,int[][] flag,int row,int col,int num)
        {
            char ch = word.charAt(num);
            //越界或者访问过就返回
            if (row < 0 || row > board.length-1 || col < 0 ||col > board[0].length-1 || flag[row][col] == 1)
                return false;
            //不符合条件
            if (ch != board[row][col])
                return false;
            //符合条件,记录数组相应位置置1,代表访问过了
            flag[row][col] = 1;
            //如果最后一个位置的字母也相等,那就是符合条件了
            if (num == word.length()-1)
                return true;
            for (int i = -1;i <= 1;i++)
            {
                for (int j = -1; j <= 1; j++) {
    //             只循环上下左右
                    if (Math.abs(i) == Math.abs(j))
                        continue;
                    //只有符合条件才会往下判断
                    if (gobacking(board,word,flag,row+i,col+j,num+1))
                        return true;
                }
            }
            //执行到这里就是没有符合条件的,回溯,把此次的位置置0
            flag[row][col] = 0;
            //能执行到这里说明没有符合条件的
            return false;
        }
    }
  • 相关阅读:
    分享5个viewport相关的jQuery插件
    超棒的响应式jQuery网格布局插件 grida licious
    6款不容错过的超棒倒计时jQuery插件
    分享45套2011年和2012年的高质量免费网站模板
    分享11个使用方便的免费智能手机UI套件
    推荐30款超精致的体育类型的网站设计
    HDOJ1001
    HDOJ1003
    HDOJ1000
    HDOJ1002
  • 原文地址:https://www.cnblogs.com/stAr-1/p/7286413.html
Copyright © 2011-2022 走看看