📖 Todo
사탕 게임
테트로미노
다트 게임
📝 Review
사탕 게임
const [num, ...arr] = require('fs').readFileSync(__dirname + '/example.txt').toString().trim().split('\n'); const N = Number(num); const candys = Array.from(Array(N), () => new Array(N).fill('A')); let answer = 0; function check() { let curAns = 1; for (let i = 0; i < N; i++) { let count = 1; for (let j = 1; j < N; j++) { if (candys[i][j] === candys[i][j - 1]) count += 1; else count = 1; if (count > curAns) curAns = count; } count = 1; for (let j = 1; j < N; j++) { if (candys[j][i] === candys[j - 1][i]) count += 1; else count = 1; if (count > curAns) curAns = count; } } return curAns; } for (let i = 0; i < N; i++) { const candy = arr[i].split(''); for (let j = 0; j < N; j++) { candys[i][j] = candy[j]; } } for (let x = 0; x < N; x++) { for (let y = 0; y < N; y++) { if (y + 1 < N) { [candys[x][y], candys[x][y + 1]] = [candys[x][y + 1], candys[x][y]]; const num = check(); if (num > answer) answer = num; [candys[x][y], candys[x][y + 1]] = [candys[x][y + 1], candys[x][y]]; } if (x + 1 < N) { [candys[x][y], candys[x + 1][y]] = [candys[x + 1][y], candys[x][y]]; const num = check(); if (num > answer) answer = num; [candys[x][y], candys[x + 1][y]] = [candys[x + 1][y], candys[x][y]]; } } } console.log(answer);
부르트포스..
매번 사탕을 돌려가면서 가장 긴 동일한 사탕의 갯수를 반환하면 된다..
테트로미노
const [num, ...arr] = require('fs').readFileSync(__dirname + '/dev/stdin').toString().trim().split('\n'); const [n, m] = num.split(' ').map(Number); const graph = Array.from(Array(n), () => new Array(m).fill(0)); let answer = 0; for (let i = 0; i < n; i++) { const row = arr[i].split(' '); for (let j = 0; j < m; j++) graph[i][j] = Number(row[j]); } const check = (y, x) => { let value = 0; if(y+2 <= n-1 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y+1][x] + graph[y+2][x]); } if(y+2 <= n-1 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y+1][x+1] + graph[y+2][x+1]); } if(y+1 <= n-1 && x+2 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y][x+1] + graph[y][x+2]); } if(y+1 <= n-1 && x+2 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y][x+2] + graph[y+1][x+2]); } if(y+1 <= n-1 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y][x+1] + graph[y+1][x+1]); } if(y+2 <= n-1 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+2][x] + graph[y+2][x+1]); } if(y+2 <= n-1 && x-1 >= 0){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+2][x] + graph[y+2][x-1]); } if(y+1 <= n-1 && x-2 >= 0){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+1][x-1] + graph[y+1][x-2]); } if(y+1 <= n-1 && x+2 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+1][x+1] + graph[y+1][x+2]); } if(y+3 <= n-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+2][x] + graph[y+3][x]); } if(y+2 <= n-1 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+1][x+1] + graph[y+2][x+1]); } if(y+2 <= n-1 && x-1 >= 0){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+1][x-1] + graph[y+2][x-1]); } if(y+1 <= n-1 && x-1 >= 0 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y+1][x] + graph[y+1][x-1]); } if(y+1 <= n-1 && x+2 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y+1][x+1] + graph[y+1][x+2]); } if(x+3 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y][x+2] + graph[y][x+3]); } if(y+1 <= n-1 && x-1 >= 0 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+1][x-1] + graph[y+1][x+1]); } if(y+1 <= n-1 && x-1 >= 0 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y][x+1] + graph[y][x-1] + graph[y+1][x]); } if(y+2 <= n-1 && x-1 >= 0){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+2][x] + graph[y+1][x-1]); } if(y+2 <= n-1 && x+1 <= m-1){ value = Math.max(value, graph[y][x] + graph[y+1][x] + graph[y+2][x] + graph[y+1][x+1]); } return value; } for (let x = 0; x < n; x++) { for (let y = 0; y < m; y++) { answer = Math.max(answer, check(x, y)); } } console.log(answer);
뭔가 장대해 보이지만, 테트로미노라는 것은 정사각형 4개로 한정되어있기 때문에 나올 수 있는 모든 경우의 수는 19개..
따라서, 이 19개에 해당하는 경우의 수를 판별해주면 된다.