Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.3 kB
4
Indexable
Never
class Solution {
public:

    bool canPlaceHor(string& word, vector<vector<char>>& board, int i, int j){
        if (j - 1 >= 0 && board[i][j - 1] != '#'){
            return false; 
        }
        if (j + word.length() < board[0].size() && board[i][j + word.length()] != '#'){
            return false; 
        }

        for (int jj = 0; jj<word.length(); jj++){
            if (j + jj >= board[0].size()){
                return false; 
            }
            if (board[i][j + jj] == ' ' || board[i][j + jj] == word[jj]){
                continue;
            }else{
                return false; 
            }
        }

        return true; 
    }
    bool canPlaceHorRev(string& word, vector<vector<char>>& board, int i, int j){
        if (j + 1 < board[0].size() && board[i][j + 1] != '#'){
            return false; 
        }
        if (j - word.length() >= 0 && board[i][j - word.length()] != '#'){
            return false; 
        }

        for (int jj = 0; jj<word.length(); jj++){
            if (j - jj < 0){
                return false; 
            }
            if (board[i][j - jj] == ' ' || board[i][j - jj] == word[jj]){
                continue;
            }else{
                return false; 
            }
        }

        return true; 
    }

    bool canPlaceVer(string& word, vector<vector<char>>& board, int i, int j){
        if (i - 1 >= 0 && board[i - 1][j] != '#'){
            return false; 
        }
        if (i + word.length() < board.size() && board[i + word.length()][j] != '#'){
            return false; 
        }

        for (int ii = 0; ii<word.length(); ii++){
            if (i + ii >= board.size()){
                return false; 
            }
            if (board[i + ii][j] == ' ' || board[i + ii][j] == word[ii]){
                continue;
            }else{
                return false; 
            }
        }

        return true; 
    }

    bool canPlaceVerRev(string& word, vector<vector<char>>& board, int i, int j){
        if (i + 1 < board.size() && board[i + 1][j] != '#'){
            return false; 
        }
        if (i - word.length() >= 0 && board[i - word.length()][j] != '#'){
            return false; 
        }

        for (int ii = 0; ii<word.length(); ii++){
            if (i - ii < 0){
                return false; 
            }
            if (board[i - ii][j] == ' ' || board[i - ii][j] == word[ii]){
                continue;
            }else{
                return false; 
            }
        }

        return true; 
    }






    bool canPlace(string& word, vector<vector<char>>& board, int i, int j){
        return canPlaceHor(word, board, i, j) || canPlaceHorRev(word, board, i, j) || canPlaceVer(word, board, i, j) || canPlaceVerRev(word, board, i, j);
    }



    bool placeWordInCrossword(vector<vector<char>>& board, string word) {
        int n = board.size(); 
        int m = board[0].size(); 
        bool ans = false;
        for (int i = 0; i<n; i++){
            for (int j = 0; j<m; j++){
                if (board[i][j] != '#'){
                    ans = ans || canPlace(word, board, i, j);
                }
            }
        }

        return ans;
    }
};