Find the largest flow from the south edge to the north edge of a rectangular grid where rectangle buildings block cells.
Medium7GraphMatrixNo attempts yetTime limit5sMemory limit512 MBAliens have landed. Their home planet has no flowing water at all, so they find the rivers of Earth interesting and now want to put up their buildings inside some of them. Your job is to make sure the buildings do not block a river too badly, because that would cause serious trouble. Given the placement of the buildings, find the maximum flow the river can sustain.
The aliens build only on stretches of river that are straight and of uniform width, so you model a river as a rectangular grid. Each cell has integer coordinates (X,Y) with 0≤X<W and 0≤Y<H. Each cell sustains a flow of 1 unit through it, and water moves only between two cells that share an edge. Every cell on the south side of the river, that is every cell whose y coordinate is 0, receives an incoming flow of 1 from outside. Every building is a grid aligned rectangle, and a cell covered by a building sustains no flow at all. Under these rules, find the largest total flow that can reach the cells on the north side of the river, that is the cells whose y coordinate is H−1.
The first line contains the number of test cases T. T test cases follow.
The first line of each test case contains three integers: the width of the river W, the height of the river H, and the number of buildings placed in the river B. Each of the next B lines contains four integers X0, Y0, X1, Y1. Here (X0,Y0) is the lower left cell of the building and (X1,Y1) is its upper right cell. Buildings never overlap, but two buildings may share an edge.
Limits
For each test case, print one line in the form Case #x: m, where x is the test case number starting from 1 and m is the maximum flow that can pass through the river.
The two pictures below draw the two test cases of the example input.
