• [leetcode]Word Search


    枚举开始位置,DFS验证

    const int dx[] = {0,0,1,-1};
    const int dy[] = {1,-1,0,0};
    class Solution {
    public:
        unordered_set<long long> flag;
        bool check(vector<vector<char> > & board , string& word , int x , int y , int pos){
            if(pos == word.size()) return true;
            for(int i = 0 ; i < 4 ; i++){
                int tx = x + dx[i];
                int ty = y + dy[i];
                if(tx >= 0 && tx < board.size() && ty >= 0 && ty < board[tx].size()){
                    if(flag.find(tx*100000+ty) == flag.end() && board[tx][ty] == word[pos]){
                        flag.insert(tx*100000+ty);
                        if(check(board , word , tx , ty , pos + 1)){
                            return true;
                        }else{
                            flag.erase(tx*100000+ty);
                        }
                    }
                }
            }
            return false;
        }
        bool exist(vector<vector<char> > &board, string word) {
            if(word == "") return true;
            int size = board.size();
            
            for(int i = 0 ; i < size ; i++){
                for(int j = 0 ; j < board[i].size() ; j++){
                    if(board[i][j] == word[0]){
                        flag.clear();
                        flag.insert(i*100000 + j);
                        if(check(board , word , i , j , 1)) return true;
                    }
                }
            }
            return false;
        }
    };
  • 相关阅读:
    DSP 知识点
    JVM中的垃圾收集
    MyBatis中的命名空间namespace的作用
    Gradle各版本下载地址
    redis学习笔记
    Mybaties 的缓存
    zookeeper配置集群报错Mode: standalone
    ZooKeeper 典型应用场景
    Linux下搭建mongDB环境
    关系型数据库三范式
  • 原文地址:https://www.cnblogs.com/x1957/p/3499625.html
Copyright © 2020-2023  润新知