<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://02ggang9.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://02ggang9.github.io/" rel="alternate" type="text/html" /><updated>2025-05-08T13:58:49+00:00</updated><id>https://02ggang9.github.io/feed.xml</id><title type="html">GGANG9</title><subtitle>An amazing website.</subtitle><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><entry><title type="html">[알고리즘] 백준 - 2075</title><link href="https://02ggang9.github.io/algorithm/baekjoon2075/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 2075" /><published>2025-05-08T00:00:00+00:00</published><updated>2025-05-08T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon2075</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon2075/"><![CDATA[<h1 id="silver-iii-n번째-큰-수---2075">[Silver III] N번째 큰 수 - 2075</h1>

<p><a href="https://www.acmicpc.net/problem/2075">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 339352 KB, 시간: 1584 ms</p>

<h3 id="분류">분류</h3>

<p>자료 구조, 우선순위 큐, 정렬</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 5월 8일 22:44:50</p>

<h3 id="문제-설명">문제 설명</h3>

<p>N×N의 표에 수 N<sup>2</sup>개 채워져 있다. 채워진 수에는 한 가지 특징이 있는데, 모든 수는 자신의 한 칸 위에 있는 수보다 크다는 것이다. N=5일 때의 예를 보자.</p>

<table class="table table-bordered" style="width:15%">
	<tbody>
		<tr>
			<td style="width:3%">12</td>
			<td style="width:3%">7</td>
			<td style="width:3%">9</td>
			<td style="width:3%">15</td>
			<td style="width:3%">5</td>
		</tr>
		<tr>
			<td>13</td>
			<td>8</td>
			<td>11</td>
			<td>19</td>
			<td>6</td>
		</tr>
		<tr>
			<td>21</td>
			<td>10</td>
			<td>26</td>
			<td>31</td>
			<td>16</td>
		</tr>
		<tr>
			<td>48</td>
			<td>14</td>
			<td>28</td>
			<td>35</td>
			<td>25</td>
		</tr>
		<tr>
			<td>52</td>
			<td>20</td>
			<td>32</td>
			<td>41</td>
			<td>49</td>
		</tr>
	</tbody>
</table>

<p>이러한 표가 주어졌을 때, N번째 큰 수를 찾는 프로그램을 작성하시오. 표에 채워진 수는 모두 다르다.</p>

<h3 id="입력">입력</h3>

<p>첫째 줄에 N(1 ≤ N ≤ 1,500)이 주어진다. 다음 N개의 줄에는 각 줄마다 N개의 수가 주어진다. 표에 적힌 수는 -10억보다 크거나 같고, 10억보다 작거나 같은 정수이다.</p>

<h3 id="출력">출력</h3>

<p>첫째 줄에 N번째 큰 수를 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import java.util.*

fun main() {
    val n = readln().toInt()
    val pQueue: Queue&lt;Long&gt; = PriorityQueue(compareByDescending { it })

    repeat(n) {
        val numbers = readln().split(" ").map { it.toLong() }
        numbers.forEach { pQueue.add(it) }
    }

    repeat(n - 1) {
        pQueue.poll()
    }

    println(pQueue.poll())
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver III] N번째 큰 수 - 2075]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 11279</title><link href="https://02ggang9.github.io/algorithm/baekjoon11279/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 11279" /><published>2025-05-05T00:00:00+00:00</published><updated>2025-05-05T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon11279</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon11279/"><![CDATA[<h1 id="silver-ii-최대-힙---11279">[Silver II] 최대 힙 - 11279</h1>

<p><a href="https://www.acmicpc.net/problem/11279">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 56128 KB, 시간: 1060 ms</p>

<h3 id="분류">분류</h3>

<p>자료 구조, 우선순위 큐</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 5월 7일 22:43:47</p>

<h3 id="문제-설명">문제 설명</h3>

<p>널리 잘 알려진 자료구조 중 최대 힙이 있다. 최대 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.</p>

<ol>
	<li>배열에 자연수 x를 넣는다.</li>
	<li>배열에서 가장 큰 값을 출력하고, <span style="line-height:1.6em">그 값을 배열에서 제거한다. </span></li>
</ol>

<p><span style="line-height:1.6em">프로그램은 처음에 비어있는 배열에서 시작하게 된다.</span></p>

