This page is still under construction.

Parts of this page are still being built. What you see may change.

Wet Tiles

Time limit15sMemory limit256 MB

Summary
Count the grid tiles reached by water spreading each minute from leaks to edge neighbors without crossing wall tiles within T minutes.
Level

Medium5 of 10

Topics
BFS, Matrix, Simulation
Solved
No attempts yet

Problem

Alice runs a construction company in Norainia, a town known for its unusually dry weather. It rains only a few days a year there, so many residents put off roof repairs until a leak appears and ruins the floor. Norainia floor tiles look beautiful and hold up badly against water. Once a tile gets wet it is ruined and has to be replaced. Every rainy day brings Alice a flood of calls, and for each house she needs to know how many replacement tiles the crew should bring.

The floor of a house is a grid of square tiles, XX tiles wide and YY tiles tall. Water drips from one or more known leak positions above particular tiles. After the first minute, the tile directly under each leak is wet. After every following minute, every dry tile that shares an edge with an already wet tile becomes wet.

Walls stop the water. The house always has four outer walls around the whole grid, so water never leaves it. A house may also have inner walls. Each inner wall covers a connected straight run of tiles and is either axis aligned or set at 45 degrees to both axes. A diagonal wall is a run of tiles that touch only at their corners. Water never enters a tile that holds a wall, and it moves only between tiles that share an edge. Walls may cross each other, and no leak sits over a wall.

Figure 1 shows in gray the damage from three leaks, each marked with a white letter L, over the first five minutes. A tile labeled 2 becomes wet during the second minute, a tile labeled 3 during the third minute, and so on. Black tiles are inner walls that restrict the flow of water. After 5 minutes, 75 tiles are wet.

Figure 1

Figure 1. 75 wet tiles

Given the grid dimensions, the leaks, the inner walls, and the number of minutes TT that pass before the crew stops the leaks, count the wet tiles.

Figures 2 through 4 show the second, third, and fourth houses of the first example.

Figure 2

Figure 2. 17 wet tiles

Figure 3

Figure 3. 4 wet tiles

Figure 4

Figure 4. 94 wet tiles

Input

The input describes one or more houses.

Each house begins with a line holding five integers XX, YY, TT, LL, WW. XX and YY are the grid dimensions, with 1≤X≤10001 \le X \le 1000 and 1≤Y≤10001 \le Y \le 1000. Coordinates are one indexed, so a tile is written as a pair (x,y)(x, y) with 1≤x≤X1 \le x \le X and 1≤y≤Y1 \le y \le Y. TT is the number of minutes that pass before the crew arrives and stops the leaks, with 1≤T≤2000001 \le T \le 200000. LL is the number of leaks, with 1≤L≤1001 \le L \le 100. WW is the number of inner walls, with 0≤W≤1000 \le W \le 100.

The next 2L2L integers, spread over one or more lines, are the LL distinct leak positions (x,y)(x, y).

If W>0W > 0, the next 4W4W integers, spread over one or more lines, describe the inner walls. Each wall is given by four integers x1x_1, y1y_1, x2x_2, y2y_2, the coordinates of its two ends. The wall covers every tile on the straight run from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2). That run is either axis aligned or set at 45 degrees. If the two ends are the same, the wall covers that single tile.

A line holding the single integer -1 ends the input.

Output

For each house, print the number of tiles that are wet after TT minutes, one per line, in the order the houses appear in the input.

Examples1

  1. Example 1

    Input
    12 12 5 3 5
    2 11 3 3 9 5
    1 9 6 9 1 7 4 4 7 1 7 4
    10 9 10 12 11 4 12 4
    9 7 8 1 3
    4 3
    2 2 6 6 6 2 2 6 8 2 8 2
    6 7 50 1 3
    3 4
    2 2 2 6 3 6 5 4 5 4 3 2
    12 12 5 3 0
    2 11 3 3 9 5
    -1
    
    Expected output
    75
    17
    4
    94