Skip to content

Instantly share code, notes, and snippets.

@Allan-Gong
Created December 21, 2025 19:38
Show Gist options
  • Select an option

  • Save Allan-Gong/6ee0e9235f6a6c9790679a9704553f7d to your computer and use it in GitHub Desktop.

Select an option

Save Allan-Gong/6ee0e9235f6a6c9790679a9704553f7d to your computer and use it in GitHub Desktop.
BFS template
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