<h3 id="입력">입력</h3>

<p>첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 자연수는 2<sup>31</sup>보다 작다.</p>

<h3 id="출력">출력</h3>

<p>입력에서 0이 주어진 횟수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import java.util.Queue
import java.util.*

fun main() {
    val n = readln().toInt()
    val pQueue: Queue&lt;Int&gt; = PriorityQueue(compareByDescending&lt;Int&gt; { it })

    repeat(n) {
        val value = readln().toInt()

        when(value) {
            0 -&gt; if (pQueue.isEmpty()) println(0) else println(pQueue.poll())
            else -&gt; pQueue.add(value)
        }
    }
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver II] 최대 힙 - 11279]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 11286</title><link href="https://02ggang9.github.io/algorithm/baekjoon11286/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 11286" /><published>2025-05-05T00:00:00+00:00</published><updated>2025-05-05T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon11286</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon11286/"><![CDATA[<h1 id="silver-i-절댓값-힙---11286">[Silver I] 절댓값 힙 - 11286</h1>

<p><a href="https://www.acmicpc.net/problem/11286">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 44448 KB, 시간: 716 ms</p>

<h3 id="분류">분류</h3>

<p>자료 구조, 우선순위 큐</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 5월 7일 23:29:31</p>

<h3 id="문제-설명">문제 설명</h3>

<p>절댓값 힙은 다음과 같은 연산을 지원하는 자료구조이다.</p>

<ol>
	<li>배열에 정수 x (x ≠ 0)를 넣는다.</li>
	<li>배열에서 절댓값이 가장 작은 값을 출력하고, 그 값을 배열에서 제거한다. 절댓값이 가장 작은 값이 여러개일 때는, 가장 작은 수를 출력하고, 그 값을 배열에서 제거한다.</li>
</ol>

<p>프로그램은 처음에 비어있는 배열에서 시작하게 된다.</p>

<h3 id="입력">입력</h3>

<p>첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 절댓값이 가장 작은 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 정수는 -2<sup>31</sup>보다 크고, 2<sup>31</sup>보다 작다.</p>

<h3 id="출력">출력</h3>

<p>입력에서 0이 주어진 회수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 절댓값이 가장 작은 값을 출력하라고 한 경우에는 0을 출력하면 된다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import kotlin.math.*
import java.util.*

fun main() {
    val n = readln().toInt()
    val pQueue: Queue&lt;Int&gt; = PriorityQueue { a, b -&gt;
        val absA = abs(a)
        val absB = abs(b)
        if (absA == absB) a - b else absA - absB
    }

    repeat(n) {
        val value = readln().toInt()

        if (value == 0) {
            if (pQueue.isEmpty()) println(0) else println(pQueue.poll())
        } else {
            pQueue.add(value)
        }
    }
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver I] 절댓값 힙 - 11286]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 11279</title><link href="https://02ggang9.github.io/algorithm/baekjoon1927/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 11279" /><published>2025-05-05T00:00:00+00:00</published><updated>2025-05-05T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon1927</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon1927/"><![CDATA[<h1 id="silver-ii-최소-힙---1927">[Silver II] 최소 힙 - 1927</h1>

<p><a href="https://www.acmicpc.net/problem/1927">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 34164 KB, 시간: 1132 ms</p>

<h3 id="분류">분류</h3>

<p>자료 구조, 우선순위 큐</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 5월 7일 23:29:13</p>

<h3 id="문제-설명">문제 설명</h3>

<p>널리 잘 알려진 자료구조 중 최소 힙이 있다. 최소 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.</p>

<ol>
	<li>배열에 자연수 x를 넣는다.</li>
	<li>배열에서 가장 작은 값을 출력하고, 그 값을 배열에서 제거한다.</li>
</ol>

<p>프로그램은 처음에 비어있는 배열에서 시작하게 된다.</p>

<h3 id="입력">입력</h3>

<p>첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 가장 작은 값을 출력하고 그 값을 배열에서 제거하는 경우이다. x는 2<sup>31</sup>보다 작은 자연수 또는 0이고, 음의 정수는 입력으로 주어지지 않는다.</p>

<h3 id="출력">출력</h3>

