13_Recursion/4.cpp
/*
 * Complete the 'crosswordPuzzle' function below.
 *
 * The function is expected to return a STRING_ARRAY.
 * The function accepts following parameters:
 *  1. STRING_ARRAY crossword
 *  2. STRING words
 */
Complete the 'crosswordPuzzle' function below.
The function is expected to return a STRING_ARRAY.
The function accepts following parameters:
1.
Complete the ' crosswordPuzzle ' function below . The function is expected to return a STRING_ARRAY . The function accepts following parameters : 1 .
bool crosswordPuzzle(vector<string>& crossword, vector<string>& words) {
    auto try_place = [&](auto i, auto j, auto r) {
        auto word = words.back();
        if ((r?j:i)+word.length() > 10) return false;
        for (size_t k = 0; k < word.length(); ++k) {
            if (crossword[r?i:i+k][r?j+k:j] != '-' 
             && crossword[r?i:i+k][r?j+k:j] != word[k])
                return false;
        }                
        auto crossword_copy = crossword;
        for (size_t k = 0; k < word.length(); ++k)
            crossword[r?i:i+k][r?j+k:j] = word[k];
        words.pop_back();
        bool success = crosswordPuzzle(crossword, words);
        words.push_back(word);
        if (!success) crossword = crossword_copy;
        return success;
    };
    if (words.size() == 0) return true;
    for (size_t i = 0; i < 10; ++i) {
        for (size_t j = 0; j < 10; ++j) {
            if (try_place(i, j, 1)) return true;
            if (try_place(i, j, 0)) return true;
        }
    }
    return false;
}
bool crosswordPuzzle ( vector < string > & crossword , vector < string > & words ) { auto try_place = [ & ] ( auto i , auto j , auto r ) { auto word = words . back ( ) ; if ( ( r ? j : i ) + word . length ( ) > 10 ) return false ; for ( size_t k = 0 ; k < word . length ( ) ; ++ k ) { if ( crossword [ r ? i : i + k ] [ r ? j + k : j ] != ' ' && crossword [ r ? i : i + k ] [ r ? j + k : j ] != word [ k ] ) return false ; } auto crossword_copy = crossword ; for ( size_t k = 0 ; k < word . length ( ) ; ++ k ) crossword [ r ? i : i + k ] [ r ? j + k : j ] = word [ k ] ; words . pop_back ( ) ; bool success = crosswordPuzzle ( crossword , words ) ; words . push_back ( word ) ; if ( ! success ) crossword = crossword_copy ; return success ; } ; if ( words . size ( ) == 0 ) return true ; for ( size_t i = 0 ; i < 10 ; ++ i ) { for ( size_t j = 0 ; j < 10 ; ++ j ) { if ( try_place ( i , j , 1 ) ) return true ; if ( try_place ( i , j , 0 ) ) return true ; } } return false ; }
Complete the 'crosswordPuzzle' function below.
Complete the ' crosswordPuzzle ' function below .
/*by row*/ /*by col*/