蓝桥杯刷题--python-27--全球变暖-dfs-bfs

1.全球变暖 - 蓝桥云课 (lanqiao.cn)

import os

import sys

# 请在此输入您的代码

sys.setrecursionlimit(60000)

n = int(input())

dao = []

for _ in range(n):

    tmp = list(input())

    dao.append(tmp)

dict = [(1, 0), (0, 1), (-1, 0), (0, -1)]

used = [[0 for _ in range(n)] for _ in range(n)]


 

def dfs(x, y):

    global flag

    # used[x][y] = 1

    if dao[x][y - 1] == "#" and dao[x][y + 1] == "#" and dao[x + 1][y] == "#" and dao[x - 1][y] == "#":

        flag = 1

    used[x][y] = 1

    for x1, y1 in dict:

        a = x + x1

        b = y + y1

        if not used[a][b] and dao[a][b]=="#":

            dfs(a, b)


 

cnt = 0

for i in range(n):

    for j in range(n):

        if dao[i][j] == "#" and not used[i][j]:

            flag = 0

            dfs(i, j)

            if not flag:

                cnt += 1

print(cnt)


 

 

from collections import deque

n = int(input())

dao = []
for _ in range(n):
    tmp = list(input())
    dao.append(tmp)
dict = [(1, 0), (0, 1), (-1, 0), (0, -1)]
used = [[0 for _ in range(n)] for _ in range(n)]


def bfs(i, j):
    global flag
    dq = deque()
    dq.append(i)
    dq.append(j)
    used[i][j] = 1

    while dq:
        x = dq.popleft()
        y = dq.popleft()

        if dao[x + 1][y] == "#" and dao[x - 1][y] == "#" and dao[x][y + 1] == "#" and dao[x][y - 1] == "#":
            flag = 1
        for x1, y1 in dict:
            if not used[x1 + x][y1 + y] and dao[x1 + x][y1 + y] == "#":
                used[x1 + x][y1 + y] = 1
                dq.append(x1 + x)
                dq.append(y1 + y)


cnt = 0
for i in range(n):
    for j in range(n):
        if not used[i][j] and dao[i][j] == '#':
            flag = 0
            bfs(i, j)
            if not flag:
                cnt += 1
print(cnt)

相关推荐

  1. --python-27--全球-dfs-bfs

    2024-03-23 22:48:02       40 阅读
  2. --python-22-dfs-bfs

    2024-03-23 22:48:02       43 阅读
  3. 深度优先搜索-[178]全球(C++)

    2024-03-23 22:48:02       31 阅读
  4. B组C++省赛 全球bfs

    2024-03-23 22:48:02       40 阅读
  5. 全球dfsbfs

    2024-03-23 22:48:02       40 阅读
  6. 每日一】4.2 全球

    2024-03-23 22:48:02       36 阅读
  7. --python-21

    2024-03-23 22:48:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 22:48:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 22:48:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 22:48:02       82 阅读
  4. Python语言-面向对象

    2024-03-23 22:48:02       91 阅读

热门阅读

  1. Zookeeper详解(zk)

    2024-03-23 22:48:02       35 阅读
  2. AndroidStudio开发 相关依赖

    2024-03-23 22:48:02       44 阅读
  3. linux修改ftp上传路径

    2024-03-23 22:48:02       37 阅读
  4. NSURLSessionConfiguration

    2024-03-23 22:48:02       40 阅读
  5. centos7 安装php82

    2024-03-23 22:48:02       41 阅读
  6. 编写人脸检测程序

    2024-03-23 22:48:02       43 阅读
  7. Vue3:编程式路由导航

    2024-03-23 22:48:02       50 阅读