<p>입력에서 0이 주어진 횟수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 작은 값을 출력하라고 한 경우에는 0을 출력하면 된다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import java.util.*

fun main() {
    val n = readln().toInt()
    val pQueue: Queue&lt;Int&gt; = PriorityQueue(compareBy { it })
    repeat(n) {
        val value = readln().toInt()
        if (value == 0) {
            if (pQueue.isEmpty()) {
                println(0)
            } else {
                println(pQueue.poll())
            }
        } else {
            pQueue.add(value)
        }
    }
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver II] 최소 힙 - 1927]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 1697</title><link href="https://02ggang9.github.io/algorithm/baekjoon1697/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 1697" /><published>2025-04-21T00:00:00+00:00</published><updated>2025-04-21T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon1697</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon1697/"><![CDATA[<h1 id="silver-i-숨바꼭질---1697">[Silver I] 숨바꼭질 - 1697</h1>

<p><a href="https://www.acmicpc.net/problem/1697">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 31888 KB, 시간: 184 ms</p>

<h3 id="분류">분류</h3>

<p>너비 우선 탐색, 그래프 이론, 그래프 탐색</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 4월 21일 08:58:41</p>

<h3 id="문제-설명">문제 설명</h3>

<p>수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 때 걷는다면 1초 후에 X-1 또는 X+1로 이동하게 된다. 순간이동을 하는 경우에는 1초 후에 2*X의 위치로 이동하게 된다.</p>

<p>수빈이와 동생의 위치가 주어졌을 때, 수빈이가 동생을 찾을 수 있는 가장 빠른 시간이 몇 초 후인지 구하는 프로그램을 작성하시오.</p>

<h3 id="입력">입력</h3>

<p>첫 번째 줄에 수빈이가 있는 위치 N과 동생이 있는 위치 K가 주어진다. N과 K는 정수이다.</p>

<h3 id="출력">출력</h3>

<p>수빈이가 동생을 찾는 가장 빠른 시간을 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import java.util.*

fun main() {
    val (n, k) = readln().split(" ").map { it.toInt() }
    val position = IntArray(100_001) { -1 }
    val queue: Queue&lt;Int&gt; = ArrayDeque()

    queue.add(n)
    position[n] = 0

    while (queue.isNotEmpty()) {
        val current = queue.poll()
        if (current == k) {
            println(position[current])
            break
        }

        for (next in arrayOf(current + 1, current -1, current * 2)) {
            if (next in 0 until 100_001 &amp;&amp; position[next] == -1) {
                position[next] = position[current] + 1
                queue.add(next)
            }
        }
    }
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver I] 숨바꼭질 - 1697]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 1012</title><link href="https://02ggang9.github.io/algorithm/baekjoon1012/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 1012" /><published>2025-04-14T00:00:00+00:00</published><updated>2025-04-14T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon1012</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon1012/"><![CDATA[<h1 id="silver-ii-유기농-배추---1012">[Silver II] 유기농 배추 - 1012</h1>

<p><a href="https://www.acmicpc.net/problem/1012">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 25556 KB, 시간: 224 ms</p>

<h3 id="분류">분류</h3>

<p>그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 4월 14일 08:52:37</p>

<h3 id="문제-설명">문제 설명</h3>

<p>차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 효과적인 배추흰지렁이를 구입하기로 결심한다. 이 지렁이는 배추근처에 서식하며 해충을 잡아 먹음으로써 배추를 보호한다. 특히, 어떤 배추에 배추흰지렁이가 한 마리라도 살고 있으면 이 지렁이는 인접한 다른 배추로 이동할 수 있어, 그 배추들 역시 해충으로부터 보호받을 수 있다. 한 배추의 상하좌우 네 방향에 다른 배추가 위치한 경우에 서로 인접해있는 것이다.</p>

<p>한나가 배추를 재배하는 땅은 고르지 못해서 배추를 군데군데 심어 놓았다. 배추들이 모여있는 곳에는 배추흰지렁이가 한 마리만 있으면 되므로 서로 인접해있는 배추들이 몇 군데에 퍼져있는지 조사하면 총 몇 마리의 지렁이가 필요한지 알 수 있다. 예를 들어 배추밭이 아래와 같이 구성되어 있으면 최소 5마리의 배추흰지렁이가 필요하다. 0은 배추가 심어져 있지 않은 땅이고, 1은 배추가 심어져 있는 땅을 나타낸다.</p>

