728x90

https://school.programmers.co.kr/learn/courses/30/lessons/120866

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

function solution(board) {
    const vector = [[1,0],[-1,0],[0,1],[0,-1], [-1,-1], [-1, 1], [1,1], [1, -1]];
    
    for(let i = 0 ; i < board.length ; i++) {
        for( let j = 0 ; j < board[0].length; j++) {
            if(board[i][j] === 1) {
                const vectors = [];
                vector.forEach(v => {
                    const x = v[0] + i;
                    const y = v[1] + j;
                    
                    if(x >= 0 && y >= 0 && x < board.length && y < board[0].length) {
                        vectors.push([x,y]);
                    }
                });
                
                vectors.forEach((v) => {
                    if(board[v[0]][v[1]] === 0) {
                        board[v[0]][v[1]] = -1;
                    }
                })
            }
        }
    }
    
    let answer = 0;
    board.forEach((bb) => {
        bb.forEach((b) => {
            if(b === 0) {
                answer++;
            }
        })
    })
    
    return answer;
}
728x90

https://www.acmicpc.net/problem/2178

 

2178번: 미로 탐색

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const [N, M] = input.shift().split(" ").map( i => Number(i));
const maps = [];
const vector = [[0,1], [1,0], [0,-1], [-1,0]];

input.forEach((i) => {
   i = i.split("").map(d => Number(d));
   maps.push(i);
});

const bfs = () => {
    const queue = [[0,0]];

    while(queue.length) {
        const cur = queue.shift();

        if(maps[cur[0]][cur[1]] !== 0) {
            const vectors = [];

            vector.forEach((v) => {
                const x = cur[0]+v[0];
                const y = cur[1]+v[1];

                if(x >= 0 && y >= 0 && x < N && y < M && maps[x][y] === 1) {
                    vectors.push([x,y]);
                }
            });

            let prev = maps[cur[0]][cur[1]];

            vectors.forEach((v) => {
                maps[v[0]][v[1]] = prev+1;
            })

            queue.push(...vectors);
        }
    }

    return maps[N-1][M-1];
}


console.log(bfs());
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/1844

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

function solution(maps) {
    var answer = 0;
    // 상 하 좌 우
    const vector = [[-1, 0], [1, 0], [0, -1], [0, 1]];
    
    const bfs = () => {
        const queue = [[0,0]];
        
        while(queue.length) {
            const cur = queue.shift();            
            if(maps[cur[0]][cur[1]] !== 0) {
                const vectors = [];
                
                vector.forEach((v) => {
                    const x = Number(cur[0])+ Number(v[0]);
                    const y = Number(cur[1])+ Number(v[1]);
                    
                    if(x >= 0 && y >= 0 && x < maps.length && y < maps[0].length && maps[x][y] === 1) {
                        vectors.push([x,y]);
                    }
                });
                
                const prev = maps[cur[0]][cur[1]];
                
                vectors.forEach((v) => {
                    maps[v[0]][v[1]] = prev+1;
                });
                queue.push(...vectors);
            }
        }
    }
    
    bfs();
    return maps[maps.length-1][maps[0].length-1] === 1 ? -1 : maps[maps.length-1][maps[0].length-1];
}
728x90

https://www.acmicpc.net/problem/1766

 

1766번: 문제집

첫째 줄에 문제의 수 N(1 ≤ N ≤ 32,000)과 먼저 푸는 것이 좋은 문제에 대한 정보의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 둘째 줄부터 M개의 줄에 걸쳐 두 정수의 순서쌍 A,B가 빈칸을 사이에 두고 주

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const [N, M] = input.shift().split(" ").map(n => Number(n));
const graph = new Array(N+1).fill(0).map(i => []);
const indegree = new Array(N+1).fill(0);

for(let i = 0 ; i < M ; i++) {
    const [to, from] = input[i].split(' ').map(n => Number(n));
    graph[to].push(from);
    indegree[from]++;
}

const tOrder = [];
const queue = [];

for(let i = 1; i < graph.length ; i ++) {
    if(indegree[i] === 0) {
        queue.push(i);
        break;
    }
}

while(queue.length) {
    const node = queue.shift();
    indegree[node] = -1;

    const targetNode = graph[node];
    targetNode.forEach((i) => {
       indegree[i]--;
    });
    tOrder.push(node);

    for(let i = 1; i < graph.length ; i ++) {
        if(indegree[i] === 0) {
            queue.push(i);
            break;
        }
    }
}

console.log(tOrder.join(" "))

+ Recent posts