Skip to content

Commit

Permalink
#18 22.08.14 > 2 > 유기농 배추
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Aug 14, 2022
1 parent d9d20c0 commit 5a6aef4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/bj/silver/2/1012_re.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
const T = input.shift()*1;
let graph, visited, m, n, k, count, p = 0;

// 문자 input -> 숫자 input
for(let i = 0; i < input.length; i++) {
input[i] = input[i].split(' ').map((v) => +v);
}

// testcase수만큼 반복문 돌기
for(let t = 0; t < T; t++) {
[m, n, k] = input[p];
// console.log(m, n, k);
graph = Array.from(Array(m), () => Array(n).fill(0)); // 배추 지도
visited = Array.from(Array(m), () => Array(n).fill(0)); // 방문 여부를 체크할 map
answer = 0;
let tmp = p; // 현재 p의 값을 tmp에 저장

// 배추 심기
for(p = p+1; p <= k+tmp; p++) {
let [x, y] = input[p];
graph[x][y] = 1;
}

// m x n의 배추 지도에서 벌레 개수 세기
for(let x = 0; x < m; x++) {
for(let y = 0; y < n; y++) {
if(graph[x][y] === 1 && visited[x][y] === 0) { // 배추가 있고, 아직 탐색하지 않았다면
dfs(x, y);
answer++;
}
}
}
console.log(answer);
}

function dfs(x, y) {
visited[x][y] = 1; // 방문처리
let dx = [-1, 0, 1, 0];
let dy = [0, 1, 0, -1];

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

if(nx >= 0 && ny >= 0 && nx < m && ny < n) {
if(visited[nx][ny] === 0 && graph[nx][ny] === 1) {
dfs(nx, ny);
}
}
}
}

3 changes: 2 additions & 1 deletion src/bj/silver/2/1912_re.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ let max = dp[0];

for(let i = 1; i <= n; i++) {
let item = progression[i]+dp[i-1];
console.log(`${i} .... 현재 item값은 ${item}, max는 ${max}`);
if(item > max) {
dp[i] = item;
max = item;
}
}
}g

console.log(Math.max(...dp));
2 changes: 1 addition & 1 deletion src/bj/silver/2/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Silver 2
||문제번호|문제명|응시 날짜|채점 결과|링크|
|:-:|:--:|:--:|:---:|:---:|--|
|1|1012|[유기농 배추](./1012.js)|22.08.06|X|[아직]()|
|1|1012|[유기농 배추](./1012.js)|22.08.06|[22.08.14](./1012_re.js)||
|2|1260|[DFS와 BFS](./1260.js)|22.08.08|다시!|[참고사이트](https://cider.tistory.com/4)|
|3|1138|[한 줄로 서기](./1138.js)|22.08.09|[22.08.13](./1138_re.js)|
|4|1912|[연속합](./1912.js)|22.08.10|[22.08.14](./1912_re.js)|

0 comments on commit 5a6aef4

Please sign in to comment.