<table class="table table-bordered" style="width:40%">
	<tbody>
		<tr>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
		</tr>
		<tr>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
		</tr>
		<tr>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
		</tr>
		<tr>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
		</tr>
		<tr>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
		</tr>
		<tr>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%">0</td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
			<td style="text-align:center; width:4%"><strong>1</strong></td>
		</tr>
	</tbody>
</table>

<h3 id="입력">입력</h3>

<p>입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트 케이스에 대해 첫째 줄에는 배추를 심은 배추밭의 가로길이 M(1 ≤ M ≤ 50)과 세로길이 N(1 ≤ N ≤ 50), 그리고 배추가 심어져 있는 위치의 개수 K(1 ≤ K ≤ 2500)이 주어진다. 그 다음 K줄에는 배추의 위치 X(0 ≤ X ≤ M-1), Y(0 ≤ Y ≤ N-1)가 주어진다. 두 배추의 위치가 같은 경우는 없다.</p>

<h3 id="출력">출력</h3>

<p>각 테스트 케이스에 대해 필요한 최소의 배추흰지렁이 마리 수를 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package baekjoon.april2025

import java.util.*

fun main() {
    val t = readln().toInt()

    repeat(t) {
        val (m, n, k) = readln().split(" ").map { it.toInt() }
        val matrix = Array(m) { IntArray(n) { 0 } }
        val visited = Array(m) { BooleanArray(n) { false } }

        repeat(k) {
            val (x, y) = readln().split(" ").map { it.toInt() }
            matrix[x][y] = 1
        }

        val dx = arrayOf(0, 1, 0, -1)
        val dy = arrayOf(-1, 0, 1, 0)
        var result = 0
        val queue: Queue&lt;IntArray&gt; = ArrayDeque()

        for (x in 0 until m) {
            for (y in 0 until n) {
                if (matrix[x][y] != 1 || visited[x][y]) continue

                visited[x][y] = true
                queue.add(intArrayOf(x, y))

                while (queue.isNotEmpty()) {
                    val (currentX, currentY) = queue.poll()

                    repeat(4) {
                        val nx = dx[it] + currentX
                        val ny = dy[it] + currentY

                        if (nx !in 0 until m || ny !in 0 until n) return@repeat
                        if (matrix[nx][ny] == 0 || visited[nx][ny]) return@repeat

                        visited[nx][ny] = true
                        queue.add(intArrayOf(nx, ny))
                    }
                }
                result++
            }
        }

        println(result)
    }
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver II] 유기농 배추 - 1012]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 2447</title><link href="https://02ggang9.github.io/algorithm/baekjoon2447/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 2447" /><published>2025-04-13T00:00:00+00:00</published><updated>2025-04-13T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon2447</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon2447/"><![CDATA[<h1 id="gold-v-별-찍기---10---2447">[Gold V] 별 찍기 - 10 - 2447</h1>

<p><a href="https://www.acmicpc.net/problem/2447">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 61300 KB, 시간: 608 ms</p>

<h3 id="분류">분류</h3>

<p>분할 정복, 재귀</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 4월 13일 19:53:37</p>

<h3 id="문제-설명">문제 설명</h3>

<p>재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다.</p>

<p>크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이 하나씩 있는 패턴이다.</p>

<pre>***
* *
***</pre>

<p>N이 3보다 클 경우, 크기 N의 패턴은 공백으로 채워진 가운데의 (N/3)×(N/3) 정사각형을 크기 N/3의 패턴으로 둘러싼 형태이다. 예를 들어 크기 27의 패턴은 예제 출력 1과 같다.</p>

<h3 id="입력">입력</h3>

<p>첫째 줄에 N이 주어진다. N은 3의 거듭제곱이다. 즉 어떤 정수 k에 대해 N=3<sup>k</sup>이며, 이때 1 ≤ k &lt; 8이다.</p>

<h3 id="출력">출력</h3>

<p>첫째 줄부터 N번째 줄까지 별을 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package baekjoon.april2025

