图片由网上提供仅供参考:
方法一:
import sys
def core(arr,x,dp):
for i in range(n):
if i != x and dp[i] and arr[x][i] >= 3:
dp[i] = False
core(arr,i,dp)
n = int(sys.stdin.readline().strip())
arr = []
for i in range(n):
arr.append(list(map(int,sys.stdin.readline().strip().split(" "))))
count = 0
dp = [True] * n
for i in range(n):
if dp[i]:
count += 1
dp[i] = False
core(arr,i,dp)
print(count)
方法二:
import sys N = int(sys.stdin.readline().strip()) arr=[] for i in range(N): arr.append(list(map(int,(sys.stdin.readline().strip().split())))) temp=0 for i in range(len(arr)): for j in range(N): if arr[i][j]==arr[j][i] and i!=j : temp+=1 temp=int(temp/2) if arr[i]==[0]*N: temp += 1 print(temp) print(temp)