-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)| |