Oil Deposits ZOJ - 1709

https://zoj.pintia.cn/problem-sets/91827364500/problems/91827365208

第一次用Python来A算法题

习惯问题 和用C写的大差不差的样子。。

 

import numpy as np
import queue

def bfs(e,book,n,m,x,y):
    next=[[0,-1],[-1,0],[0,1],[1,0],[-1,-1],[-1,1],[1,1],[1,-1]]

    que=queue.Queue()
    tx,ty=x,y
    tmp=(tx,ty)
    que.put(tmp)
    book[tx][ty]=1
    while que.empty()==False:
        cur=que.get()
        for i in range(8):
            tx,ty=cur[0]+next[i][0],cur[1]+next[i][1]
            if 0<=tx and tx<n and 0<=ty and ty<m and e[tx][ty]=='@' and book[tx][ty]==0:
                que.put((tx,ty))
                book[tx][ty]=1


def solve(n,m,e):
    book=np.zeros((n,m))
    ans=int(0)
    for i in range(n):
        for j in range(m):
            if e[i][j]=='@' and book[i][j]==0:
                bfs(e,book,n,m,i,j)
                ans=ans+1
    return ans

while 1:
    n,m=map(int,input().split())
    if n==0:
        break;
    e=[]
    for i in range(n):
        str=input()
        tmp=[]
        for j in range(m):
            tmp.append(str[j])
        e.append(tmp)
    print(solve(n,m,e))


"""
3 5
*@*@*
**@**
*@*@*

m=input()
for i in m.split():
    print(i)

"""

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值