bool dfs(char** board, int rSize, int* cSize, char* word, int r, int c, int cnt){ if(r>=rSize || r<0 || c>=*cSize || c<0 || board[r][c]!=word[cnt]) return false; if(cnt==strlen(word)-1) return true; board[r][c]='0'; bool flag = dfs(board,rSize,cSize,word,r-1,c,cnt+1) || dfs(board,rSize,cSize,word,r+1,c,cnt+1) || dfs(board,rSize,cSize,word,r,c-1,cnt+1) || dfs(board,rSize,cSize,word,r,c+1,cnt+1); board[r][c]=word[cnt]; return flag; } bool exist(char** board, int boardSize, int* boardColSize, char* word){ for(int i=0; i<boardSize; i++) for(int j=0; j<*boardColSize; j++) if(dfs(board,boardSize,boardColSize,word,i,j,0)) return true; return false; }