2023年7月4日 星期二

桃園國中實作 第2題

 # 桃園國中實作 第2題


# 第二題


# 0代表海

# 1代理陸地


# 給一個坐標,若是0,則輸出0

# 若是1,則與他這個陸地相連的土地有幾個


# 輸入

# 4 5(列,行)

# 1 1坐標

# 00000

# 00000

# 00000

# 00000


# 輸出0


# 4 5

# 1 2

# 0 1 1 0 0

# 0 1 1 0 1

# 0 0 1 0 0

# 0 0 0 0  1


# 輸出5

d = '''

4 5

1 2

0 1 1 0 0

0 1 1 0 1

0 0 1 0 0

0 0 0 0  1

'''

d = d.strip().splitlines()

m,n = map(int,d[0].split())

x,y = map(int,d[1].split())


dd = d[2:]

d = []

for i in dd:

    t = [ int(j) for j in i.split() ]

    d.append(t)

if d[x][y]==0: 

    print(d[x][y])

else:

    rangeList= []

    for i in range(m):

        for j in range(n):

            rangeList.append([i,j])


    visitList = []

    t = [x,y]

  

    tryList = [t]

    while len(tryList)>0:

        t = tryList.pop()

        visitList.append(t)


        tryList = []

        for i in [-1,0,1]:

            for j in [-1,0,1]:

                if [x+i,x+j] in rangeList and  not (i !=0 and j!=0) and [x+i,x+j] not in visitList:

                    tryList.append([x+i,x+j])


        for i in tryList:

            p,q = i[0],i[1]

            if d[p][q]!=0:

                d[p][q]=d[p][q]+1


maxList = []

for i in d:

    maxList.append(max(i))

print(max(maxList))

 

# 執行結果

# 5  

沒有留言:

張貼留言