티스토리 뷰

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

 

14397번: 해변

단위 정육각형 이루어져 있는 지도가 주어졌을 때, 해변의 길이를 구하는 프로그램을 작성하시오. 해변은 정육각형의 변 중에서 한 쪽은 물인데, 한 쪽은 땅인 곳을 의미한다.

www.acmicpc.net

const fs = require('fs');
const [n, ...arr] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");

const [R, C] = n.split(' ').map(v => +v);
let answer = 0;
const board = []

for (let i = 0; i < R; i++) {
  board.push([]);
  if (i % 2 == 1) board[i].push('*');
  for (let j = 0; j < C; j++) {
    board[i].push(arr[i][j])
    board[i].push(arr[i][j])
  }
}


for (let i = 0; i < R; i++) {
  for (let j = 0; j < board[i].length; j++) {
    if (board[i][j] == '#') {
      if (j > 0 && board[i][j - 1] == '.') {
        answer++;
      }
      if (j < board[i].length - 1 && board[i][j + 1] == '.') {
        answer++;
      }
      if (i > 0 && board[i - 1][j] == '.') {
        answer++;
      }
      if (i < R - 1 && board[i + 1][j] == '.') {
        answer++;
      }
    }
  }
}

console.log(answer)
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
글 보관함