Skip to content

Commit

Permalink
12-알파벳
Browse files Browse the repository at this point in the history
  • Loading branch information
minbr0ther committed Mar 4, 2022
1 parent 0245fa0 commit 64ed365
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 2022/graph-traversal/12-알파벳/app.js
@@ -0,0 +1,34 @@
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
let input = fs.readFileSync(filePath).toString().trim().split("\n");

const [R, C] = input.shift().split(" ").map(Number);
const graph = input.map((str) => str.split(""));

const dy = [1, 0, -1, 0];
const dx = [0, 1, 0, -1];

let result = 0;
const visited = new Set([graph[0][0]]);

const dfs = (y, x, count = 1) => {
result = Math.max(result, count);

for (let i = 0; i < 4; i++) {
const ny = y + dy[i];
const nx = x + dx[i];

if (ny >= 0 && ny < R && nx >= 0 && nx < C) {
if (!visited.has(graph[ny][nx])) {
visited.add(graph[ny][nx]);
dfs(ny, nx, ++count);
--count;
visited.delete(graph[ny][nx]);
}
}
}
};

dfs(0, 0);

console.log(result);
6 changes: 6 additions & 0 deletions 2022/graph-traversal/12-알파벳/input.txt
@@ -0,0 +1,6 @@
5 5
IEFCJ
FHFKC
FFALF
HFGCF
HMCHH

0 comments on commit 64ed365

Please sign in to comment.