Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Git
- 항해99
- Cookie
- 코드잇
- MySQL
- 코딩테스트준비
- 파이썬
- 99클럽
- 프로그래머스
- 엘리스sw트랙
- nestjs
- CSS
- 꿀단집
- 데이터베이스시스템
- TiL
- presignedurl
- aws
- JavaScript
- redis
- 개발자취업
- 코딩테스트
- 유노코딩
- node.js
- 파이썬프로그래밍기초
- HTML
- SQL
- 중간이들
- 방송대컴퓨터과학과
- Python
- 방송대
Archives
- Today
- Total
배꼽파지 않도록 잘 개발해요
99클럽 코테 스터디 31일차 TIL - 양 한마리 본문
function countSheepGroups(grid, H, W) {
const directions = [
[-1, 0], [1, 0], [0, -1], [0, 1]
];
function dfs(x, y) {
if (x < 0 || x >= H || y < 0 || y >= W) return;
if (grid[x][y] !== '#') return;
grid[x][y] = '.';
for (let [dx, dy] of directions) {
dfs(x + dx, y + dy);
}
}
let sheepGroupCount = 0;
for (let i = 0; i < H; i++) {
for (let j = 0; j < W; j++) {
if (grid[i][j] === '#') {
sheepGroupCount++;
dfs(i, j);
}
}
}
return sheepGroupCount;
}
function solve(input) {
try {
const lines = input.trim().split('\n');
let idx = 0;
const T = parseInt(lines[idx++]);
const results = [];
for (let t = 0; t < T; t++) {
const [H, W] = lines[idx++].split(' ').map(Number);
// H나 W가 0이면 양 무리는 0개
if (H === 0 || W === 0) {
results.push(0);
continue;
}
const grid = [];
for (let i = 0; i < H; i++) {
grid.push(lines[idx++].split(''));
}
results.push(countSheepGroups(grid, H, W));
}
// 최종적으로 한 번에 출력
console.log(results.join('\n'));
} catch (error) {
console.error("An error occurred:", error.message);
}
}
// 예제 입력
const input = `2
4 4
#.#.
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###`;
solve(input);
728x90
'코딩테스트 > 99클럽' 카테고리의 다른 글
99클럽 코테 스터디 31일차 TIL - 영역 구하기 (0) | 2024.08.26 |
---|---|
99클럽 코테 스터디 31일차 TIL - Number of Good Leaf Nodes Pairs (0) | 2024.08.24 |
99클럽 코테 스터디 31일차 TIL - Bad Grass (0) | 2024.08.23 |
99클럽 코테 스터디 31일차 TIL - Clear Cold Water (0) | 2024.08.22 |
99클럽 코테 스터디 29일차 TIL - Arranging Coins (0) | 2024.08.21 |