티스토리 뷰

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

 

1987번: 알파벳

세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으

www.acmicpc.net

const input = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n').map(v=>v.trim())
const [N,M] = input[0].split(' ').map(Number);

let board = input.slice(1).map(v=>v.split(''));
const dir = [[0,1],[0,-1],[-1,0],[1,0]]

let start = (1<<board[0][0].charCodeAt(0)-65);
let answer = 1;
solve([0,0],1,start);
console.log(answer)

function solve(pos,cnt,used){
  const [r,c] = pos;
  dir.forEach(v=>{
    const [x,y] = v;
    if(r+x>=0 && c+y>=0 && r+x<N && c+y<M && !(used&((1<<board[r+x][y+c].charCodeAt(0)-65)))){
      answer = Math.max(cnt+1,answer);
      solve([r+x,c+y],cnt+1,used|((1<<board[r+x][y+c].charCodeAt(0)-65)))
    }
  })
}
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함