zoukankan      html  css  js  c++  java
  • LeetCode "Word Search"

    DFS + Backtrace + Pruning

    class Solution {
    public:
        vector<vector<bool> > visited;
        bool checkPos(vector<vector<char> > &board, char c, int currX, int currY)
        {        
            if (currX < 0 || currX >= board[0].size()) return false;
            if (currY < 0 || currY >= board.size()) return false;
            if (visited[currY][currX]) return false;
            return board[currY][currX] == c;
        }
        bool _exist(vector<vector<char> > &board, string word, int currX, int currY)
        {
            if (word.length() == 0) return true;
    
            char c = word[0];
            visited[currY][currX] = true;
    
            if (word.length() == 1)
            {            
                return    (checkPos(board, c, currX - 1, currY)) ||
                        (checkPos(board, c, currX, currY - 1)) ||
                        (checkPos(board, c, currX + 1, currY)) ||
                        (checkPos(board, c, currX, currY + 1));
            }
    
            if (checkPos(board, c, currX - 1, currY))
            {
                bool bLeft = _exist(board, word.substr(1, word.length() - 1), currX - 1, currY);
                if (bLeft) return true;
                visited[currY][currX - 1] = false;
            }
            if (checkPos(board, c, currX, currY - 1))
            {
                bool bUp = _exist(board, word.substr(1, word.length() - 1), currX, currY - 1);
                if (bUp) return true;
                visited[currY - 1][currX] = false;
            }
            if (checkPos(board, c, currX + 1, currY))
            {
                bool bRight = _exist(board, word.substr(1, word.length() - 1), currX + 1, currY);
                if (bRight) return true;
                visited[currY][currX + 1] = false;
            }
            if (checkPos(board, c, currX, currY + 1))
            {
                bool bDown = _exist(board, word.substr(1, word.length() - 1), currX, currY + 1);
                if (bDown) return true;
                visited[currY + 1][currX] = false;
            }
            return false;
        }
        bool exist(vector<vector<char> > &board, string word) {
            int n = board.size(); if (n == 0) return false;
            int m = board[0].size(); if (m == 0) return false;
            
            visited.resize(n);
            for (int i = 0; i < n; i++)
            {
                visited[i].resize(m);
                std::fill(visited[i].begin(), visited[i].end(), false);
            }
    
            char c = word[0];
            for (int j = 0; j < n; j ++)
            for (int i = 0; i < m; i ++)
            {
                if (board[j][i] == c)
                {
                    if (_exist(board, word.substr(1, word.length() - 1), i, j)) return true;
                    visited[j][i] = false;
                }
            }
            return false;
        }
    };
  • 相关阅读:
    视频学习网站
    保存文章
    maven常见命令总结
    Eclipse vs IDEA快捷键对比大全(win系统)
    JS调用android逻辑方法
    【原创】不用封装jar包 直接引入工程使用的方法(类似android的 is Library功能)
    windows下eclipse+hadoop2
    Solaris用户管理(一):用户与组管理
    jquery 操作 checkbox
    模拟用户登录的操作
  • 原文地址:https://www.cnblogs.com/tonix/p/3890122.html
Copyright © 2011-2022 走看看