다윤 풀이
const filePath = process.platform === "linux" ? "/dev/stdin" : "input.txt"; let [NM, ...inputs] = require("fs") .readFileSync(filePath) .toString() .trim() .split("\n"); const [N, M] = NM.split(" ").map(Number); const board = []; for (let input of inputs) { let tiles = input.split(""); board.push(tiles); } const bfs = (i, j) => { const queue = [[i, j]]; while (queue.length) { let [i, j] = queue.shift(); let now = board[i][j]; board[i][j] = 0; if (now == "-") { j++; } else { i++; } if (i >= 0 && i < N && j >= 0 && j < M && board[i][j] == now) queue.push([i, j]); } }; let answer = 0; for (let i = 0; i < N; i++) { for (let j = 0; j < M; j++) { const now = board[i][j]; if (now) { bfs(i, j); answer++; } } } console.log(answer);
민수 풀이
let input = require('fs').readFileSync(__dirname + "/../input.txt").toString().trim().split("\n"); // let input = require('fs').readFileSync("/dev/stdin").toString().trim().split("\n"); const [N, M] = input.shift().split(' ').map(Number); input = input.map(e => e.trim().split('')); let dy = [1, -1, 0, 0]; let dx = [0, 0, 1, -1]; let cnt = 0; function dfs(i, j){ if(i > -1 && j > -1 && i < N && j < M && input[i][j] !== 'X'){ const chkRowCol = input[i][j] === '-' ? [0,1] : [2,3]; const value = input[i][j]; input[i][j] = 'X'; for(let k = chkRowCol[0]; k < chkRowCol[1]+1; k++){ const my = dx[k] + i; const mx = dy[k] + j; if(mx > -1 && my > -1 && mx < M && my < N){ if(input[my][mx] !== 'X' && value === input[my][mx]){ dfs(my,mx); } } } } } for(let i = 0; i < N; i++){ for(let j = 0; j < M; j++){ if(input[i][j] !== 'X') { cnt += 1; dfs(i, j); } } } console.log(cnt)
송희 풀이
const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; const [size, ...floor] = fs.readFileSync(filePath).toString().trim().split("\n"); const [n, m] = size.split(" "); const floorShape = floor.map((v, i) => v.split("")); let count = 0; for (i = 0; i < +n; i++) { // ➡️➡️➡️ let continueWidth = true; for (j = 0; j < +m; j++) { if (continueWidth == true && floorShape[i][j] == "-") { count++; continueWidth = false; } else if (floorShape[i][j] == "|") continueWidth = true; } } for (j = 0; j < +m; j++) { //⬇️⬇️⬇️ let continueHeight = true; for (i = 0; i < +n; i++) { if (continueHeight == true && floorShape[i][j] == "|") { count++; continueHeight = false; } else if (floorShape[i][j] == "-") continueHeight = true; } } console.log(count);
승민 풀이
let fs = require('fs'); //const inputs = fs.readFileSync('/dev/stdin').toString().split('\n'); const inputs = fs.readFileSync(__dirname+'/ex2.txt').toString().split('\n'); const [n, m] = inputs[0].split(' ').map(Number); //세로 가로 let graph = []; for (let i = 1; i <= n; i++) { graph.push(inputs[i].split('')); } let cnt = 0; // - for (let i=0; i<n; i++) { let flag = true; for (let j=0; j<m; j++) { if (flag && graph[i][j] === '-') { cnt++; flag = false; } else if (graph[i][j] === '|') { flag = true; } } } // | for (let i=0; i<m; i++) { let flag = true; for (let j=0; j<n; j++) { if (flag && graph[j][i] === '|') { cnt++; flag = false; } else if (graph[j][i] === '-') { flag = true; } } } console.log(cnt);