Skip to content

Commit

Permalink
[Silver IV 6186] Title: Best Grass, Time: 176 ms, Memory: 22944 KB -N…
Browse files Browse the repository at this point in the history
…KLCBHub
  • Loading branch information
dongx0915 committed Aug 28, 2023
1 parent 05605d9 commit d01e079
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.lang.StringBuilder
import java.util.LinkedList
import java.util.Queue

private val map = arrayListOf<StringBuilder>()
private val dx = arrayOf(-1, 0, 1, 0)
private val dy = arrayOf( 0, -1, 0, 1)

fun Pair<Int, Int>.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<Pair<Int, Int>> = 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))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# <img width="20px" src="https://d2gd6pc034wcta.cloudfront.net/tier/7.svg" class="solvedac-tier"> [Best Grass](https://www.acmicpc.net/problem/6186)

| 제출 번호 | 닉네임 | 채점 결과 | 메모리 | 시간 | 언어 | 코드 길이 |
|---|---|---|---|---|---|---|
|65747002|dongdong99|맞았습니다!! |22944KB|176ms|Kotlin (JVM)|1197B|

## 문제
<p>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.</p>

<p>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.</p>

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

<pre>.#....
..#...
..#..#
...##.
.#....</pre>

<p>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.</p>

## 입력
<ul>
<li>Line 1: Two space-separated integers: R and C</li>
<li>Lines 2..R+1: Line i+1 describes row i of the field with C characters, each of which is a '#' or a '.'</li>
</ul>

## 출력
<ul>
<li>Line 1: A single integer that is the number of grass clumps Bessie can munch</li>
</ul>

0 comments on commit d01e079

Please sign in to comment.