새소식

Baekjoon

[BaekJoon] 2210 번 숫자판 점프 문제 - (nodejs)

  • -
728x90
문제 번호 :  2210번

문제 바로가기 https://www.acmicpc.net/problem/2210

 

<<< 문제 내용 >>>

 

const fs = require('fs'); const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt'; let input = fs.readFileSync(filePath).toString().trim().split('\r\n'); // 표준입력 테스트를 위해 이렇게 했지만 제출을 위해서는 // split('\r\n') 에서 \r은 제거하고 제출해야합니다. const tables = input.map(el=> el.split(' ')); let visited = Array.from(Array(tables.length), () => Array(tables.length).fill(0)); let arr = []; const dy = [-1,1,0,0]; const dx = [0,0,-1,1]; for(let i=0; i< tables.length; i++){ for(let j=0; j<tables.length; j++){ dfs(0, i, j, ''); } } const set = new Set(arr); const setarr = [...set]; console.log(setarr.length); function dfs(count/*재귀횟수*/, y, x, str){ if(count==6){ arr.push(str); return; } else{ str += tables[y][x]; for(let i=0; i<4; i++){ ay = y+ dy[i]; ax = x+ dx[i]; if(ay<0 || ay>=tables.length || ax<0 || ax>=tables.length){ continue; } dfs(count+1, ay, ax, str); } } }

* 기본적으로 브루트포스로 전부 찾아야하는 문제입니다. DFS로 탐색하면서 재귀횟수가 6번이되면 멈추도록 하고, 6번이 됐을때 저장된 내용을 기입한 후 함수를 종료합니다.

 

* 고려해야할 점은 중복이 가능하다는 점(visited 사용x)과 배열의 테두리를 넘어가는 방향은 제거해주어야 합니다.

 

 

 

도움이 되셨다면 공감 부탁드립니다.

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.