From d01e0791071e9ce220ca64b109bf1cf4092334aa Mon Sep 17 00:00:00 2001 From: Donghyeon-Kim <63500239+Donghyeon0915@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:40:44 +0900 Subject: [PATCH] [Silver IV 6186] Title: Best Grass, Time: 176 ms, Memory: 22944 KB -NKLCBHub --- .../Best\342\200\205Grass.kt" | 53 +++++++++++++++++++ .../#6186 Best Grass/README.md | 32 +++++++++++ 2 files changed, 85 insertions(+) create mode 100644 "BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/Best\342\200\205Grass.kt" create mode 100644 BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/README.md diff --git "a/BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/Best\342\200\205Grass.kt" "b/BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/Best\342\200\205Grass.kt" new file mode 100644 index 00000000..08f12914 --- /dev/null +++ "b/BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/Best\342\200\205Grass.kt" @@ -0,0 +1,53 @@ +import java.lang.StringBuilder +import java.util.LinkedList +import java.util.Queue + +private val map = arrayListOf() +private val dx = arrayOf(-1, 0, 1, 0) +private val dy = arrayOf( 0, -1, 0, 1) + +fun Pair.isOutRange(n:Int, m: Int): Boolean { + return first < 0 || first >= n || second < 0 || second >= m +} + +fun bfs(x:Int, y: Int, n: Int, m: Int){ + val q:Queue> = LinkedList() + q.add(Pair(x, y)) + + while (q.isNotEmpty()){ + val cur = q.poll() + map[cur.first][cur.second] = ' ' + + for (i in 0 until 4) { + val next = Pair(cur.first + dx[i], cur.second + dy[i]) + if(next.isOutRange(n, m)) continue + + if(map[next.first][next.second] == '#') { + q.add(next) + } + } + } +} + +fun solve(n:Int, m:Int): Int { + var cnt = 0 + for (i in 0 until n){ + for(j in 0 until m){ + if(map[i][j] == '#'){ + bfs(i, j, n, m) + cnt++ + } + } + } + + return cnt +} + +fun main() { + val (n, m) = readln().split(" ").map { it.toInt() } + repeat(n){ + map.add(StringBuilder(readln())) + } + + println(solve(n, m)) +} \ No newline at end of file diff --git a/BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/README.md b/BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/README.md new file mode 100644 index 00000000..8f456cc7 --- /dev/null +++ b/BaekJoon Online Judge/BFS(Breadth-First-Search)/#6186 Best Grass/README.md @@ -0,0 +1,32 @@ +# [Best Grass](https://www.acmicpc.net/problem/6186) + +| 제출 번호 | 닉네임 | 채점 결과 | 메모리 | 시간 | 언어 | 코드 길이 | +|---|---|---|---|---|---|---| +|65747002|dongdong99|맞았습니다!! |22944KB|176ms|Kotlin (JVM)|1197B| + +## 문제 +

Bessie is planning her day of munching tender spring grass and is gazing out upon the pasture which Farmer John has so lovingly partitioned into a grid with R (1 <= R <= 100) rows and C (1 <= C <= 100) columns. She wishes to count the number of grass clumps in the pasture.

+ +

Each grass clump is shown on a map as either a single '#' symbol or perhaps two '#' symbols side-by-side (but not on a diagonal). No two symbols representing two different clumps are adjacent. Given a map of the pasture, tell Bessie how many grass clumps there are.

+ +

By way of example, consider this pasture map where R=5 and C=6:

+ +
.#....
+..#...
+..#..#
+...##.
+.#....
+ +

This pasture has a total of 5 clumps: one on the first row, one that spans the second and third row in column 2, one by itself on the third row, one that spans columns 4 and 5 in row 4, and one more in row 5.

+ +## 입력 +
    +
  • Line 1: Two space-separated integers: R and C
  • +
  • Lines 2..R+1: Line i+1 describes row i of the field with C characters, each of which is a '#' or a '.'
  • +
+ +## 출력 +
    +
  • Line 1: A single integer that is the number of grass clumps Bessie can munch
  • +
+