1113. 红与黑

摘要
Title: 1113. 红与黑
Tag: DFS
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

1113. 红与黑

  • 题意

    有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。
    你站在其中一块黑色的瓷砖上,只能向相邻(上下左右四个方向)的黑色瓷砖移动。
    请写一个程序,计算你总共能够到达多少块黑色的瓷砖。

  • 思路

    floyd fill填满一个连通块

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    '''
    Author: NEFU AB-IN
    Date: 2022-03-28 21:46:17
    FilePath: \ACM\Acwing\1113.py
    LastEditTime: 2022-03-28 21:46:17
    '''
    ans = 0


    def dfs(sx, sy):
    global ans
    st[sx][sy] = 1
    ans += 1
    for i in range(4):
    x = sx + dx[i]
    y = sy + dy[i]
    if x >= 0 and x < n and y >= 0 and y < m and st[x][y] == 0 and g[x][
    y] == '.':
    dfs(x, y)


    while True:
    m, n = map(int, input().split())
    if n + m == 0:
    break
    g = []
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    st = [[0] * m for _ in range(n)]
    for i in range(n):
    g.append(list(input()))
    for i in range(n):
    for j in range(m):
    if g[i][j] == '@':
    sx, sy = i, j
    break
    dfs(sx, sy)
    print(ans)
    ans = 0
使用搜索:谷歌必应百度