fun main() {
    val n = readln().toInt()
    val matrix = Array(n) { CharArray(n) { '*' } }

    fun loop(n: Int, currentX: Int, currentY: Int) {
        if (n == 3) {
            for (i in 0 until 3) {
                for (j in 0 until 3) {
                    matrix[currentX + i][currentY + j] = '*'
                }
            }
            matrix[currentX + 1][currentY + 1] = ' '
            return
        }

        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + i][currentY + j] = '*' } } // 1
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + i][currentY + (n/3 * 1) + j] = '*' } } // 2
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + i][currentY + (n/3 * 2) + j] = '*' } } // 3
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + (n/3 * 1) + i][currentY + (n/3 * 0) + j] = '*' } } // 4
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + (n/3 * 1) + i][currentY + (n/3 * 1) + j] = ' ' } } // 5
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + (n/3 * 1) + i][currentY + (n/3 * 2) + j] = '*' } } // 6
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + (n/3 * 2) + i][currentY + (n/3 * 0) + j] = '*' } } // 7
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + (n/3 * 2) + i][currentY + (n/3 * 1) + j] = '*' } } // 8
        for (i in 0 until n/3) { for (j in 0 until n/3) { matrix[currentX + (n/3 * 2) + i][currentY + (n/3 * 2) + j] = '*' } } // 9


        for (i in 0 until 3) {
            for (j in 0 until 3) {
                if (i == 1 &amp;&amp; j == 1) continue
                loop(n/3, currentX + ((n/3) * i), currentY + ((n/3) * j))
            }
        }
    }

    loop(n, 0, 0)
    for (row in matrix) {
        println(row.joinToString(""))
    }
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Gold V] 별 찍기 - 10 - 2447]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 1992</title><link href="https://02ggang9.github.io/algorithm/baekjoon1992/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 1992" /><published>2025-04-12T00:00:00+00:00</published><updated>2025-04-12T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon1992</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon1992/"><![CDATA[<h1 id="silver-i-쿼드트리---1992">[Silver I] 쿼드트리 - 1992</h1>

<p><a href="https://www.acmicpc.net/problem/1992">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 28068 KB, 시간: 208 ms</p>

<h3 id="분류">분류</h3>

<p>분할 정복, 재귀</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 4월 12일 23:10:12</p>

<h3 id="문제-설명">문제 설명</h3>

<p>흑백 영상을 압축하여 표현하는 데이터 구조로 쿼드 트리(Quad Tree)라는 방법이 있다. 흰 점을 나타내는 0과 검은 점을 나타내는 1로만 이루어진 영상(2차원 배열)에서 같은 숫자의 점들이 한 곳에 많이 몰려있으면, 쿼드 트리에서는 이를 압축하여 간단히 표현할 수 있다.</p>

<p>주어진 영상이 모두 0으로만 되어 있으면 압축 결과는 "0"이 되고, 모두 1로만 되어 있으면 압축 결과는 "1"이 된다. 만약 0과 1이 섞여 있으면 전체를 한 번에 나타내지를 못하고, 왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래, 이렇게 4개의 영상으로 나누어 압축하게 되며, 이 4개의 영역을 압축한 결과를 차례대로 괄호 안에 묶어서 표현한다</p>

<p style="text-align: center;"><img alt="" height="186" src="" width="408" /></p>

<p>위 그림에서 왼쪽의 영상은 오른쪽의 배열과 같이 숫자로 주어지며, 이 영상을 쿼드 트리 구조를 이용하여 압축하면 "<code>(0(0011)(0(0111)01)1)</code>"로 표현된다.  N ×N 크기의 영상이 주어질 때, 이 영상을 압축한 결과를 출력하는 프로그램을 작성하시오.</p>

<h3 id="입력">입력</h3>

<p>첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1 ≤ N ≤ 64의 범위를 가진다. 두 번째 줄부터는 길이 N의 문자열이 N개 들어온다. 각 문자열은 0 또는 1의 숫자로 이루어져 있으며, 영상의 각 점들을 나타낸다.</p>

<h3 id="출력">출력</h3>

<p>영상을 압축한 결과를 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package baekjoon.april2025

