Created
December 21, 2025 19:38
-
-
Save Allan-Gong/6ee0e9235f6a6c9790679a9704553f7d to your computer and use it in GitHub Desktop.
BFS template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BFS | |
| visited = {} | |
| // initialize | |
| queue = [a] | |
| current_size = 0 | |
| offsets = [[0,1],[0,-1],[1,0],[-1,0]] | |
| while(queue is not empty) { | |
| curren = queup.tail() | |
| visited[] = a; | |
| current_size++; | |
| for (offset : offsets) {// avoid some edge cases ? | |
| nei = curren + offset; | |
| if (inBound(nei) && !visited(nei) && isLand(nei)) { | |
| queue.push(nei); | |
| } | |
| } | |
| } | |
| return current_size; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment