測試時的測資都沒有問題,而且我看沒什麼問題,但是送出怎麼都過不了,懇請大佬幫我看看問題出在哪
測試時的測資都沒有問題,而且我看沒什麼問題,但是送出怎麼都過不了,懇請大佬幫我看看問題出在哪
#startsize = input()size = list(map(int, size.split()))mapsign = []for i in range(size[0]):s = input()s = list(map(int, s.split()))mapsign.append(s)start = [0,0]
#定位1的座標for x in range(size[1]):for y in range(size[0]):if mapsign[y][x] == 1:start[0] = ystart[1] = xbreak#找下一步def nextstep(steps , L = [0 , 0]):choose = []x = [0,0,0,0]y = [0,0,0,0]if L[0] > 0:x[0] = L[1]y[0] = L[0] - 1choose.append(mapsign[y[0]][x[0]]) #下面else:choose.append(-1)if L[1] < size[1] -1:x[1] = L[1] + 1y[1] = L[0]choose.append(mapsign[y[1]][x[1]]) #右邊else:choose.append(-1)
if L[0] < size[0] -1:x[2] = L[1]y[2] = L[0] + 1choose.append(mapsign[y[2]][x[2]]) #上面else:choose.append(-1)if L[1] > 0:x[3] = L[1] - 1y[3] = L[0]choose.append(mapsign[y[3]][x[3]]) #左邊else:choose.append(-1)
for i in range(4):if max(choose) != -1:if choose[i] == min([x for x in choose if x >= 0]):steps += choose[i]mapsign[L[0]][L[1]] = -1L[0] = y[i]L[1] = x[i]return nextstep(steps , L)return steps#執行print(nextstep(1, start))謝謝
兩個地方要修正
1.初始位置:題目有保證是非負整數,不代表最小值一定是1,例如
1 3
3 4 2 =>初始位置應該是[2,0] 但由於程式未偵測到1,初始位置為[0,0]2
2.mapsign[L[0]][L[1]] = -1這行:由於已經選定此格,無論如何都要設成-1,不應該只在有下一步時才變為-1
最後我有一點疑問:理論上在的測資都沒有問題,而且我看沒什麼問題,但是送出怎麼都過不了,懇請大佬幫我看看問題出在哪
#start
size = input()
size = list(map(int, size.split()))
mapsign = []
for i in range(size[0]):
s = input()
s = list(map(int, s.split()))
mapsign.append(s)
start = [0,0]
#定位1的座標
for x in range(size[1]):
for y in range(size[0]):
if mapsign[y][x] == 1:
start[0] = y
start[1] = x
break
#找下一步
def nextstep(steps , L = [0 , 0]):
choose = []
x = [0,0,0,0]
y = [0,0,0,0]
if L[0] > 0:
x[0] = L[1]
y[0] = L[0] - 1
choose.append(mapsign[y[0]][x[0]]) #下面
else:
choose.append(-1)
if L[1] < size[1] -1:
x[1] = L[1] + 1
y[1] = L[0]
choose.append(mapsign[y[1]][x[1]]) #右邊
else:
choose.append(-1)
if L[0] < size[0] -1:
x[2] = L[1]
y[2] = L[0] + 1
choose.append(mapsign[y[2]][x[2]]) #上面
else:
choose.append(-1)
if L[1] > 0:
x[3] = L[1] - 1
y[3] = L[0]
choose.append(mapsign[y[3]][x[3]]) #左邊
else:
choose.append(-1)
for i in range(4):
if max(choose) != -1:
if choose[i] == min([x for x in choose if x >= 0]):
steps += choose[i]
mapsign[L[0]][L[1]] = -1
L[0] = y[i]
L[1] = x[i]
return nextstep(steps , L)
return steps
#執行
print(nextstep(1, start))
謝謝
兩個地方要修正
1.初始位置:題目有保證是非負整數,不代表最小值一定是1,例如
1 3
3 4 2 =>初始位置應該是[2,0] 但由於程式未偵測到1,初始位置為[0,0]
由於初始位置的值不一定是1,所以初始steps要更改
2.mapsign[L[0]][L[1]] = -1這行:由於已經選定此格,無論如何都要設成-1,不應該只在有下一步時才變為-1
參考更正後程式
#start
size = input()
size = list(map(int, size.split()))
mapsign = []
for i in range(size[0]):
s = input()
s = list(map(int, s.split()))
mapsign.append(s)
#正確尋找最小值
start = [0,0]
mins=mapsign[0][0]
for x in range(size[1]):
for y in range(size[0]):
if mapsign[y][x] <mins:
mins=mapsign[y][x]
start[0] = y
start[1] = x
#找下一步
def nextstep(steps , L = [0 , 0]):
choose = []
#global mapsign
mapsign[L[0]][L[1]] = -1#無論如何都要設成-1
x = [0,0,0,0]
y = [0,0,0,0]
if L[0] > 0:
x[0] = L[1]
y[0] = L[0] - 1
choose.append(mapsign[y[0]][x[0]]) #下面
else:
choose.append(-1)
if L[1] < size[1] -1:
x[1] = L[1] + 1
y[1] = L[0]
choose.append(mapsign[y[1]][x[1]]) #右邊
else:
choose.append(-1)
if L[0] < size[0] -1:
x[2] = L[1]
y[2] = L[0] + 1
choose.append(mapsign[y[2]][x[2]]) #上面
else:
choose.append(-1)
if L[1] > 0:
x[3] = L[1] - 1
y[3] = L[0]
choose.append(mapsign[y[3]][x[3]]) #左邊
else:
choose.append(-1)
for i in range(4):
if max(choose) != -1:
if choose[i] == min([x for x in choose if x >= 0]):
steps += choose[i]
L[0] = y[i]
L[1] = x[i]
return nextstep(steps , L)
return steps
#執行
print(nextstep(mins, start))
最後我有一點疑問:理論上在函式內修改變數要global才會全部修改,但這題依照上面的方法修改後就不用global也AC,加了global也可以,是我的理解有誤嗎?