Word Search Apr 18 '12
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 =[ ["ABCE"], ["SFCS"], ["ADEE"]]word =
"ABCCED"
, -> returns true
, word = "SEE"
, -> returns true
, word = "ABCB"
, -> returns false
. uncompleted
class Position{public: int x; int y; Position(int xx, int yy) : x(xx),y(yy){}; };class Solution {public: bool exist(vector> &board, string word) { // Start typing your C/C++ solution below // DO NOT write int main() function if(word.size() ==0) return true; if(board.size()==0) return false; Solution::m = board.size(); Solution::n = board[0].size(); vector temp(n,false); vector > flags(m, temp); for( int i=0; i > &board, string & word, vector >& flags, int level,Position p ) { if( level == word.size()-1) return true; if( word[level] != board[p.x][p.y]) return false; for( int i=-1; i<=1; i++) { for( int j=-1; j<=1; j++) { if(i+j!=1 && i+j!=-1) continue; if( p.x+i>=0 && p.y+j>=0 && p.x+i