#37325: python 紀錄


BensonDC (python戰士)

學校 : 不指定學校
編號 : 240921
來源 : [1.175.217.87]
最後登入時間 :
2024-03-27 12:33:26
g004. 社區熱門度 (Popularity) -- TOI練習賽202105新手組第1題 | From: [36.238.104.63] | 發表日期 : 2023-09-01 23:09

R,C=map(int,input().split())
L=[]
for _ in range(R):
    L.append([int(x) for x in input().split()])
Ln=[]
for i in range(R):
    Ln.append(L[i].copy())
for i in range(R):
    for j in range(C):
        if L[i][j]==0:
            sume=0
            b=4
            try:
                if L[i-1][j]!=0 and i-1>=0:sume+=L[i-1][j]
                else:b-=1 
            except:b-=1
            try:
                if L[i+1][j]!=0:sume+=L[i+1][j]
                else:b-=1 
            except:b-=1
            try:
                if L[i][j-1]!=0 and j-1>=0:sume+=L[i][j-1]
                else:b-=1 
            except:b-=1
            try:
                if L[i][j+1]!=0:sume+=L[i][j+1]
                else:b-=1 
            except:b-=1
            if b!=0:
                Ln[i][j]=sume//b
for i in Ln:
    print(*i)

 
#37327: Re: python 紀錄


BensonDC (python戰士)

學校 : 不指定學校
編號 : 240921
來源 : [1.175.217.87]
最後登入時間 :
2024-03-27 12:33:26
g004. 社區熱門度 (Popularity) -- TOI練習賽202105新手組第1題 | From: [36.238.104.63] | 發表日期 : 2023-09-01 23:20

R,C=map(int,input().split())
L=[]
for _ in range(R):
    L.append([int(x) for x in input().split()])
Ln=[]
for i in range(R):
    Ln.append(L[i].copy())
for i in range(R):
    for j in range(C):
        if L[i][j]==0:
            sume=0
            b=4
            try:
                if L[i-1][j]!=0 and i-1>=0:sume+=L[i-1][j]
                else:b-=1 
            except:b-=1
            try:
                if L[i+1][j]!=0:sume+=L[i+1][j]
                else:b-=1 
            except:b-=1
            try:
                if L[i][j-1]!=0 and j-1>=0:sume+=L[i][j-1]
                else:b-=1 
            except:b-=1
            try:
                if L[i][j+1]!=0:sume+=L[i][j+1]
                else:b-=1 
            except:b-=1
            if b!=0:
                Ln[i][j]=sume//b
for i in Ln:
    print(*i)

 

R, C=map(int,input().split())
L=[]
for _ in range(R):
    L.append([int(x) for x in input().split()])

Ln = [row.copy() for row in L]

directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 上,下,左,右

for i in range(R):
    for j in range(C):
        if L[i][j]==0:
            sume, count = 0, 0
            for dx, dy in directions:
                nx, ny = i + dx, j + dy
                if 0 <= nx < R and 0 <= ny < C and L[nx][ny] != 0:
                    sume += L[nx][ny]
                    count += 1
            if count > 0:
                Ln[i][j] = sume // count

for row in Ln:
    print(*row)

 
ZeroJudge Forum