defsolution(park,routes):answer=[]two_dim_park_arr=[]two_dim_routes_arr=[]forpinpark:two_dim_park_arr.append(list(p))answer=[[i,j]foriinrange(len(two_dim_park_arr))forjinrange(len(two_dim_park_arr[0]))iftwo_dim_park_arr[i][j]=='S'][0]print('초기위치:',answer)print('=======================')forrinroutes:direction,num=r.split(' ')two_dim_routes_arr.append([direction,int(num)])fordirection,numintwo_dim_routes_arr:print('시작위치:',answer)print('이동 지시:',direction,num)result=[]success=Truei,j=answerwhilesuccessandnum>0:num-=1if(direction=='N'):i-=1elif(direction=='S'):i+=1elif(direction=='E'):j+=1elif(direction=='W'):j-=1if(i<0orj<0):print('공원을 벗어남:',i,j)success=Falsebreak;try:iftwo_dim_park_arr[i][j]=='X':print('장애물:',i,j)success=Falsebreakelse:print('이동 가능',i,j)result=[i,j]except:print('존재하지 않는 구역:',i,j)success=Falsebreakprint('이동 가능 여부:',success)if(success):print('이동 성공:',result)answer=resultprint('=======================')print('결과',answer)returnanswer
다른 풀이
클래스를 사용
공원을 x,y 좌표를 나타내는 이차원 배열로 만들 때 for문을 사용하지 않고 간단하게 만듬