Skip to content

02226: Muddy Fields

http://cs101.openjudge.cn/practice/02226/

Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat.

To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field.

Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other.

Compute the minimum number of boards FJ requires to cover all the mud in the field.

输入

* Line 1: Two space-separated integers: R and C

* Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

输出

* Line 1: A single integer representing the number of boards FJ needs.

样例输入

4 4
*.*.
.***
***.
..*.

样例输出

4

提示

OUTPUT DETAILS:

Boards 1, 2, 3 and 4 are placed as follows: 1.2. .333 444. ..2. Board 2 overlaps boards 3 and 4.

来源

USACO 2005 January Gold

A well‐known approach is to first label each maximal horizontal contiguous segment and each maximal vertical contiguous segment, build a bipartite graph between these segments (with an edge if a muddy cell belongs to both), and then compute the maximum matching (which, by Kőnig’s theorem, equals the minimum vertex cover – i.e. the minimum boards needed).

python
def min_boards(R, C, field):
    # Label horizontal segments.
    hor = [[0] * C for _ in range(R)]
    hor_id = 0
    for r in range(R):
        c = 0
        while c < C:
            if field[r][c] == '*':
                hor_id += 1
                # label contiguous '*' segment in row r
                while c < C and field[r][c] == '*':
                    hor[r][c] = hor_id
                    c += 1
            else:
                c += 1

    # Label vertical segments.
    ver = [[0] * C for _ in range(R)]
    ver_id = 0
    for c in range(C):
        r = 0
        while r < R:
            if field[r][c] == '*':
                ver_id += 1
                # label contiguous '*' segment in column c
                while r < R and field[r][c] == '*':
                    ver[r][c] = ver_id
                    r += 1
            else:
                r += 1

    # Build bipartite graph: for each horizontal segment, list all vertical segments that intersect it.
    graph = {i: set() for i in range(1, hor_id + 1)}
    for r in range(R):
        for c in range(C):
            if field[r][c] == '*':
                h = hor[r][c]
                v = ver[r][c]
                graph[h].add(v)

    # Use DFS to find an augmenting path in the bipartite graph.
    match = {}  # maps vertical segment -> horizontal segment

    def dfs(u, seen):
        for v in graph[u]:
            if v in seen:
                continue
            seen.add(v)
            if v not in match or dfs(match[v], seen):
                match[v] = u
                return True
        return False

    result = 0
    for u in range(1, hor_id + 1):
        if dfs(u, set()):
            result += 1
    return result

if __name__ == "__main__":
    import sys
    data = sys.stdin.read().strip().split()
    if not data:
        exit(0)
    R = int(data[0])
    C = int(data[1])
    field = data[2:]
    print(min_boards(R, C, field))

Explanation

  1. Segment Labeling:
    • Horizontal Segments: For each row, we scan left-to-right. When we hit a '*', we label all consecutive '*'cells as one horizontal segment.
    • Vertical Segments: For each column, we scan top-to-bottom. When we hit a '*', we label all consecutive '*' cells as one vertical segment.
  2. Graph Construction:
    • For every cell with mud ('*'), we know its horizontal segment id (from hor) and its vertical segment id (from ver). We then add an edge from the horizontal segment to the vertical segment.
  3. Maximum Bipartite Matching:
    • We use a DFS-based augmenting path algorithm to compute the maximum matching. By Kőnig’s theorem, this matching size is equal to the minimum number of boards required.
python
#23n2300011335
r,c = map(int,input().split())
a,b = 0,0
matrix = [[False for _ in range(1000)] for _ in range(1000)]
match = [0 for _ in range(1000)]
maze = [['' for _ in range(1000)] for _ in range(1000)]
h,s = [[0 for _ in range(1000)] for _ in range(1000)],[[0 for _ in range(1000)] for _ in range(1000)]
for i in range(1,r+1):
    maze[i][1:] = list(input())
    for j in range(1,c+1):
        if maze[i][j] == '*':
            if j == 1 or maze[i][j-1] != '*':
                a += 1
                h[i][j] = a
            else:
                h[i][j] = a
for j in range(1,c+1):
    for i in range(1,r+1):
        if maze[i][j] == '*':
            if i == 1 or maze[i-1][j] != '*':
                b += 1
                s[i][j] = b
            else:
                s[i][j] = b
            matrix[h[i][j]][s[i][j]] = True
visited = []
def dfs(x):
    for i in range(1,b+1):
        if matrix[x][i] and not visited[i]:
            visited[i] = True
            if not match[i] or dfs(match[i]):
                match[i] = x
                return 1
    return 0
ans = 0
for i in range(1,a+1):
    visited = [False for _ in range(1000)]
    ans += dfs(i)
print(ans)