Graphs
108 / 170GraphsLesson 1 of 8About 79 min
Cells are nodes, adjacency is edges, and the visited mark is permanent. That last word is the whole lesson.
Three readings of one traversal, none of which is a plain count. region_of returns the sorted cells of the region containing a given cell. bounding_box returns the smallest rectangle enclosing that region, as [top, left, bottom, right]. regions_touching_border counts how many separate regions of X touch any edge of the board. Cells connect side to side only, not diagonally.
Tests
board = [ ["X", "X", ".", "."], ["X", ".", ".", "X"], [".", ".", "X", "X"],]print(region_of(board, 0, 0))print(bounding_box(board, 0, 0), bounding_box(board, 2, 3))print(region_of(board, 0, 2), bounding_box(board, 0, 2))print(regions_touching_border(board), regions_touching_border([[".", "."]]))Output
Run tests to see output.
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.