#b900
w,h=map(int,input().split())
p=[[0]*(w*2+1) for q in range(h)]#建立二維陣列
for i in range(h):
k=list(str(input()))#把每個字串轉成str,再分開
for j in range(1,2*w):
p[i][j]=k[j-1]#存入字串
now=p[0][1]#現在的位置
t=1#從左邊數過來第幾個
for q in range(h):#從上面數下來第幾個
now=p[q][t]
if p[q][t-1]=="-":#左邊是不是"-"
now = p[q][t-2]
t-=2
elif p[q][t+1]=="-":#右邊是不是"-"
now = p[q][t+2]
t+=2
print(t)
我的解題想法是把第一行的 |-|.| 儲存成[" 0 " , " | " , " - " , " | " , " . " , " | " , " 0 "]
上半部的巢狀for迴圈確認過了沒有問題
我認為問題是在下半部,因為在家自學的關係,資源只有網路,可能有些觀念是我沒有注意到的,再加上我沒有做二維陣列的經驗,我需要幫助
W,H=map(int,input().split()) #寬度跟高度map=[]walk=[]'''==============<走訪記錄>================='''def go_check():global walkdel walk[:]for y in range(H):walk.append([])for x in range(2*W-1):walk[y].append(map[y][x])'''================<輸入>================='''for y in range(H):map.append([])map[y]=list(str(input()))'''================<探路>================='''for x in range(0,2*W-1,2):go_x=xgo_y=0go_check()while(go_y<=H):if (go_y == H):if (go_x > 0):print(go_x//2+1,end=' ')breakelse:print(go_x+1,end=' ')breakwalk[go_y][go_x] = 'X' #走過了#往左走if (go_x > 0 and map[go_y][go_x - 1] == '-' and walk[go_y][go_x - 1] != 'X'):walk[go_y][go_x - 1] = 'X'go_x-=2go_y+=1# 往右走elif (go_x < 2 * W - 2 and map[go_y][go_x + 1] == '-' and walk[go_y][go_x + 1] != 'X'):walk[go_y][go_x + 1] = 'X'go_x+=2go_y+=1# 往下走else:go_y+=1
用走訪的概念寫的,你可以參考看看
| AC (22ms, 3.4MB)結果 好久沒寫python www |
W,H=map(int,input().split()) #寬度跟高度map=[]'''================<輸入>================='''for y in range(H):map.append([])map[y]=list(str(input()))'''================<探路>================='''for x in range(0,2*W-1,2):go_x=xgo_y=0while(go_y<=H):if (go_y == H):if (go_x > 0):print(go_x//2+1,end=' ')breakelse:print(go_x+1,end=' ')break#往左走if (go_x > 0 and map[go_y][go_x - 1] == '-'):go_x-=2go_y+=1# 往右走elif (go_x < 2 * W - 2 and map[go_y][go_x + 1] == '-' ):go_x+=2go_y+=1# 往下走else:go_y+=1
這樣寫就好,walk=[]測試而已
W,H=map(int,input().split()) #寬度跟高度map=[]'''================<輸入>================='''for y in range(H):map.append([])map[y]=list(str(input()))'''================<探路>================='''for x in range(0,2*W-1,2):go_x=xgo_y=0while(go_y<=H):if (go_y == H):if (go_x > 0):print(go_x//2+1,end=' ')breakelse:print(go_x+1,end=' ')break#往左走if (go_x > 0 and map[go_y][go_x - 1] == '-'):go_x-=2go_y+=1# 往右走elif (go_x < 2 * W - 2 and map[go_y][go_x + 1] == '-' ):go_x+=2go_y+=1# 往下走else:go_y+=1這樣寫就好,walk=[]測試而已
厲害...
W,H=map(int,input().split()) #寬度跟高度map=[]'''================<輸入>================='''for y in range(H):map.append([])map[y]=list(str(input()))'''================<探路>================='''for x in range(0,2*W-1,2):go_x=xgo_y=0while(go_y<=H):if (go_y == H):if (go_x > 0):print(go_x//2+1,end=' ')breakelse:print(go_x+1,end=' ')break#往左走if (go_x > 0 and map[go_y][go_x - 1] == '-'):go_x-=2go_y+=1# 往右走elif (go_x < 2 * W - 2 and map[go_y][go_x + 1] == '-' ):go_x+=2go_y+=1# 往下走else:go_y+=1這樣寫就好,walk=[]測試而已
厲害...
那麼久沒寫python了,但還是AC了,厲害的人用什麼語言依舊厲害
W,H=map(int,input().split()) #寬度跟高度map=[]'''================<輸入>================='''for y in range(H):map.append([])map[y]=list(str(input()))'''================<探路>================='''for x in range(0,2*W-1,2):go_x=xgo_y=0while(go_y<=H):if (go_y == H):if (go_x > 0):print(go_x//2+1,end=' ')breakelse:print(go_x+1,end=' ')break#往左走if (go_x > 0 and map[go_y][go_x - 1] == '-'):go_x-=2go_y+=1# 往右走elif (go_x < 2 * W - 2 and map[go_y][go_x + 1] == '-' ):go_x+=2go_y+=1# 往下走else:go_y+=1這樣寫就好,walk=[]測試而已
厲害...
那麼久沒寫python了,但還是AC了,厲害的人用什麼語言依舊厲害
我的程度沒那麼高,有些地方沒看懂@@
(go_x > 0 and map[go_y][go_x - 1] == '-'):
這是其中一句,我之後慢慢消化
假設一開始是(x,y) =(0,0) 當作走1
就會是
(0,0) - > (2,1) -> (0,2) -> (0,3) 輸出@1@
一開始是(2,0)當作走2
(2,0) -> (0,1) -> (2,2) -> (4,3) 輸出@3@
一開始是(4,0) 當作走3
(4,0) -> (4,1) - > (4,2) -> (2,3) 輸出@2@
假設一開始是(x,y) =(0,0) 當作走1
就會是
(0,0) - > (2,1) -> (0,2) -> (0,3) 輸出@1@
一開始是(2,0)當作走2
(2,0) -> (0,1) -> (2,2) -> (4,3) 輸出@3@
一開始是(4,0) 當作走3
(4,0) -> (4,1) - > (4,2) -> (2,3) 輸出@2@
請問我的二維陣列得到的結果和你的一樣嗎?
w,h=map(int,input().split())
p=[[0]*(w*2+1) for q in range(h)]#建立二維陣列
for i in range(h):
k=list(str(input()))#把每個字串轉成str,再分開
for j in range(1,2*w):
p[i][j]=k[j-1]#存入字串
不一樣,你print(p)就可以得到陣列
我的印出來會是
[['|', '-', '|', '.', '|'], ['|', '-', '|', '.', '|'], ['|', '.', '|', '-', '|']]
我的想法會是這樣(二維)
(x,y)
(1,1)='-'
(1,2)='.'
(3,2)='-'[ ['|', '-', '|', '.', '|'],
['|', '-', '|', '.', '|'],
['|', '.', '|', '-', '|'] ]
[somthing]一維
[[thing],[thing]]二維
解惑了,謝謝