[JavaScript]プログラマーコース建設LEVEL 3


問題のソース
const [dx, dy] = [[0, 0, -1, 1], [-1, 1, 0, 0]]; // 상 하 좌 우

const bfs = (board) => {
    const n = board.length;
    const visit = Array.from(Array(n), () => new Array(n).fill(0));
    const queue = [[0, 0, 0, 0]]; // x, y, 현재까지 비용, 방향(상하: 0, 좌우: 1)
    
    while (queue.length) {
        const [x, y, cost, dir] = queue.shift();
        
        for (let i=0; i<4; i++) {
            const nx = x + dx[i];
            const ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= n || ny >= n || board[nx][ny] === 1) continue;
            
            const direction = i < 2 ? 0 : 1;
            const costByDirection = dir === direction || (x === 0 && y === 0) ? 100 : 600;
            
            if (visit[nx][ny] === 0 || visit[nx][ny] >= cost + costByDirection) {
                visit[nx][ny] = cost + costByDirection;
                queue.push([nx, ny, cost + costByDirection, direction]);
            }
        }
    }
    
    return visit[n-1][n-1];
};

function solution(board) {
    return bfs(board);
}
に答える
一般的なBFS問題とは若干の違いがある.
上下に移動するか、左右に移動するかを区別して、料金を計算します.
  • visit配列をチェックすると、以前に計算されたコストが現在計算されたコストより大きい場合、スキップではなくアクセスノードをキューに再配置します.