한 번 움직일 때마다 이유를 생각해 보세요
벽을 피해 출발점에서 도착점까지 이동해 보세요. 가장 짧은 길을 생각하고, 이동을 되돌리며 다른 경로도 살펴볼 수 있습니다. 시간 제한은 없어요. 정답을 확인한 뒤 다음 문제로 넘어가며, 어려우면 다시 해볼 수 있습니다.
기록은 이 브라우저에 남아요
정답을 맞힌 문제까지 이어서 할 수 있어요. 마지막 문제를 마치면 10문제 완료 기록을 남깁니다. 재도전을 포함한 연습 결과이며, 선택한 숫자나 이동 경로는 저장하지 않아요.
const data = {"values": [], "grid": ["..", ".."]};
function solve(d) {
const grid=d.grid, width=grid[0].length;
const queue=[[0,0,0]], seen=new Set(["0,0"]);
while(queue.length){
const [x,y,distance]=queue.shift();
if(x===width-1&&y===grid.length-1)return distance;
for(const [dx,dy] of [[1,0],[0,1],[-1,0],[0,-1]]){
const nx=x+dx,ny=y+dy,key=`${nx},${ny}`;
if(nx>=0&&ny>=0&&nx<width&&ny<grid.length&&grid[ny][nx]!=="#"&&!seen.has(key)){
seen.add(key);queue.push([nx,ny,distance+1]);
}
}
}
return -1;
}
console.log(JSON.stringify(solve(data)));
다른 언어의 코드도 읽어보기
Python
import json
data = json.loads('{"values": [], "grid": ["..", ".."]}')
def solve(d):
grid = d["grid"]
width = len(grid[0])
queue, seen = [(0,0,0)], {(0,0)}
while queue:
x,y,distance = queue.pop(0)
if x == width-1 and y == len(grid)-1: return distance
for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
nx,ny = x+dx,y+dy
if 0 <= nx < width and 0 <= ny < len(grid) and grid[ny][nx] != "#" and (nx,ny) not in seen:
seen.add((nx,ny)); queue.append((nx,ny,distance+1))
return -1
print(json.dumps(solve(data), ensure_ascii=False))