fun main() {
    val n = readln().toInt()
    val matrix = Array(n) { readln().split("").filter { it.isNotEmpty() }.map { it.toInt() }}
    val result = StringBuilder()

    fun loop(n: Int, currentX: Int, currentY: Int) {
        val initNumber = matrix[currentX][currentY]
        for (i in 0 until n) {
            for (j in 0 until n) {
                if (matrix[currentX + i][currentY + j] != initNumber) {
                    result.append("(")
                    loop(n/2, currentX, currentY)
                    loop(n/2, currentX, currentY + n/2)
                    loop(n/2, currentX + n/2, currentY)
                    loop(n/2, currentX + n/2, currentY + n/2)
                    result.append(")")
                    return
                }
            }
        }

        result.append(if (initNumber == 1) 1 else 0)
    }

    loop(n, 0, 0)
    println(result.toString())
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver I] 쿼드트리 - 1992]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 2630</title><link href="https://02ggang9.github.io/algorithm/baekjoon2630/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 2630" /><published>2025-04-12T00:00:00+00:00</published><updated>2025-04-12T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon2630</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon2630/"><![CDATA[<h1 id="silver-ii-색종이-만들기---2630">[Silver II] 색종이 만들기 - 2630</h1>

<p><a href="https://www.acmicpc.net/problem/2630">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 24032 KB, 시간: 192 ms</p>

<h3 id="분류">분류</h3>

<p>분할 정복, 재귀</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 4월 12일 22:31:20</p>

<h3 id="문제-설명">문제 설명</h3>

<p>아래 &lt;그림 1&gt;과 같이 여러개의 정사각형칸들로 이루어진 정사각형 모양의 종이가 주어져 있고, 각 정사각형들은 하얀색으로 칠해져 있거나 파란색으로 칠해져 있다. 주어진 종이를 일정한 규칙에 따라 잘라서 다양한 크기를 가진 정사각형 모양의 하얀색 또는 파란색 색종이를 만들려고 한다.</p>

<p style="text-align: center;"><img alt="" src="https://www.acmicpc.net/upload/images/bwxBxc7ghGOedQfiT3p94KYj1y9aLR.png" style="height:221px; width:215px" /></p>

<p>전체 종이의 크기가 N×N(N=2<sup>k</sup>, k는 1 이상 7 이하의 자연수) 이라면 종이를 자르는 규칙은 다음과 같다.</p>

<p>전체 종이가 모두 같은 색으로 칠해져 있지 않으면 가로와 세로로 중간 부분을 잘라서 &lt;그림 2&gt;의 I, II, III, IV와 같이 똑같은 크기의 네 개의 N/2 × N/2색종이로 나눈다. 나누어진 종이 I, II, III, IV 각각에 대해서도 앞에서와 마찬가지로 모두 같은 색으로 칠해져 있지 않으면 같은 방법으로 똑같은 크기의 네 개의 색종이로 나눈다. 이와 같은 과정을 잘라진 종이가 모두 하얀색 또는 모두 파란색으로 칠해져 있거나, 하나의 정사각형 칸이 되어 더 이상 자를 수 없을 때까지 반복한다.</p>

<p>위와 같은 규칙에 따라 잘랐을 때 &lt;그림 3&gt;은 &lt;그림 1&gt;의 종이를 처음 나눈 후의 상태를, &lt;그림 4&gt;는 두 번째 나눈 후의 상태를, &lt;그림 5&gt;는 최종적으로 만들어진 다양한 크기의 9장의 하얀색 색종이와 7장의 파란색 색종이를 보여주고 있다.</p>

<p style="text-align: center;"><img alt="" src="" style="height:488px; width:487px" /></p>

<p>입력으로 주어진 종이의 한 변의 길이 N과 각 정사각형칸의 색(하얀색 또는 파란색)이 주어질 때 잘라진 하얀색 색종이와 파란색 색종이의 개수를 구하는 프로그램을 작성하시오.</p>

<h3 id="입력">입력</h3>

<p>첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다. 하얀색으로 칠해진 칸은 0, 파란색으로 칠해진 칸은 1로 주어지며, 각 숫자 사이에는 빈칸이 하나씩 있다.</p>

<h3 id="출력">출력</h3>

<p>첫째 줄에는 잘라진 햐얀색 색종이의 개수를 출력하고, 둘째 줄에는 파란색 색종이의 개수를 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package baekjoon.april2025

