base implementation, not optimized.
add explored set to improve efficiency.