1. 全球变暖-178
    python标准库-queue
from queue import Queue

n = int(input())
ph = []
for i in range(n):
    ph.append(input())
book = [[0]*n for _ in range(n)]
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y):
    global flag
    q = Queue()
    q.put((x, y))
    book[x][y] = 1
    while not q.empty():
        t = q.get()
        tx, ty = t[0], t[1]
        # 不可以写成tx, ty = q.get()[0], q.get()[1](执行了两次q.get()操作)
        if ph[tx+1][ty] == '#' and ph[tx-1][ty] == '#' and ph[tx][ty+1] == '#' and ph[tx][ty-1] == '#':
            flag = 1
        for i in range(4):
            nx = tx + drct[i][0]
            ny = ty + drct[i][1]
            if book[nx][ny] == 0 and ph[nx][ny] == '#':
                q.put((nx, ny))
                book[nx][ny] = 1

ans = 0
for i in range(n):
    for j in range(n):
        if book[i][j] == 0 and ph[i][j] == '#':
            flag = 0
            bfs(i, j)
            if flag == 0:
                ans += 1
print(ans)

在此题基础上改一改:有多少个独立小岛?(淹没之前)

from queue import Queue
n = int(input())
ph = []
for i in range(n):
    ph.append(input())
book = [[0]*n for _ in range(n)]
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y):
    q = Queue()
    q.put((x, y))
    while not q.empty():
        t = q.get()

        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]
            if book[nx][ny] == 0 and ph[nx][ny] == '#':
                q.put((nx, ny))
                book[nx][ny] = 1

ans = 0
for i in range(n):
    for j in range(n):
        if book[i][j] == 0 and ph[i][j] == '#':
            bfs(i, j)
            ans += 1
print(ans)

2.迷宫-602

from queue import Queue
n, m = 30, 50 #30行50列
maze = []
for i in range(n):
    maze.append(input())
book = [[0]*m for i in range(n)]
drct = [[1, 0], [0, -1], [0, 1], [-1, 0]]
drct_s = 'DLRU'

def bfs(x, y, z, w): #x, y, step, path
    q = Queue()
    if maze[x][y] != '1' and book[x][y] == 0:
        q.put((x, y, z, w))
        book[x][y] = 1
    while not q.empty():
        t = q.get()
        if t[0] == n-1 and t[1] == m-1:
            print(t[3])
            break
        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if book[nx][ny] == 1:
                continue
            if maze[nx][ny] == '1':
                continue

            nstep = t[2] + 1
            npath = t[3] + drct_s[i]

            q.put((nx, ny, nstep, npath))
            book[nx][ny] = 1

bfs(0, 0, 0, '')
  1. 穿越雷区-141
from queue import Queue
n = int(input())
mp = []
for i in range(n):
    a = input().split()
    mp.append(a)
    if 'A' in a:
        aa = [i, a.index('A')]
    if 'B' in a:
        bb = [i, a.index('B')]    
book = [[0]*n for _ in range(n)]
drct = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def bfs(x, y, z):  # x, y, step
    q = Queue()
    q.put((x, y, z))
    book[x][y] = 1
    while not q.empty():
        t = q.get()
        if t[0] == bb[0] and t[1] == bb[1]:
            print(t[2])
            break
        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= n:
                continue
            if book[nx][ny] == 1:
                continue
            if mp[nx][ny] == mp[t[0]][t[1]]:
                continue

            nstep = t[2] + 1
            q.put((nx, ny, nstep))
            book[nx][ny] = 1

bfs(aa[0], aa[1], 0)   
  1. 灌溉-551
    感觉这题用bfs麻烦了些,应该有更好的方法。
from queue import Queue
n, m = map(int, input().split()) # n 行 m 列
t = int(input()) # 出水管数量
pos = [] # 出水管位置
book = [[0]*m for _ in range(n)]
for i in range(t):
    a, b = map(int, input().split())
    book[a-1][b-1] = 1
    l = [a-1, b-1]
    pos.append(l)
k = int(input()) # k分钟后,有多少个方格被灌溉好
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y, minu):
    q = Queue()
    q.put((x, y, minu))
    book[x][y] = 1
    while not q.empty():
        t = q.get()
        if t[2] == k:
            break
        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if book[nx][ny] == 1:
                continue
            q.put((nx, ny, minu+1))
            book[nx][ny] = 1

for i in pos:
    bfs(i[0], i[1], 0)

ans = 0
for i in range(n):
    for j in range(m):
        if book[i][j] == 1:
            ans += 1

print(ans)
  1. 宝岛探险
from queue import Queue

n, m, sx, sy = map(int, input().split())  #n行m列, startx, starty
sx -= 1
sy -= 1
mp = []
for i in range(n):
    a = list(map(int, input().split()))
    assert len(a) == m
    mp.append(a)
assert len(mp) == n
book = [[0]*m for _ in range(n)]
drct = [[0, 1], [0, -1], [1, 0], [-1, 0]]

def bfs(x, y):
    q = Queue()
    q.put((x, y))
    book[x][y] = 1
    while not q.empty():
        t = q.get()

        for i in range(4):
            nx = t[0] + drct[i][0]
            ny = t[1] + drct[i][1]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if book[nx][ny] == 1:
                continue
            if mp[nx][ny] == 0:
                continue

            q.put((nx, ny))
            book[nx][ny] = 1

bfs(sx, sy)

ans = 0
for i in range(n):
    for j in range(m):
        if book[i][j] == 1:
            ans += 1
print(ans)
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