fun main() {
    val n = readln().toInt()
    val matrix = Array(n) { readln().split(" ").map { it.toInt() } }

    var white = 0
    var blue = 0

    fun loop(n: Int, currentX: Int, currentY: Int) {
        val initialNumber = matrix[currentX][currentY]
        for (i in 0 until n) {
            for (j in 0 until n) {
                if (matrix[currentX + i][currentY + j] != initialNumber) {
                    loop(n/2, currentX, currentY)
                    loop(n/2, currentX + n/2, currentY)
                    loop(n/2, currentX, currentY + n/2)
                    loop(n/2, currentX + n/2, currentY + n/2)
                    return
                }
            }
        }

        if (initialNumber == 1) white += 1 else blue += 1
    }

    loop(n, 0, 0)
    println(blue)
    println(white)
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Silver II] 색종이 만들기 - 2630]]></summary></entry><entry><title type="html">[알고리즘] 백준 - 1074</title><link href="https://02ggang9.github.io/algorithm/baekjoon1074/" rel="alternate" type="text/html" title="[알고리즘] 백준 - 1074" /><published>2025-04-06T00:00:00+00:00</published><updated>2025-04-06T00:00:00+00:00</updated><id>https://02ggang9.github.io/algorithm/baekjoon1074</id><content type="html" xml:base="https://02ggang9.github.io/algorithm/baekjoon1074/"><![CDATA[<h1 id="gold-v-z---1074">[Gold V] Z - 1074</h1>

<p><a href="https://www.acmicpc.net/problem/1074">문제 링크</a></p>

<h3 id="성능-요약">성능 요약</h3>

<p>메모리: 21344 KB, 시간: 116 ms</p>

<h3 id="분류">분류</h3>

<p>분할 정복, 재귀</p>

<h3 id="제출-일자">제출 일자</h3>

<p>2025년 4월 6일 19:11:19</p>

<h3 id="문제-설명">문제 설명</h3>

<p>한수는 크기가 2<sup>N</sup> × 2<sup>N</sup>인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다.</p>

<p style="text-align:center"><img alt="" src="" style="width: 100px; height: 99px;" /></p>

<p>N &gt; 1인 경우, 배열을 크기가 2<sup>N-1</sup> × 2<sup>N-1</sup>로 4등분 한 후에 재귀적으로 순서대로 방문한다.</p>

<p>다음 예는 2<sup>2</sup> × 2<sup>2</sup> 크기의 배열을 방문한 순서이다.</p>

<p style="text-align:center"><img alt="" src="https://u.acmicpc.net/adc7cfae-e84d-4d5c-af8e-ee011f8fff8f/Screen%20Shot%202020-12-02%20at%208.11.17%20AM.png" style="width: 250px; height: 252px;" /></p>

<p>N이 주어졌을 때, r행 c열을 몇 번째로 방문하는지 출력하는 프로그램을 작성하시오.</p>

<p>다음은 N=3일 때의 예이다.</p>

<p style="text-align:center"><img alt="" src="" style="width: 533px; height: 535px;" /></p>

<h3 id="입력">입력</h3>

<p>첫째 줄에 정수 N, r, c가 주어진다.</p>

<h3 id="출력">출력</h3>

<p>r행 c열을 몇 번째로 방문했는지 출력한다.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>package baekjoon.april2025

fun main() {
    val (n, r, c) = readln().split(" ").map { it.toInt() }

    fun recur(n: Int, r: Int, c: Int): Int {
        if (n == 0) return 0

        val half = (1 shl (n - 1))

        // 1번째 칸일 때
        if (r &lt; half &amp;&amp; c &lt; half) return recur(n-1, r, c)
        // 2번째 칸일 때
        if (r &lt; half &amp;&amp; c &gt;= half) return recur(n-1, r, c - half) + (half * half)
        // 3번째 칸일 때
        if (r &gt;= half &amp;&amp; c &lt; half) return recur(n-1, r - half, c) + (2 * half * half)
        // 4 번째 칸일 때
        return recur(n-1, r - half, c - half) + (3 * half * half)
    }

    println(recur(n, r, c))
}
</code></pre></div></div>]]></content><author><name>이수빈</name><email>02ggang9@gmail.com</email></author><category term="algorithm" /><summary type="html"><![CDATA[[Gold V] Z - 1074]]></summary></entry></feed>