Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. def match(board, char, row, col)
  2. return false if row < 0 or row >= board.length
  3. return false if col < 0 or col >= board.first.length
  4. return board[row][col] == char
  5. end
  6.  
  7. def _exist(board, word, row, col)
  8. return true if word.empty?
  9. [[row-1, col], [row+1, col], [row, col-1], [row, col+1]].each { |cur_row, cur_col|
  10. if (match(board, word[0], cur_row, cur_col))
  11. letter = board[cur_row][cur_col]
  12. board[cur_row][cur_col] = nil
  13. return true if (_exist(board, word[1..-1], cur_row, cur_col))
  14. board[cur_row][cur_col] = letter
  15. end
  16. }
  17. return false
  18. end
  19.  
  20. def exist(board, word)
  21. return true if word.empty?
  22.  
  23. board.each_index { |row|
  24. board[row].each_index { |col|
  25. if (board[row][col] == word[0])
  26. letter = board[row][col]
  27. board[row][col] = nil
  28. return true if _exist(board, word[1..-1], row, col)
  29. board[row][col] = letter
  30. end
  31. }
  32. }
  33.  
  34. return false
  35. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement