|
| 1 | +const fs = require('fs'); |
| 2 | +const stdin = ( |
| 3 | + process.platform === 'linux' |
| 4 | + ? fs.readFileSync('/dev/stdin').toString() |
| 5 | + : `1 1 1 1 1 |
| 6 | +1 1 1 1 1 |
| 7 | +1 1 1 1 1 |
| 8 | +1 1 1 2 1 |
| 9 | +1 1 1 1 1` |
| 10 | +).split('\n'); |
| 11 | + |
| 12 | +const input = (() => { |
| 13 | + let line = 0; |
| 14 | + return () => stdin[line++]; |
| 15 | +})(); |
| 16 | + |
| 17 | +const solution = () => { |
| 18 | + const board = new Array(5); |
| 19 | + const result = new Set(); |
| 20 | + |
| 21 | + for (let i = 0; i < 5; i++) { |
| 22 | + board[i] = input().split(' ').map(Number); |
| 23 | + } |
| 24 | + |
| 25 | + for (let i = 0; i < 5; i++) { |
| 26 | + for (let j = 0; j < 5; j++) { |
| 27 | + bfs(i, j, board, result); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return result.size; |
| 32 | +}; |
| 33 | + |
| 34 | +const bfs = (x, y, board, result) => { |
| 35 | + const queue = [[x, y, 0, `${board[x][y]}`]]; // 시작 좌표, 이동 횟수, 이동 경로(문자열) |
| 36 | + |
| 37 | + const dx = [-1, 1, 0, 0]; |
| 38 | + const dy = [0, 0, -1, 1]; |
| 39 | + |
| 40 | + while (queue.length > 0) { |
| 41 | + const [x, y, count, path] = queue.shift(); |
| 42 | + |
| 43 | + if (count === 5) { |
| 44 | + if (!result.has(path)) { |
| 45 | + result.add(path); |
| 46 | + } |
| 47 | + |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + for (let i = 0; i < 5; i++) { |
| 52 | + const nx = x + dx[i]; |
| 53 | + const ny = y + dy[i]; |
| 54 | + |
| 55 | + if (checkBoardRange(nx, ny)) { |
| 56 | + queue.push([nx, ny, count + 1, path + String(board[nx][ny])]); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +const checkBoardRange = (x, y) => { |
| 63 | + if (x >= 0 && x < 5 && y >= 0 && y < 5) return true; |
| 64 | + else return false; |
| 65 | +}; |
| 66 | + |
| 67 | +console.log(solution()); |
0 commit comments