问题描述
此代码,if count >= 3 : print("test")
成功但 return True
不工作.. 为什么..?!?
def dfs(a,b,i,count):
if a<=-1 or a>=7 or b<=-1 or b>=7:
return False
if lists[a][b] == i and lists[a][b] > 0:
lists[a][b] = -1 #확인 했음을 표시
count += 1;
dfs(a+1,count)
dfs(a-1,count)
dfs(a,b+1,b-1,count)
if count >= 3 :
print("test") #THIS PRINT SUCCESS!!
return True #BUT DON'T return True!!
else:
return False
return False
lists = []
for i in range(7):
lists.append(list(map(int,input().split(" "))))
result = 0
for a in range(0,7):
for b in range(0,7):
if dfs(a,lists[a][b],0) == True:
#Never run.. this codes..
print("result up?")
result += 1
print( result )
输入数据是
2 1 5 1 1 3 4
2 1 5 1 3 5 3
2 3 4 5 2 2 4
4 4 3 2 3 1 3
4 3 5 3 1 4 3
5 4 4 3 3 5 5
2 1 3 5 1 1 2
请帮帮我ㅠㅠ..
解决方法
问题出在
dfs(a+1,b,i,count)
dfs(a-1,count)
dfs(a,b+1,b-1,count)
如果你改成
print(dfs(a+1,count))
print(dfs(a-1,count))
print(dfs(a,count))
突然出现“missing”返回true。这里的问题是你没有对实际返回的“True”做任何事情。所以你需要对这个返回值做一些事情,我不确定你在尝试什么,但我希望它能回答关于发生了什么的问题
部分输出
False
False
False
False
test
True
False
False