<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>그래프 이론 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EA%B7%B8%EB%9E%98%ED%94%84-%EC%9D%B4%EB%A1%A0/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Thu, 17 Oct 2024 10:19:12 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>그래프 이론 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 16953번 (A → B, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 17 Oct 2024 08:40:22 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[16953번]]></category>
		<category><![CDATA[A → B]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Recursive]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그리디 알고리즘]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 16953번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[재귀]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=38376</guid>

					<description><![CDATA[<p>A → B https://www.acmicpc.net/problem/16953 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 512 MB 61243 25275 19996 39.758% 문제 정수 A를 B로 바꾸려고 한다. 가능한 연산은 다음과 같은 두 가지이다. A를 B로 바꾸는데 필요한 연산의 최솟값을 구해보자. 입력 첫째 줄에 A, B (1 ≤ A &#60; B ≤ 109)가 주어진다. 출력 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/">백준 16953번 (A → B, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading">A → B <img decoding="async" width="30" height="38" class="wp-image-37910" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/16953" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/16953</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">61243</td><td class="has-text-align-left" data-align="left">25275</td><td class="has-text-align-left" data-align="left">19996</td><td class="has-text-align-left" data-align="left">39.758%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p>정수 A를 B로 바꾸려고 한다. 가능한 연산은 다음과 같은 두 가지이다.</p>



<ul class="wp-block-list">
<li>2를 곱한다.</li>



<li>1을 수의 가장 오른쪽에 추가한다.&nbsp;</li>
</ul>



<p>A를 B로 바꾸는데 필요한 연산의 최솟값을 구해보자.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p>첫째 줄에 A, B (1 ≤ A &lt; B ≤ 10<sup>9</sup>)가 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p>A를 B로 바꾸는데 필요한 연산의 최솟값에 1을 더한 값을 출력한다. </p>



<p>만들 수 없는 경우에는 -1을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입/출력 1</h3>



<div class="wp-block-uagb-container uagb-block-ecb9f492 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-5a2a4110">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2 162</pre>
</div>



<div class="wp-block-uagb-container uagb-block-99e5d95d">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5</pre>
</div>
</div></div>



<p>2&nbsp;→ 4&nbsp;→ 8&nbsp;→ 81&nbsp;→ 162</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입/출력 2</h3>



<div class="wp-block-uagb-container uagb-block-1a4486a4 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-edc25381">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 42</pre>
</div>



<div class="wp-block-uagb-container uagb-block-7c5a70a3">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1</pre>
</div>
</div></div>



<p>2&nbsp;→ 4&nbsp;→ 8&nbsp;→ 81&nbsp;→ 162</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입/출력 3</h3>



<div class="wp-block-uagb-container uagb-block-789cb5d6 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-dd969038">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100 40021</pre>
</div>



<div class="wp-block-uagb-container uagb-block-32bc121e">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5</pre>
</div>
</div></div>



<p>100&nbsp;→ 200&nbsp;→ 2001&nbsp;→ 4002&nbsp;→ 40021</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7">그래프</a><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener"> </a><a href="https://www.acmicpc.net/problem/tag/7">이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/33" target="_blank" rel="noreferrer noopener">그리디 알고리즘</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>
</ul>



<div style="height:92px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<p>DFS(재귀)를 이용하여 해결</p>



<p>재귀를 수행하면서 조건을 만족할 수 없다면 Return</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
using namespace std;

int MinRes = INT16_MAX;
long long int Target = 0;

void Recursion(int _depth, long long int _val) {
    // 목표 값보다 크면 반환
    if (_val > Target)
        return;

    // 현재 깊이가 최소값보다 크면 반환
    if (_depth > MinRes)
        return;

    // 목표 값에 도달하면 최소 깊이 갱신
    if (Target == _val) {
        MinRes = min(MinRes, _depth);
        return;
    }

    // 재귀 호출을 통해 2를 곱하거나 1을 추가
    Recursion(_depth + 1, _val * 2);
    Recursion(_depth + 1, (_val * 10) + 1);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long int Start = 0;
    cin >> Start >> Target;
    
    // 초기 깊이는 1로 설정
    Recursion(1, Start);
    
    // 결과 출력: 만들 수 없는 경우 -1 출력
    if (INT16_MAX == MinRes)
        cout &lt;&lt; -1;
    else
        cout &lt;&lt; MinRes;
}
</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1039" height="113" src="https://lycos7560.com/wp-content/uploads/2024/10/image-11.png" alt="" class="wp-image-38377" srcset="https://lycos7560.com/wp-content/uploads/2024/10/image-11.png 1039w, https://lycos7560.com/wp-content/uploads/2024/10/image-11-300x33.png 300w, https://lycos7560.com/wp-content/uploads/2024/10/image-11-768x84.png 768w" sizes="(max-width: 1039px) 100vw, 1039px" /></figure>



<p>int =&gt; long long int</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/">백준 16953번 (A → B, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2146번 (다리 만들기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2146%eb%b2%88-%eb%8b%a4%eb%a6%ac-%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/37865/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2146%eb%b2%88-%eb%8b%a4%eb%a6%ac-%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/37865/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 25 Feb 2024 00:23:34 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2146]]></category>
		<category><![CDATA[2146번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold3]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드3]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다리 만들기]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2146]]></category>
		<category><![CDATA[백준 2146번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=37865</guid>

					<description><![CDATA[<p>백준 2146번 '다리 만들기' 문제에 대한 글입니다. BFS를 이용하여 섬을 구분해준 이후에 다시 BFS로 섬간의 최단 거리를 구하여 문제를 해결하였습니다. (This is about the issue of 'Building a Bridge' BaekJoon No. 2146. After classifying the islands using BFS, we solved the problem by finding the shortest distance between the islands again with BFS.)</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2146%eb%b2%88-%eb%8b%a4%eb%a6%ac-%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/37865/">백준 2146번 (다리 만들기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#다리-만들기" class="uagb-toc-link__trigger">다리 만들기</a><li class="uagb-toc__list"><a href="#추가-반례" class="uagb-toc-link__trigger">추가 반례</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">다리 만들기 <img decoding="async" width="30" height="38" class="wp-image-37891" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/02/Gold_3.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/02/Gold_3.jpg 1250w, https://lycos7560.com/wp-content/uploads/2024/02/Gold_3-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2024/02/Gold_3-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2024/02/Gold_3-1200x1536.jpg 1200w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/2146" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2146</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">192 MB</td><td class="has-text-align-left" data-align="left">42718</td><td class="has-text-align-left" data-align="left">15973</td><td class="has-text-align-left" data-align="left">9991</td><td class="has-text-align-left" data-align="left">34.260%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">문제</h3>



<p>여러 섬으로 이루어진 나라가 있다. </p>



<p>이 나라의 대통령은 섬을 잇는 다리를 만들겠다는 공약으로 인기몰이를 해 당선될 수 있었다. </p>



<p>하지만 막상 대통령에 취임하자, 다리를 놓는다는 것이 아깝다는 생각을 하게 되었다. </p>



<p>그래서 그는, 생색내는 식으로 한 섬과 다른 섬을 잇는 다리 하나만을 만들기로 하였고, 그 또한 다리를 가장 짧게 하여 돈을 아끼려 하였다.</p>



<p>이 나라는 N×N크기의 이차원 평면상에 존재한다. 이 나라는 여러 섬으로 이루어져 있으며, 섬이란 동서남북으로 육지가 붙어있는 덩어리를 말한다. </p>



<p>다음은 세 개의 섬으로 이루어진 나라의 지도이다.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="302" height="284" src="https://lycos7560.com/wp-content/uploads/2024/02/image-1.png" alt="" class="wp-image-37866" srcset="https://lycos7560.com/wp-content/uploads/2024/02/image-1.png 302w, https://lycos7560.com/wp-content/uploads/2024/02/image-1-300x282.png 300w" sizes="(max-width: 302px) 100vw, 302px" /></figure>



<p>위의 그림에서 색이 있는 부분이 육지이고, 색이 없는 부분이 바다이다. </p>



<p>이 바다에 가장 짧은 다리를 놓아 두 대륙을 연결하고자 한다. </p>



<p>가장 짧은 다리란, 다리가 격자에서 차지하는 칸의 수가 가장 작은 다리를 말한다. </p>



<p>다음 그림에서 두 대륙을 연결하는 다리를 볼 수 있다.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="301" height="271" src="https://lycos7560.com/wp-content/uploads/2024/02/image-3.png" alt="" class="wp-image-37868"/></figure>



<p>물론 위의 방법 외에도 다리를 놓는 방법이 여러 가지 있으나, 위의 경우가 놓는 다리의 길이가 3으로 가장 짧다</p>



<p>(물론 길이가 3인 다른 다리를 놓을 수 있는 방법도 몇 가지 있다).</p>



<p>지도가 주어질 때, 가장 짧은 다리 하나를 놓아 두 대륙을 연결하는 방법을 찾으시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">입력</h3>



<p>첫 줄에는 지도의 크기 N(100이하의 자연수)가 주어진다. </p>



<p>그 다음 N줄에는 N개의 숫자가 빈칸을 사이에 두고 주어지며, 0은 바다, 1은 육지를 나타낸다. </p>



<p>항상 두 개 이상의 섬이 있는 데이터만 입력으로 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출력</h3>



<p>첫째 줄에 가장 짧은 다리의 길이를 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">10
1 1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1 1
1 0 1 1 0 0 0 0 1 1
0 0 1 1 1 0 0 0 0 1
0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:113px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">추가 반례</h2>



<p><a href="https://www.acmicpc.net/board/search/all/problem/2146" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/board/search/all/problem/2146</a></p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
1 0 0 1
0 0 0 0
0 1 1 0
0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6
1 0 0 1 1 1
1 0 0 0 0 1
1 0 0 0 0 0
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 C</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
1 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 C</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출처</h3>



<ul class="wp-block-list">
<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/choiking10">cho</a><a href="https://www.acmicpc.net/user/choiking10" target="_blank" rel="noreferrer noopener">i</a><a href="https://www.acmicpc.net/user/choiking10">king10</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/hello70825" target="_blank" rel="noreferrer noopener">hello70825</a>,&nbsp;<a href="https://www.acmicpc.net/user/ohyuni" target="_blank" rel="noreferrer noopener">ohyuni</a></li>



<li>잘못된 데이터를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/tncks0121" target="_blank" rel="noreferrer noopener">tncks0121</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading">통과된 코드</h2>



<p>1. 맵을 순회하면서 BFS를 이용하여 섬을 구분</p>



<p>2. 섬마다 가장 가까운 다른 섬까지의 거리를 구함</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>

using namespace std;

constexpr int MAX = 100;
int _QSize;

int _MAP[MAX][MAX];
bool _IsVisted[MAX][MAX];

int DxDy[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

void ClearMap()
{
	for (int i = 0; i &lt; _QSize; i++)
		for (int j = 0; j &lt; _QSize; j++) {
			_IsVisted[i][j] = false;
			if (_MAP[i][j] &lt;= 0)
				_MAP[i][j] = 0;
		}

}

void SortOutTypesIslands(const int _cntSort, const int _startI, const int _startJ)
{
	queue&lt;pair&lt;int, int>> SortQueue;
	SortQueue.push({ _startI, _startJ });
	_MAP[_startI][_startJ] = _cntSort;
	while (!SortQueue.empty())
	{
		pair&lt;int, int> _cntXY = SortQueue.front();
		SortQueue.pop();

		for (int i = 0; i &lt; 4; i++) {
			int _Tx = _cntXY.first + DxDy[i][0];
			int _Ty = _cntXY.second + DxDy[i][1];

			if (_Tx >= _QSize || _Ty >= _QSize || _Tx &lt; 0 || _Ty &lt; 0)
				continue;
			
			if (_MAP[_Tx][_Ty] == 1) {
				_MAP[_Tx][_Ty] = _cntSort;
				SortQueue.push({ _Tx, _Ty });
			}

		}

	}
}


int FindShortBridge(queue&lt;pair&lt;int, int>>&amp; TypeQueue, const int _cntType)
{
	int _shortLen = INT32_MAX;

	while (!TypeQueue.empty())
	{
		pair&lt;int, int> _cntXY = TypeQueue.front();
		TypeQueue.pop();

		int _cnt;
		if (_MAP[_cntXY.first][_cntXY.second] == _cntType) _cnt = 0;
		else _cnt = _MAP[_cntXY.first][_cntXY.second];

		for (int i = 0; i &lt; 4; i++) {
			int _Tx = _cntXY.first + DxDy[i][0];
			int _Ty = _cntXY.second + DxDy[i][1];

			if (_Tx >= _QSize || _Ty >= _QSize || _Tx &lt; 0 || _Ty &lt; 0 || _IsVisted[_Tx][_Ty])
				continue;

			if (_MAP[_Tx][_Ty] != _cntType &amp;&amp; _MAP[_Tx][_Ty] >= 1) {
				if (_shortLen > -_cnt) 
					_shortLen = -_cnt;
				break;
			}

			if (_MAP[_Tx][_Ty] == 0) {
				TypeQueue.push({ _Tx, _Ty });
				_IsVisted[_Tx][_Ty] = true;
				_MAP[_Tx][_Ty] = _cnt - 1;

			}
		}

	}

	return _shortLen;
}


int main()
{ 
	int _res = INT32_MAX;
	cin >> _QSize;
	for (int i = 0; i &lt; _QSize; i++)
		for (int j = 0; j &lt; _QSize; j++)
			cin >> _MAP[i][j];

	int _Sort = 2;
	for (int i = 0; i &lt; _QSize; i++)
		for (int j = 0; j &lt; _QSize; j++)
			if (_MAP[i][j] == 1) {
				SortOutTypesIslands(_Sort, i, j);
				_Sort++;
			}

	while (_Sort != 1)
	{
		queue&lt;pair&lt;int, int>> TypeQueue;
		for (int i = 0; i &lt; _QSize; i++)
			for (int j = 0; j &lt; _QSize; j++)
				if (_MAP[i][j] == _Sort) {
					TypeQueue.push({ i, j });
					_IsVisted[i][j] = true;
				}

		ClearMap();
		_res = min(_res, FindShortBridge(TypeQueue, _Sort));
		_Sort--;
	}

	cout &lt;&lt; _res;

	return 0;
}</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1201" height="51" src="https://lycos7560.com/wp-content/uploads/2024/02/image-4.png" alt="" class="wp-image-37869" srcset="https://lycos7560.com/wp-content/uploads/2024/02/image-4.png 1201w, https://lycos7560.com/wp-content/uploads/2024/02/image-4-300x13.png 300w, https://lycos7560.com/wp-content/uploads/2024/02/image-4-768x33.png 768w" sizes="(max-width: 1201px) 100vw, 1201px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>섬의 좌표를 구분단계에서 미리 저장하여 비효율적인 부분을 제거</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>

using namespace std;

constexpr int MAX = 100;
int _QSize;

int _MAP[MAX][MAX];
bool _IsVisted[MAX][MAX];

int DxDy[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

void ClearMap()
{
	for (int i = 0; i &lt; _QSize; i++)
		for (int j = 0; j &lt; _QSize; j++) {
			_IsVisted[i][j] = false;
			if (_MAP[i][j] &lt;= 0)
				_MAP[i][j] = 0;
		}

}

queue&lt;pair&lt;unsigned char, unsigned char>> SortOutTypesIslands(const int _cntSort, const int _startI, const int _startJ)
{
	queue&lt;pair&lt;unsigned char, unsigned char>> TotalPosQueue;

	queue&lt;pair&lt;int, int>> SortQueue;
	SortQueue.push({ _startI, _startJ });
	TotalPosQueue.push({ _startI, _startJ });
	_MAP[_startI][_startJ] = _cntSort;
	while (!SortQueue.empty())
	{
		pair&lt;int, int> _cntXY = SortQueue.front();
		SortQueue.pop();

		for (int i = 0; i &lt; 4; i++) {
			int _Tx = _cntXY.first + DxDy[i][0];
			int _Ty = _cntXY.second + DxDy[i][1];

			if (_Tx >= _QSize || _Ty >= _QSize || _Tx &lt; 0 || _Ty &lt; 0)
				continue;

			if (_MAP[_Tx][_Ty] == 1) {
				_MAP[_Tx][_Ty] = _cntSort;
				SortQueue.push({ _Tx, _Ty });
				TotalPosQueue.push({ _Tx, _Ty });
			}

		}

	}

	return TotalPosQueue;
}


int FindShortBridge(queue&lt;pair&lt;unsigned char, unsigned char>>&amp; TypeQueue, const int _cntType)
{
	int _shortLen = INT32_MAX;

	while (!TypeQueue.empty())
	{
		pair&lt;int, int> _cntXY = TypeQueue.front();
		TypeQueue.pop();

		int _cnt;
		if (_MAP[_cntXY.first][_cntXY.second] == _cntType) _cnt = 0;
		else _cnt = _MAP[_cntXY.first][_cntXY.second];

		for (int i = 0; i &lt; 4; i++) {
			int _Tx = _cntXY.first + DxDy[i][0];
			int _Ty = _cntXY.second + DxDy[i][1];

			if (_Tx >= _QSize || _Ty >= _QSize || _Tx &lt; 0 || _Ty &lt; 0 || _IsVisted[_Tx][_Ty])
				continue;

			if (_MAP[_Tx][_Ty] != _cntType &amp;&amp; _MAP[_Tx][_Ty] >= 1) {
				if (_shortLen > -_cnt)
					_shortLen = -_cnt;
				break;
			}

			if (_MAP[_Tx][_Ty] == 0) {
				TypeQueue.push({ _Tx, _Ty });
				_IsVisted[_Tx][_Ty] = true;
				_MAP[_Tx][_Ty] = _cnt - 1;

			}
		}

	}

	return _shortLen;
}


int main()
{
	int _res = INT32_MAX;
	queue&lt;queue&lt;pair&lt;unsigned char, unsigned char>>> TotalQueue;
	cin >> _QSize;
	for (int i = 0; i &lt; _QSize; i++)
		for (int j = 0; j &lt; _QSize; j++)
			cin >> _MAP[i][j];

	int _Sort = 2;
	for (int i = 0; i &lt; _QSize; i++)
		for (int j = 0; j &lt; _QSize; j++)
			if (_MAP[i][j] == 1) {
				TotalQueue.push(SortOutTypesIslands(_Sort, i, j));
				_Sort++;
			}

	while (!TotalQueue.empty())
	{
		queue&lt;pair&lt;unsigned char, unsigned char>> TypeQueue = TotalQueue.front();
		_Sort = _MAP[TypeQueue.front().first][TypeQueue.front().second];
		TotalQueue.pop();
		ClearMap();
		_res = min(_res, FindShortBridge(TypeQueue, _Sort));
	}

	cout &lt;&lt; _res;

	return 0;
}</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1035" height="53" src="https://lycos7560.com/wp-content/uploads/2024/02/image-5.png" alt="" class="wp-image-37870" srcset="https://lycos7560.com/wp-content/uploads/2024/02/image-5.png 1035w, https://lycos7560.com/wp-content/uploads/2024/02/image-5-300x15.png 300w, https://lycos7560.com/wp-content/uploads/2024/02/image-5-768x39.png 768w" sizes="(max-width: 1035px) 100vw, 1035px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1203" height="360" src="https://lycos7560.com/wp-content/uploads/2024/02/image-6.png" alt="" class="wp-image-37871" srcset="https://lycos7560.com/wp-content/uploads/2024/02/image-6.png 1203w, https://lycos7560.com/wp-content/uploads/2024/02/image-6-300x90.png 300w, https://lycos7560.com/wp-content/uploads/2024/02/image-6-768x230.png 768w" sizes="(max-width: 1203px) 100vw, 1203px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2146%eb%b2%88-%eb%8b%a4%eb%a6%ac-%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/37865/">백준 2146번 (다리 만들기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2146%eb%b2%88-%eb%8b%a4%eb%a6%ac-%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/37865/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2583번 (영역 구하기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2583%eb%b2%88-%ec%98%81%ec%97%ad-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36841/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2583%eb%b2%88-%ec%98%81%ec%97%ad-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36841/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 20 Sep 2023 06:27:49 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2583]]></category>
		<category><![CDATA[2583번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그래프이론]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[깊이]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2583]]></category>
		<category><![CDATA[백준 2583번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[영역 구하기]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36841</guid>

					<description><![CDATA[<p>백준 2583번 '영역 구하기' 문제에 대한 글입니다. BFS 알고리즘을 이용하여 해결하였습니다. (This article is about Baekjoon Number 2583 'Saving Areas'. It was solved using the BFS algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2583%eb%b2%88-%ec%98%81%ec%97%ad-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36841/">백준 2583번 (영역 구하기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#영역-구하기" class="uagb-toc-link__trigger">영역 구하기</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">영역 구하기 <img decoding="async" width="30" height="38" class="wp-image-37895" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1.jpg 1250w, https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1-1200x1536.jpg 1200w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/2583" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2583</a></p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">41254</td><td class="has-text-align-left" data-align="left">23543</td><td class="has-text-align-left" data-align="left">18463</td><td class="has-text-align-left" data-align="left">57.499%</td></tr></tbody></table></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>눈금의 간격이 1인 M×N(M,N≤100)크기의 모눈종이가 있다. </p>



<p>이 모눈종이 위에 눈금에 맞추어 K개의 직사각형을 그릴 때, </p>



<p>이들 K개의 직사각형의 내부를 제외한 나머지 부분이 몇 개의 분리된 영역으로 나누어진다.</p>



<p>예를 들어 M=5, N=7 인 모눈종이 위에 &lt;그림 1&gt;과 같이 직사각형 3개를 그렸다면, </p>



<p>그 나머지 영역은 &lt;그림 2&gt;와 같이 3개의 분리된 영역으로 나누어지게 된다.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1148" height="371" src="https://lycos7560.com/wp-content/uploads/2023/09/image-33.png" alt="" class="wp-image-36842" srcset="https://lycos7560.com/wp-content/uploads/2023/09/image-33.png 1148w, https://lycos7560.com/wp-content/uploads/2023/09/image-33-300x97.png 300w, https://lycos7560.com/wp-content/uploads/2023/09/image-33-768x248.png 768w" sizes="(max-width: 1148px) 100vw, 1148px" /></figure>



<p>&lt;그림 2&gt;와 같이 분리된 세 영역의 넓이는 각각 1, 7, 13이 된다.</p>



<p>M, N과 K 그리고 K개의 직사각형의 좌표가 주어질 때, </p>



<p>K개의 직사각형 내부를 제외한 나머지 부분이 몇 개의 분리된 영역으로 나누어지는지, </p>



<p>그리고 분리된 각 영역의 넓이가 얼마인지를 구하여 이를 출력하는 프로그램을 작성하시오.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. </p>



<p>M, N, K는 모두 100 이하의 자연수이다. </p>



<p>둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 </p>



<p>오른쪽 위 꼭짓점의 x, y좌표값이 빈칸을 사이에 두고 차례로 주어진다. </p>



<p>모눈종이의 왼쪽 아래 꼭짓점의 좌표는 (0,0)이고, 오른쪽 위 꼭짓점의 좌표는(N,M)이다. </p>



<p>입력되는 K개의 직사각형들이 모눈종이 전체를 채우는 경우는 없다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에 분리되어 나누어지는 영역의 개수를 출력한다. </p>



<p>둘째 줄에는 각 영역의 넓이를 오름차순으로 정렬하여 빈칸을 사이에 두고 출력한다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 7 3
0 2 4 4
1 1 2 5
4 0 6 2</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
1 7 13</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/70" target="_blank" rel="noreferrer noopener">지역본선 2006</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/369" target="_blank" rel="noreferrer noopener">고등부</a>&nbsp;2번</p>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/alphago92" target="_blank" rel="noreferrer noopener">alphago92</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/hjhj97">hjhj97</a></li>



<li>잘못된 데이터를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/kookmin20103324" target="_blank" rel="noreferrer noopener">kookmin20103324</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/127" target="_blank" rel="noreferrer noopener">깊이 우선 탐색</a></li>
</ul>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
#include &lt;vector>
#include &lt;algorithm>

using namespace std;

int _M, _N, _K, _DxDy[4][2] = {{1,0}, {-1,0}, {0, 1}, {0,-1}};
bool Table[100][100] = { false };
vector&lt;int> ResVector;

int BFS(int _x, int _y)
{
	int _Area = 1;
	queue&lt;pair&lt;int, int>> BFSQueue;
	Table[_x][_y] = true;
	BFSQueue.push({_x, _y});
	while (!BFSQueue.empty()) {
		pair&lt;int, int> _cnt = BFSQueue.front();
		BFSQueue.pop();
		for (int i = 0; i &lt; 4; i++) {
			int _dx = _cnt.first + _DxDy[i][0];
			int _dy = _cnt.second + _DxDy[i][1];
			if (_dx &lt; 0 || _dy &lt; 0 || _dx >= _M|| _dy >= _N)
				continue;
			if (Table[_dx][_dy] == true)
				continue;
			BFSQueue.push({ _dx, _dy });
			Table[_dx][_dy] = true;
			_Area++;
		}
	}

	return _Area;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> _M >> _N >> _K;

	for (int i = 0; i &lt; _K; i++) {
		int _x1, _y1, _x2, _y2;
		cin >> _x1 >> _y1 >> _x2 >> _y2;
		for (int j = _x1; j &lt; _x2; j++)
			for (int k = _y1; k &lt; _y2; k++)
				Table[k][j] = true;
	}

	for (int i = 0; i &lt; _M; i++)
		for (int j = 0; j &lt; _N; j++) 
			if (Table[i][j] == false)
				ResVector.push_back(BFS(i, j));
	cout &lt;&lt; ResVector.size() &lt;&lt; "\n";
	sort(ResVector.begin(), ResVector.end());
	for (int i = 0; i &lt; ResVector.size(); i++)
		cout &lt;&lt; ResVector[i] &lt;&lt; " ";
	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1202" height="97" src="https://lycos7560.com/wp-content/uploads/2023/09/image-34.png" alt="" class="wp-image-36843" srcset="https://lycos7560.com/wp-content/uploads/2023/09/image-34.png 1202w, https://lycos7560.com/wp-content/uploads/2023/09/image-34-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/09/image-34-768x62.png 768w" sizes="(max-width: 1202px) 100vw, 1202px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2583%eb%b2%88-%ec%98%81%ec%97%ad-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36841/">백준 2583번 (영역 구하기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2583%eb%b2%88-%ec%98%81%ec%97%ad-%ea%b5%ac%ed%95%98%ea%b8%b0-c-baekjoon/36841/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2636번 (치즈, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2636%eb%b2%88-%ec%b9%98%ec%a6%88-c-baekjoon/36604/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2636%eb%b2%88-%ec%b9%98%ec%a6%88-c-baekjoon/36604/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 26 Aug 2023 02:52:29 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2636]]></category>
		<category><![CDATA[2636번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold4]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드4]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구현]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그래프이론]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2636]]></category>
		<category><![CDATA[백준 2636번]]></category>
		<category><![CDATA[시뮬레이션]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[치즈]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36604</guid>

					<description><![CDATA[<p>백준 2636번 '치즈' 문제에 대한 글입니다. BFS 알고리즘을 이용하여 해결하였습니다.(This article is about the question of "Cheese" in Baekjoon Number 2636. It was solved using the BFS algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2636%eb%b2%88-%ec%b9%98%ec%a6%88-c-baekjoon/36604/">백준 2636번 (치즈, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#치즈" class="uagb-toc-link__trigger">치즈</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">치즈 <img decoding="async" width="30" height="38" class="wp-image-37904" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/2636" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2636</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">25672</td><td class="has-text-align-left" data-align="left">14240</td><td class="has-text-align-left" data-align="left">10335</td><td class="has-text-align-left" data-align="left">54.602%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>아래 &lt;그림 1&gt;과 같이 정사각형 칸들로 이루어진 사각형 모양의 판이 있고, </p>



<p>그 위에 얇은 치즈(회색으로 표시된 부분)가 놓여 있다. </p>



<p>판의 가장자리(&lt;그림 1&gt;에서 네모 칸에 X친 부분)에는 치즈가 놓여 있지 않으며 치즈에는 하나 이상의 구멍이 있을 수 있다.</p>



<p>이 치즈를 공기 중에 놓으면 녹게 되는데 공기와 접촉된 칸은 한 시간이 지나면 녹아 없어진다. </p>



<p>치즈의 구멍 속에는 공기가 없지만 구멍을 둘러싼 치즈가 녹아서 구멍이 열리면 구멍 속으로 공기가 들어가게 된다. </p>



<p>&lt;그림 1&gt;의 경우, 치즈의 구멍을 둘러싼 치즈는 녹지 않고 ‘c’로 표시된 부분만 한 시간 후에 녹아 없어져서 &lt;그림 2&gt;와 같이 된다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="379" height="408" src="https://lycos7560.com/wp-content/uploads/2023/08/image-25.png" alt="" class="wp-image-36609" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-25.png 379w, https://lycos7560.com/wp-content/uploads/2023/08/image-25-279x300.png 279w" sizes="(max-width: 379px) 100vw, 379px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>다시 한 시간 후에는 &lt;그림 2&gt;에서 ‘c’로 표시된 부분이 녹아 없어져서 &lt;그림 3&gt;과 같이 된다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="371" height="860" src="https://lycos7560.com/wp-content/uploads/2023/08/image-26.png" alt="" class="wp-image-36610" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-26.png 371w, https://lycos7560.com/wp-content/uploads/2023/08/image-26-129x300.png 129w" sizes="(max-width: 371px) 100vw, 371px" /></figure>



<p>&lt;그림 3&gt;은 원래 치즈의 두 시간 후 모양을 나타내고 있으며, </p>



<p>남은 조각들은 한 시간이 더 지나면 모두&nbsp;녹아 없어진다. </p>



<p>그러므로 처음 치즈가 모두 녹아 없어지는 데는 세 시간이 걸린다. </p>



<p>&lt;그림 3&gt;과 같이 치즈가 녹는 과정에서 여러 조각으로 나누어 질 수도 있다.</p>



<p>입력으로 사각형 모양의 판의 크기와 한 조각의 치즈가 판 위에 주어졌을 때, </p>



<p><strong>공기 중에서 치즈가 모두 녹아 없어지는 데 걸리는 시간과 </strong></p>



<p><strong>모두 녹기 한 시간 전에 남아있는 치즈조각이 놓여 있는 칸의 개수를 구하는 프로그램을 작성하시오.</strong></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫째 줄에는 사각형 모양 판의 세로와 가로의 길이가 양의 정수로 주어진다. </p>



<p>세로와 가로의 길이는 최대 100이다. </p>



<p>판의 각 가로줄의 모양이 윗 줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다. </p>



<p>치즈가 없는 칸은 0, 치즈가 있는 칸은 1로 주어지며 각 숫자 사이에는 빈칸이 하나씩 있다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에는 치즈가 모두 녹아서 없어지는 데 걸리는 시간을 출력하고, </p>



<p>둘째 줄에는 모두 녹기 한 시간 전에 남아있는 치즈조각이 놓여 있는 칸의 개수를 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">13 12
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0
0 1 1 1 0 0 0 1 1 0 0 0
0 1 1 1 1 1 1 0 0 0 0 0
0 1 1 1 1 1 0 1 1 0 0 0
0 1 1 1 1 0 0 1 1 0 0 0
0 0 1 1 0 0 0 1 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
5</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/2">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/78">KOI 2000</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/391">초등부</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/compro0317">compro0317</a>,&nbsp;<a href="https://www.acmicpc.net/user/shawn050912">shawn050912</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/jung2381187">jung2381187</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/102">구현</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/141">시뮬레이션</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126">너비 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
#include &lt;vector>
using namespace std;
constexpr short MAX = 100;
short _N, _M, _Cheese = 0;
short _DxDy[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
bool _Map[MAX][MAX];
bool _IsVisted[MAX][MAX];
vector&lt;short> Records;
vector&lt;pair&lt;short, short>> _Next;
queue&lt;pair&lt;short, short>> BFSqueue;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N >> _M;
	bool _t;
	for (short i = 0; i &lt; _N; i++)
		for (short j = 0; j &lt; _M; j++) {
			cin >> _t;
			_Map[i][j] = _t;
			if (_t == 1)
				_Cheese++;
		}
	short _time = 0;
	while (true) {
		Records.push_back(_Cheese);
		if (_Cheese == 0)
			break;
		for (short i = 0; i &lt; _N; i++)
			for (short j = 0; j &lt; _M; j++)
				_IsVisted[i][j] = false;
		_Next.clear();
		BFSqueue.push({ 0,0 });
		_IsVisted[0][0] = true;
		while (!BFSqueue.empty()) {
			pair&lt;short, short> _pos = BFSqueue.front();
			BFSqueue.pop();
			for (short i = 0; i &lt; 4; i++) {
				short _dx = _pos.first + _DxDy[i][0];
				short _dy = _pos.second + _DxDy[i][1];

				if (_dx &lt; 0 || _dy &lt; 0 || _dx >= _N || _dy >= _M)
					continue;
				if (_IsVisted[_dx][_dy])
					continue;
				if (_Map[_dx][_dy] == 1) {
					_Next.push_back({ _dx, _dy });
					_IsVisted[_dx][_dy] = true;
					continue;
				}

				_IsVisted[_dx][_dy] = true;
				BFSqueue.push({ _dx, _dy});
			}
		}

		for (auto&amp; it : _Next) {
			_Map[it.first][it.second] = 0;
			_Cheese--;
		}

		_time++;
	}

	cout &lt;&lt; _time &lt;&lt; "\n" &lt;&lt; Records[Records.size() - 2];


	return 0;
}</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1063" height="86" src="https://lycos7560.com/wp-content/uploads/2023/08/image-24.png" alt="" class="wp-image-36605" style="aspect-ratio:12.28;width:1045px;height:auto" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-24.png 1063w, https://lycos7560.com/wp-content/uploads/2023/08/image-24-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-24-768x62.png 768w" sizes="(max-width: 1063px) 100vw, 1063px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2636%eb%b2%88-%ec%b9%98%ec%a6%88-c-baekjoon/36604/">백준 2636번 (치즈, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2636%eb%b2%88-%ec%b9%98%ec%a6%88-c-baekjoon/36604/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2573번 (빙산, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2573%eb%b2%88-%eb%b9%99%ec%82%b0-c-baekjoon/36585/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2573%eb%b2%88-%eb%b9%99%ec%82%b0-c-baekjoon/36585/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 25 Aug 2023 16:57:28 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2573]]></category>
		<category><![CDATA[2573번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold4]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드4]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구현]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그래프이론]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[깊이]]></category>
		<category><![CDATA[깊이 우선 탐색]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2573]]></category>
		<category><![CDATA[백준 2573번]]></category>
		<category><![CDATA[빙산]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36585</guid>

					<description><![CDATA[<p>백준 2573번 '빙산' 문제에 대한 글입니다. BFS 알고리즘을 이용하여 해결하였습니다. (This article is about the issue of "ice mountain" in Baekjoon Number 2573. It was solved using the BFS algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2573%eb%b2%88-%eb%b9%99%ec%82%b0-c-baekjoon/36585/">백준 2573번 (빙산, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#빙산" class="uagb-toc-link__trigger">빙산</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">빙산 <img decoding="async" width="30" height="38" class="wp-image-37904" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/2573" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2573</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">66795</td><td class="has-text-align-left" data-align="left">18943</td><td class="has-text-align-left" data-align="left">12600</td><td class="has-text-align-left" data-align="left">25.842%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>지구 온난화로 인하여 북극의 빙산이 녹고 있다. </p>



<p>빙산을 그림 1과 같이 2차원 배열에 표시한다고 하자. </p>



<p>빙산의 각 부분별 높이 정보는 배열의 각 칸에 양의 정수로 저장된다. </p>



<p>빙산 이외의 바다에 해당되는 칸에는 0이 저장된다. </p>



<p>그림 1에서 빈칸은 모두 0으로 채워져 있다고 생각한다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="598" height="362" src="https://lycos7560.com/wp-content/uploads/2023/08/image-22.png" alt="" class="wp-image-36590" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-22.png 598w, https://lycos7560.com/wp-content/uploads/2023/08/image-22-300x182.png 300w" sizes="(max-width: 598px) 100vw, 598px" /><figcaption class="wp-element-caption">그림 1. 행의 개수가 5이고 열의 개수가 7인 2차원 배열에 저장된 빙산의 높이 정보</figcaption></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>빙산의 높이는 바닷물에 많이 접해있는 부분에서 더 빨리 줄어들기 때문에, </p>



<p>배열에서 빙산의 각 부분에 해당되는 칸에 있는 높이는 일년마다 그 칸에 동서남북 네 방향으로 붙어있는 0이 저장된 칸의 개수만큼 줄어든다. </p>



<p>단, 각 칸에 저장된 높이는 0보다 더 줄어들지 않는다. </p>



<p>바닷물은 호수처럼 빙산에 둘러싸여 있을 수도 있다. </p>



<p>따라서 그림 1의 빙산은 일년후에 그림 2와 같이 변형된다.</p>



<p>그림 3은 그림 1의 빙산이 2년 후에 변한 모습을 보여준다. </p>



<p>2차원 배열에서 동서남북 방향으로 붙어있는 칸들은 서로 연결되어 있다고 말한다. </p>



<p>따라서 그림 2의 빙산은 한 덩어리이지만, 그림 3의 빙산은 세 덩어리로 분리되어 있다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="636" height="900" src="https://lycos7560.com/wp-content/uploads/2023/08/image-23.png" alt="" class="wp-image-36593" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-23.png 636w, https://lycos7560.com/wp-content/uploads/2023/08/image-23-212x300.png 212w" sizes="(max-width: 636px) 100vw, 636px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>한 덩어리의 빙산이 주어질 때, 이 빙산이 두 덩어리 이상으로 분리되는 최초의 시간(년)을 구하는 프로그램을 작성하시오. </p>



<p>그림 1의 빙산에 대해서는 2가 답이다. </p>



<p>만일 전부 다 녹을 때까지 두 덩어리 이상으로 분리되지 않으면 프로그램은 0을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫 줄에는 이차원 배열의 행의 개수와 열의 개수를 나타내는 두 정수 N과 M이 한 개의 빈칸을 사이에 두고 주어진다. </p>



<p>N과 M은 3 이상 300 이하이다. 그 다음 N개의 줄에는 각 줄마다 배열의 각 행을 나타내는 M개의 정수가 한 개의 빈 칸을 사이에 두고 주어진다. </p>



<p>각 칸에 들어가는 값은 0 이상 10 이하이다. </p>



<p>배열에서 빙산이 차지하는 칸의 개수, 즉, 1 이상의 정수가 들어가는 칸의 개수는 10,000 개 이하이다. </p>



<p>배열의 첫 번째 행과 열, 마지막 행과 열에는 항상 0으로 채워진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫 줄에 빙산이 분리되는 최초의 시간(년)을 출력한다. </p>



<p>만일 빙산이 다 녹을 때까지 분리되지 않으면 0을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 7
0 0 0 0 0 0 0
0 2 4 5 3 0 0
0 3 0 2 5 2 0
0 7 6 2 4 0 0
0 0 0 0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 7
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 0 0 0 0 
0 0 0 0 0 0 0
0 0 0 0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7 7
0 0 0 0 0 0 0
0 1 1 0 1 1 0
0 1 9 1 9 1 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/69" target="_blank" rel="noreferrer noopener">KOI 2006</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/364">초등부</a>&nbsp;2번</p>



<ul class="wp-block-list">
<li>잘못된 데이터를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/occidere" target="_blank" rel="noreferrer noopener">occidere</a>,&nbsp;<a href="https://www.acmicpc.net/user/zayne" target="_blank" rel="noreferrer noopener">zayne</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/102">구현</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126">너비 우선 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/127">깊이 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
using namespace std;
constexpr short MAX = 300;
short _MAP[2][MAX][MAX];
bool _IsVisted[MAX][MAX];
short _N, _M, _Res = 0, _Glaciers = 0, _Cluster = 0;
short _DxDy[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

void BFS(short _i, short _j)
{
	queue&lt;pair&lt;short, short>> BFSQueue;
	BFSQueue.push({ _i, _j });
	while (!BFSQueue.empty()) {
		pair&lt;short, short> _CntPos = BFSQueue.front();
		BFSQueue.pop();
		short _Sea = 0;
		for (short i = 0; i &lt; 4; i++) {
			short _dx = _CntPos.first + _DxDy[i][0];
			short _dy = _CntPos.second + _DxDy[i][1];
			if (_dx &lt; 0 || _dy &lt; 0 || _dx >= _N || _dy >= _M)
				continue;
			if (_MAP[0][_dx][_dy] &lt;= 0) {
				_Sea++;
				continue;
			}
			if (_IsVisted[_dx][_dy])
				continue;
			_IsVisted[_dx][_dy] = true;
			BFSQueue.push({ _dx, _dy });
		}
		_MAP[1][_CntPos.first][_CntPos.second] = _Sea;
	}
}


short ClusterCount()
{
	short _cnt = 0;
	for (short i = 0; i &lt; _N; i++)
		for (short j = 0; j &lt; _M; j++)
			_IsVisted[i][j] = false;
	for (short i = 0; i &lt; _N; i++)
		for (short j = 0; j &lt; _M; j++) {
			if (_MAP[0][i][j] &lt;= 0 || _IsVisted[i][j])
				continue;
			BFS(i, j);
			_cnt++;
		}
	return _cnt;
}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N >> _M;
	short _Temp;
	for (short i = 0; i &lt; _N; i++)
		for (short j = 0; j &lt; _M; j++) {
			cin >> _Temp;
			if (_Temp > 0)
				_Glaciers++;
			_MAP[0][i][j] = _Temp;
		}
	short _Cnt = 0;
	while (true) {
		_Cluster = ClusterCount();
		if (_Cluster >= 2 ) {
			_Res = _Cnt;
			break;
		}
		if (_Glaciers &lt;= 1) {
			_Res = 0; 
			break;
		}

		for (short i = 0; i &lt; _N; i++)
			for (short j = 0; j &lt; _M; j++) {
				if (_MAP[0][i][j] &lt;= 0)
					continue;
				_MAP[0][i][j] -= _MAP[1][i][j];
				if (_MAP[0][i][j] &lt;= 0)
					_Glaciers--;
			}
		_Cnt++;
	}

	cout &lt;&lt; _Res;

	return 0;
}</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1205" height="190" src="https://lycos7560.com/wp-content/uploads/2023/08/image-21.png" alt="" class="wp-image-36587" style="width:867px;height:137px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-21.png 1205w, https://lycos7560.com/wp-content/uploads/2023/08/image-21-300x47.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-21-768x121.png 768w" sizes="(max-width: 1205px) 100vw, 1205px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2573%eb%b2%88-%eb%b9%99%ec%82%b0-c-baekjoon/36585/">백준 2573번 (빙산, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2573%eb%b2%88-%eb%b9%99%ec%82%b0-c-baekjoon/36585/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 5639번 (이진 검색 트리, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5639%eb%b2%88-%ec%9d%b4%ec%a7%84-%ea%b2%80%ec%83%89-%ed%8a%b8%eb%a6%ac-c-baekjoon/36556/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5639%eb%b2%88-%ec%9d%b4%ec%a7%84-%ea%b2%80%ec%83%89-%ed%8a%b8%eb%a6%ac-c-baekjoon/36556/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 23 Aug 2023 04:20:19 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[5639]]></category>
		<category><![CDATA[5639번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[endoffile]]></category>
		<category><![CDATA[EOF]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold5]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드5]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 5639]]></category>
		<category><![CDATA[백준 5639번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[이진 검색 트리]]></category>
		<category><![CDATA[재귀]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트리]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36556</guid>

					<description><![CDATA[<p>백준 온라인 저지 5639번 문제 '이진 검색 트리' 에 대한 글입니다. 단순한 재귀를 이용하여 해결하였습니다. (Baekjoon Online Judge Number 5639 This article is about the binary search tree. It was solved using simple recursion.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5639%eb%b2%88-%ec%9d%b4%ec%a7%84-%ea%b2%80%ec%83%89-%ed%8a%b8%eb%a6%ac-c-baekjoon/36556/">백준 5639번 (이진 검색 트리, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#이진-검색-트리" class="uagb-toc-link__trigger">이진 검색 트리</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">이진 검색 트리 <img decoding="async" width="30" height="38" class="wp-image-37922" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-1.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-1.jpg 1250w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-1-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-1-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-1-1200x1536.jpg 1200w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/5639" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/5639</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">35442</td><td class="has-text-align-left" data-align="left">13888</td><td class="has-text-align-left" data-align="left">9821</td><td class="has-text-align-left" data-align="left">38.260%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>이진 검색 트리는 다음과 같은 세 가지 조건을 만족하는 이진 트리이다.</p>



<ul class="wp-block-list">
<li>노드의 왼쪽 서브트리에 있는 모든 노드의 키는 노드의 키보다 작다.</li>



<li>노드의 오른쪽 서브트리에 있는 모든 노드의 키는 노드의 키보다 크다.</li>



<li>왼쪽, 오른쪽 서브트리도 이진 검색 트리이다.</li>
</ul>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="677" height="315" src="https://lycos7560.com/wp-content/uploads/2023/08/image-17.png" alt="" class="wp-image-36557" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-17.png 677w, https://lycos7560.com/wp-content/uploads/2023/08/image-17-300x140.png 300w" sizes="(max-width: 677px) 100vw, 677px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>전위 순회 (루트-왼쪽-오른쪽)은 루트를 방문하고, 왼쪽 서브트리, 오른쪽 서브 트리를 순서대로 방문하면서 노드의 키를 출력한다. </p>



<p>후위 순회 (왼쪽-오른쪽-루트)는 왼쪽 서브트리, 오른쪽 서브트리, 루트 노드 순서대로 키를 출력한다. </p>



<p>예를 들어, 위의 이진 검색 트리의 전위 순회 결과는 50 30 24 5 28 45 98 52 60 이고, 후위 순회 결과는 5 28 24 45 30 60 52 98 50 이다.</p>



<p>이진 검색 트리를 전위 순회한 결과가 주어졌을 때, 이 트리를 후위 순회한 결과를 구하는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>트리를 전위 순회한 결과가 주어진다. </p>



<p>노드에 들어있는 키의 값은 10<sup>6</sup>보다 작은 양의 정수이다. </p>



<p>모든 값은 한 줄에 하나씩 주어지며, 노드의 수는 10,000개 이하이다. </p>



<p>같은 키를 가지는 노드는 없다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>입력으로 주어진 이진 검색 트리를 후위 순회한 결과를 한 줄에 하나씩 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">50
30
24
5
28
45
98
52
60</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
28
24
45
30
60
52
98
50</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/1" target="_blank" rel="noreferrer noopener">ICPC</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/7" target="_blank" rel="noreferrer noopener">Regionals</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/42" target="_blank" rel="noreferrer noopener">Asia Pacific</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/102" target="_blank" rel="noreferrer noopener">Thailand</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/562" target="_blank" rel="noreferrer noopener">2011 ACM-ICPC Asia Phuket Regional Programming Contest</a>&nbsp;B번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/120" target="_blank" rel="noreferrer noopener">트리</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/62" target="_blank" rel="noreferrer noopener">재귀</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

struct BinaryNode
{
    int _Num;
    BinaryNode* _Left;
    BinaryNode* _Right;

    BinaryNode(int num)
        : _Num(num), _Left(nullptr), _Right(nullptr)
    {}

    void ArrayBinaryNode(int _n)
    {
        if (_Num > _n) {
            if (_Left == nullptr) _Left = new BinaryNode(_n);
            else _Left->ArrayBinaryNode(_n);
        }
        else {
            if (_Right == nullptr) _Right = new BinaryNode(_n);
            else _Right->ArrayBinaryNode(_n);
        }
    }
};

void PostorderTraverse(BinaryNode&amp; CurrentNode)
{
    if (CurrentNode._Left != nullptr) PostorderTraverse(*CurrentNode._Left);
    if (CurrentNode._Right != nullptr) PostorderTraverse(*CurrentNode._Right);
    cout &lt;&lt; CurrentNode._Num &lt;&lt; "\n";
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int _N;
    cin >> _N;
    BinaryNode _RootNode(_N);
    while (true) {
        cin >> _N;
        if (cin.eof() == true)
            break;
        _RootNode.ArrayBinaryNode(_N);
    }

    PostorderTraverse(_RootNode);

	return 0;
}

</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1076" height="98" src="https://lycos7560.com/wp-content/uploads/2023/08/image-18.png" alt="" class="wp-image-36561" style="width:927px;height:84px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-18.png 1076w, https://lycos7560.com/wp-content/uploads/2023/08/image-18-300x27.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-18-768x70.png 768w" sizes="(max-width: 1076px) 100vw, 1076px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5639%eb%b2%88-%ec%9d%b4%ec%a7%84-%ea%b2%80%ec%83%89-%ed%8a%b8%eb%a6%ac-c-baekjoon/36556/">백준 5639번 (이진 검색 트리, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5639%eb%b2%88-%ec%9d%b4%ec%a7%84-%ea%b2%80%ec%83%89-%ed%8a%b8%eb%a6%ac-c-baekjoon/36556/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1967번 (트리의 지름, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 17 Aug 2023 00:01:03 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1967]]></category>
		<category><![CDATA[1967번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold4]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드4]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그래프이론]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[깊이]]></category>
		<category><![CDATA[깊이 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1967]]></category>
		<category><![CDATA[백준 1967번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트리]]></category>
		<category><![CDATA[트리의 지름]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36529</guid>

					<description><![CDATA[<p>백준 1967번 '트리의 지름' 문제에 대한 글입니다. DFS 탐색 알고리즘을 이용하여 해결하였습니다. (This article is about the problem of Baekjoon Number 1967 'Diameter of the Tree'. It was solved using the DFS search algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/">백준 1967번 (트리의 지름, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-75779d84      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트리의-지름" class="uagb-toc-link__trigger">트리의 지름</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">트리의 지름 <img decoding="async" width="30" height="38" class="wp-image-37904" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/1967">https://www.acmicpc.net/problem/1967</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">39735</td><td class="has-text-align-left" data-align="left">15747</td><td class="has-text-align-left" data-align="left">11924</td><td class="has-text-align-left" data-align="left">41.200%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>트리(tree)는 사이클이 없는 무방향 그래프이다. </p>



<p>트리에서는 어떤 두 노드를 선택해도 둘 사이에 경로가 항상 하나만 존재하게 된다. </p>



<p>트리에서 어떤 두 노드를 선택해서 양쪽으로 쫙 당길 때, 가장 길게 늘어나는 경우가 있을 것이다. </p>



<p>이럴 때 트리의 모든 노드들은 이 두 노드를 지름의 끝 점으로 하는 원 안에 들어가게 된다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="483" height="201" src="https://lycos7560.com/wp-content/uploads/2023/08/image-12.png" alt="" class="wp-image-36531" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-12.png 483w, https://lycos7560.com/wp-content/uploads/2023/08/image-12-300x125.png 300w" sizes="(max-width: 483px) 100vw, 483px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>이런 두 노드 사이의 경로의 길이를 트리의 지름이라고 한다. </p>



<p>정확히 정의하자면 트리에 존재하는 모든 경로들 중에서 가장 긴 것의 길이를 말한다.</p>



<p>입력으로 루트가 있는 트리를 가중치가 있는 간선들로 줄 때, 트리의 지름을 구해서 출력하는 프로그램을 작성하시오. </p>



<p>아래와 같은 트리가 주어진다면 트리의 지름은 45가 된다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="486" height="262" src="https://lycos7560.com/wp-content/uploads/2023/08/image-14.png" alt="" class="wp-image-36533" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-14.png 486w, https://lycos7560.com/wp-content/uploads/2023/08/image-14-300x162.png 300w" sizes="(max-width: 486px) 100vw, 486px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>트리의 노드는 1부터 n까지 번호가 매겨져 있다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>파일의 첫 번째 줄은 노드의 개수 n(1 ≤ n ≤ 10,000)이다. </p>



<p>둘째 줄부터 n-1개의 줄에 각 간선에 대한 정보가 들어온다. </p>



<p>간선에 대한 정보는 세 개의 정수로 이루어져 있다. </p>



<p>첫 번째 정수는 간선이 연결하는 두 노드 중 부모 노드의 번호를 나타내고, 두 번째 정수는 자식 노드를, 세 번째 정수는 간선의 가중치를 나타낸다. </p>



<p>간선에 대한 정보는 부모 노드의 번호가 작은 것이 먼저 입력되고, 부모 노드의 번호가 같으면 자식 노드의 번호가 작은 것이 먼저 입력된다. </p>



<p>루트 노드의 번호는 항상 1이라고 가정하며, 간선의 가중치는 100보다 크지 않은 양의 정수이다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에 트리의 지름을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">12
1 2 3
1 3 2
2 4 5
3 5 11
3 6 9
4 7 1
4 8 7
5 9 15
5 10 4
6 11 6
6 12 10</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">45</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/apjw6112">apjw6112</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/dlwjsgud12">dlwjsgud12</a>,&nbsp;<a href="https://www.acmicpc.net/user/shg9411" target="_blank" rel="noreferrer noopener">shg9411</a>,&nbsp;<a href="https://www.acmicpc.net/user/tony9402" target="_blank" rel="noreferrer noopener">tony9402</a>,&nbsp;<a href="https://www.acmicpc.net/user/yongdol503" target="_blank" rel="noreferrer noopener">yongdol503</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/120">트리</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/127">깊이 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-embed is-type-wp-embed"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="wCvZK5P8Wt"><a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/">백준 1167번 (트리의 지름, C++) [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 1167번 (트리의 지름, C++) [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/embed/#?secret=TGKI0WPUmP#?secret=wCvZK5P8Wt" data-secret="wCvZK5P8Wt" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>백준 1167번 문제와 비슷한 문제</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
using namespace std;

unsigned short _V, _ResNode;
int _Res;
bool isvisted[100001];

struct Node
{
    unsigned short Number;
    vector&lt;pair&lt;unsigned short, unsigned short>> Connections;
    Node(unsigned short _num) : Number(_num) { };
};

vector&lt;Node> Nodes;

void DFS(unsigned short cntNode, int _dist)
{
    isvisted[cntNode] = true;
    if (Nodes[cntNode].Connections.size() == 1 &amp;&amp; _ResNode != cntNode) {
        if (_Res &lt; _dist) {
            _Res = _dist;
            _ResNode = cntNode;
        }
        return;
    }

    for (auto&amp; it : Nodes[cntNode].Connections) {
        if (isvisted[it.first])
            continue;
        DFS(it.first, _dist + it.second);
    }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> _V;
    for (int i = 0; i &lt;= _V; i++)
        Nodes.push_back(Node(i));

    unsigned short _n1, _n2, _d;
    for (int i = 0; i &lt; _V - 1; i++) {
        cin >> _n1 >> _n2 >> _d;
        Nodes[_n1].Connections.push_back({ _n2, _d });
        Nodes[_n2].Connections.push_back({ _n1, _d });
    }

    _Res = 0;
    _ResNode = 1;
    DFS(1, 0);
    for (int i = 1; i &lt;= _V; i++)
        isvisted[i] = false;
    _Res = 0;
    DFS(_ResNode, 0);
    cout &lt;&lt; _Res;
    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1443" height="114" src="https://lycos7560.com/wp-content/uploads/2023/08/image-15.png" alt="" class="wp-image-36535" style="width:1028px;height:81px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-15.png 1443w, https://lycos7560.com/wp-content/uploads/2023/08/image-15-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-15-768x61.png 768w" sizes="(max-width: 1443px) 100vw, 1443px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/">백준 1967번 (트리의 지름, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 7562번 (나이트의 이동, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-7562%eb%b2%88-%eb%82%98%ec%9d%b4%ed%8a%b8%ec%9d%98-%ec%9d%b4%eb%8f%99-c-baekjoon/36485/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-7562%eb%b2%88-%eb%82%98%ec%9d%b4%ed%8a%b8%ec%9d%98-%ec%9d%b4%eb%8f%99-c-baekjoon/36485/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 14 Aug 2023 00:24:24 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[7562]]></category>
		<category><![CDATA[7562번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Sliver]]></category>
		<category><![CDATA[Sliver1]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[나이트의 이동]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 7562]]></category>
		<category><![CDATA[백준 7562번]]></category>
		<category><![CDATA[실버]]></category>
		<category><![CDATA[실버1]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36485</guid>

					<description><![CDATA[<p>백준 7562번 '나이트의 이동' 문제에 대한 글입니다. BFS 알고리즘을 이용하여 해결하였습니다. (This article is about Baekjoon Number 7562 "Movement of the Night". It was solved using the BFS algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-7562%eb%b2%88-%eb%82%98%ec%9d%b4%ed%8a%b8%ec%9d%98-%ec%9d%b4%eb%8f%99-c-baekjoon/36485/">백준 7562번 (나이트의 이동, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-7fa90fe8      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#나이트의-이동" class="uagb-toc-link__trigger">나이트의 이동</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">나이트의 이동 <img decoding="async" width="30" height="38" class="wp-image-37895" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1.jpg 1250w, https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/09/Sliver_1-1200x1536.jpg 1200w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/7562">https://www.acmicpc.net/problem/7562</a></p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">52423</td><td class="has-text-align-left" data-align="left">27213</td><td class="has-text-align-left" data-align="left">20242</td><td class="has-text-align-left" data-align="left">50.811%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>체스판 위에 한 나이트가 놓여져 있다. </p>



<p>나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. </p>



<p>나이트가 이동하려고 하는 칸이 주어진다. </p>



<p>나이트는 몇 번 움직이면 이 칸으로 이동할 수 있을까?</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="325" height="320" src="https://lycos7560.com/wp-content/uploads/2023/08/image-6.png" alt="" class="wp-image-36487" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-6.png 325w, https://lycos7560.com/wp-content/uploads/2023/08/image-6-300x295.png 300w" sizes="(max-width: 325px) 100vw, 325px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>입력의 첫째 줄에는 테스트 케이스의 개수가 주어진다.</p>



<p>각 테스트 케이스는 세 줄로 이루어져 있다. </p>



<p>첫째 줄에는 체스판의 한 변의 길이 l(4 ≤&nbsp;l ≤ 300)이 주어진다. </p>



<p>체스판의 크기는 l × l이다. 체스판의 각 칸은 두 수의 쌍 {0, &#8230;, l-1} × {0, &#8230;, l-1}로 나타낼 수 있다. </p>



<p>둘째 줄과 셋째 줄에는 나이트가 현재 있는 칸, 나이트가 이동하려고 하는 칸이 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>각 테스트 케이스마다 나이트가 최소 몇 번만에 이동할 수 있는지 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
1 3 2 -1
2 4 4 -1
3 1 2 4 3 -1
4 2 4 3 3 5 6 -1
5 4 6 -1</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">11</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/5" target="_blank" rel="noreferrer noopener">University</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/213" target="_blank" rel="noreferrer noopener">Tu-Darmstadt Programming Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/909" target="_blank" rel="noreferrer noopener">TUD Contest 2001</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/sait2000">sait2000</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/sgchoi5">sgchoi5</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126">너비 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>

using namespace std;

constexpr int MAX = 300;

int _T, _I, _Res = 0;
int _DxDy[8][2] = { {-2,-1},{-2,1}, {2, 1}, {2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2} };
bool _IsVisted[MAX][MAX];
pair&lt;int, int> Pos[2];

struct CntPos
{
	int _Cnt = 0;
	pair&lt;int, int> _CntPos = {0, 0};

	CntPos(int _cnt, pair&lt;int, int> _cntPos) : _Cnt(_cnt), _CntPos(_cntPos) {};
};

queue&lt;CntPos> BFSQueue;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin >> _T;
	while (_T--)
	{
		cin >> _I;
		for (int i = 0; i &lt; _I; i++)
			for (int j = 0; j &lt; _I; j++)
				_IsVisted[i][j] = false;
			
		int _x, _y;
		for (int i = 0; i &lt; 2; i++) {
			cin >> _x >> _y;
			Pos[i].first = _x;
			Pos[i].second = _y;
		}

		while (!BFSQueue.empty())
			BFSQueue.pop();
		
		BFSQueue.push(CntPos(0, Pos[0]));
		_IsVisted[Pos[0].first][Pos[0].second] = true;
		while (!BFSQueue.empty()) {
			Pos[0] = BFSQueue.front()._CntPos;
			int _cnt = BFSQueue.front()._Cnt;
			BFSQueue.pop();
			if (Pos[0] == Pos[1]) {
				_Res = _cnt;
				break;
			}

			for (int i = 0; i &lt; 8; i++) {
				int _dx = Pos[0].first + _DxDy[i][0];
				int _dy = Pos[0].second + _DxDy[i][1];
				if (_dx &lt; 0 || _dy &lt; 0 || _dx >= _I || _dy >= _I)
					continue;
				if (_IsVisted[_dx][_dy]) 
					continue;
				BFSQueue.push(CntPos(_cnt + 1, { _dx, _dy }));
				_IsVisted[_dx][_dy] = true;
			}
		}

		cout &lt;&lt;_Res &lt;&lt; "\n";
	}

	return 0;
}

</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1206" height="97" src="https://lycos7560.com/wp-content/uploads/2023/08/image-7.png" alt="" class="wp-image-36489" style="width:1088px;height:87px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-7.png 1206w, https://lycos7560.com/wp-content/uploads/2023/08/image-7-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-7-768x62.png 768w" sizes="(max-width: 1206px) 100vw, 1206px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-7562%eb%b2%88-%eb%82%98%ec%9d%b4%ed%8a%b8%ec%9d%98-%ec%9d%b4%eb%8f%99-c-baekjoon/36485/">백준 7562번 (나이트의 이동, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-7562%eb%b2%88-%eb%82%98%ec%9d%b4%ed%8a%b8%ec%9d%98-%ec%9d%b4%eb%8f%99-c-baekjoon/36485/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1167번 (트리의 지름, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 12 Aug 2023 19:20:07 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1167]]></category>
		<category><![CDATA[1167번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold2]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드2]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[깊이]]></category>
		<category><![CDATA[깊이 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1167]]></category>
		<category><![CDATA[백준 1167번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트리]]></category>
		<category><![CDATA[트리의 지름]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36474</guid>

					<description><![CDATA[<p>백준 1167번 '트리의 지름' 문제에 대한 글입니다. DFS를 이용하여 문제를 해결하였습니다. (This article is about Baekjoon Number 1167 'Diameter of the Tree'. I solved the problem using 'DFS')</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/">백준 1167번 (트리의 지름, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-a1d67416      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트리의-지름" class="uagb-toc-link__trigger">트리의 지름</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">트리의 지름 <img decoding="async" width="30" height="38" class="wp-image-37903" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_2-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/1167" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1167</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">44657</td><td class="has-text-align-left" data-align="left">16318</td><td class="has-text-align-left" data-align="left">11729</td><td class="has-text-align-left" data-align="left">33.983%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>트리의 지름이란, 트리에서 임의의 두 점 사이의 거리 중 가장 긴 것을 말한다. </p>



<p>트리의 지름을 구하는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>트리가 입력으로 주어진다.</p>



<p>먼저 첫 번째 줄에서는 트리의 정점의 개수 V가 주어지고 (2 ≤ V ≤ 100,000)</p>



<p>둘째 줄부터 V개의 줄에 걸쳐 간선의 정보가 다음과 같이 주어진다. </p>



<p>정점 번호는 1부터 V까지 매겨져 있다.</p>



<p>먼저 정점 번호가 주어지고, 이어서 연결된 간선의 정보를 의미하는 정수가 두 개씩 주어지는데, </p>



<p>하나는 정점번호, 다른 하나는 그 정점까지의 거리이다. </p>



<p>예를 들어 네 번째 줄의 경우 정점 3은 정점 1과 거리가 2인 간선으로 연결되어 있고, </p>



<p>정점 4와는 거리가 3인 간선으로 연결되어 있는 것을 보여준다. 각 줄의 마지막에는 -1이 입력으로 주어진다. </p>



<p>주어지는 거리는 모두 10,000 이하의 자연수이다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에 트리의 지름을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
1 3 2 -1
2 4 4 -1
3 1 2 4 3 -1
4 2 4 3 3 5 6 -1
5 4 6 -1</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">11</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람: <a href="https://www.acmicpc.net/user/ababc1005" target="_blank" rel="noreferrer noopener">ababc1005</a>, <a href="https://www.acmicpc.net/user/cfghj101" target="_blank" rel="noreferrer noopener">cfghj101</a>, <a href="https://www.acmicpc.net/user/WeissBlume" target="_blank" rel="noreferrer noopener">WeissBlume</a></li>



<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/author5" target="_blank" rel="noreferrer noopener">author5</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/120" target="_blank" rel="noreferrer noopener">트리</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/127" target="_blank" rel="noreferrer noopener">깊이 우선 탐색</a></li>
</ul>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
using namespace std;

int _V,  _ResNode, _Res;
bool isvisted[100001];

struct Node
{
	int Number;
	vector&lt;pair&lt;int, int>> Connections;
	Node(int _num) : Number(_num) { };
};

vector&lt;Node> Nodes;

void DFS(int cntNode, int _dist) 
{
	isvisted[cntNode] = true;
	if (Nodes[cntNode].Connections.size() == 1 &amp;&amp; _ResNode != cntNode) {
		if (_Res &lt; _dist) {
			_Res = _dist;
			_ResNode = cntNode;
		}
		return;
	}

	for (auto&amp; it : Nodes[cntNode].Connections) { 
		if (isvisted[it.first])
			continue;
		DFS(it.first, _dist + it.second);
	}
}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _V;
	for (int i = 0; i &lt;= _V; i++) 
		Nodes.push_back(Node(i));
	
	int _n1, _n2, _d;
	for (int i = 1; i &lt;= _V; i++) {
		cin >> _n1;
		while (true) {
			cin >> _n2;
			if (_n2 == -1) break;
			cin >> _d;
			Nodes[_n1].Connections.push_back({ _n2, _d});
		}
	}

	_Res = 0;
	_ResNode = 1;
	DFS(1, 0);
	for (int i = 1; i &lt;= _V; i++)
		isvisted[i] = false;
	_Res = 0;
	DFS(_ResNode, 0);
	cout &lt;&lt; _Res;
	return 0;
}

</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1202" height="143" src="https://lycos7560.com/wp-content/uploads/2023/08/image-5.png" alt="" class="wp-image-36480" style="width:1006px;height:120px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-5.png 1202w, https://lycos7560.com/wp-content/uploads/2023/08/image-5-300x36.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-5-768x91.png 768w" sizes="(max-width: 1202px) 100vw, 1202px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/">백준 1167번 (트리의 지름, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 9376번 (탈옥, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9376%eb%b2%88-%ed%83%88%ec%98%a5-c-baekjoon/36378/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9376%eb%b2%88-%ed%83%88%ec%98%a5-c-baekjoon/36378/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 04 Aug 2023 10:01:09 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[9376]]></category>
		<category><![CDATA[9376번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 9376]]></category>
		<category><![CDATA[백준 9376번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[탈옥]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36378</guid>

					<description><![CDATA[<p>백준 9376번 '탈옥' 문제에 대한 글입니다. priority_queue와 BFS 탐색을 이용하여 해결하였습니다. (This article is about Baekjoon Number 9376 'exile' problem. Resolved using priority_queue and BFS.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9376%eb%b2%88-%ed%83%88%ec%98%a5-c-baekjoon/36378/">백준 9376번 (탈옥, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-412d31fe      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#탈옥" class="uagb-toc-link__trigger">탈옥</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">탈옥</h1>



<p><a href="https://www.acmicpc.net/problem/9376" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/9376</a></p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">10168</td><td class="has-text-align-left" data-align="left">1962</td><td class="has-text-align-left" data-align="left">1382</td><td class="has-text-align-left" data-align="left">26.234%</td></tr></tbody></table></figure>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>상근이는 감옥에서 죄수 두 명을 탈옥시켜야 한다. </p>



<p>이 감옥은 1층짜리 건물이고, 상근이는 방금 평면도를 얻었다.</p>



<p>평면도에는 모든 벽과 문이 나타나있고, 탈옥시켜야 하는 죄수의 위치도 나타나 있다. </p>



<p>감옥은 무인 감옥으로 죄수 두 명이 감옥에 있는 유일한 사람이다.</p>



<p>문은 중앙 제어실에서만 열 수 있다. </p>



<p>상근이는 특별한 기술을 이용해 제어실을 통하지 않고 문을 열려고 한다. </p>



<p>하지만, 문을 열려면 시간이 매우 많이 걸린다. </p>



<p>두 죄수를 탈옥시키기 위해서 열어야 하는 문의 개수를 구하는 프로그램을 작성하시오. </p>



<p>문을 한 번 열면 계속 열린 상태로 있는다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫째 줄에 테스트 케이스의 개수가 주어진다. </p>



<p>테스트 케이스의 수는 100개를 넘지 않는다.</p>



<p>첫째 줄에는 평면도의 높이 h와 너비 w가 주어진다. (2 ≤ h, w ≤ 100) </p>



<p>다음 h개 줄에는 감옥의 평면도 정보가 주어지며, </p>



<p>빈 공간은 &#8216;.&#8217;, 지나갈 수 없는 벽은 &#8216;*&#8217;, 문은 &#8216;#&#8217;, 죄수의 위치는 &#8216;$&#8217;이다.</p>



<p>상근이는 감옥 밖을 자유롭게 이동할 수 있고, 평면도에 표시된 죄수의 수는 항상 두 명이다. </p>



<p>각 죄수와 감옥의 바깥을 연결하는 경로가 항상 존재하는 경우만 입력으로 주어진다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>각 테스트 케이스마다 두 죄수를 탈옥시키기 위해서 열어야 하는 문의 최솟값을 출력한다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
5 9
****#****
*..#.#..*
****.****
*$#.#.#$*
*********
5 11
*#*********
*$*...*...*
*$*.*.*.*.*
*...*...*.*
*********.*
9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*#*****#*
*.#.#.#.*
*********</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
0
9</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/1" target="_blank" rel="noreferrer noopener">ICPC</a> > <a href="https://www.acmicpc.net/category/7" target="_blank" rel="noreferrer noopener">Regionals</a> > <a href="https://www.acmicpc.net/category/10" target="_blank" rel="noreferrer noopener">Europe</a> > <a href="https://www.acmicpc.net/category/15" target="_blank" rel="noreferrer noopener">Northwestern European Regional Contest</a> > <a href="https://www.acmicpc.net/category/89" target="_blank" rel="noreferrer noopener">Benelux Algorithm Programming Contest</a> > <a href="https://www.acmicpc.net/category/detail/1160" target="_blank" rel="noreferrer noopener">BAPC 2013</a> J번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람: <a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/doju" target="_blank" rel="noreferrer noopener">doju</a>, <a href="https://www.acmicpc.net/user/playsworld16" target="_blank" rel="noreferrer noopener">playsworld16</a>, <a href="https://www.acmicpc.net/user/yukino" target="_blank" rel="noreferrer noopener">yukino</a></li>
</ul>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/176" target="_blank" rel="noreferrer noopener">0-1 너비 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

constexpr int MAX = 102;
using namespace std;

int _T, _H, _W;
int _DxDy[4][2] = { { 1, 0}, { 0, -1}, { 0, 1}, { -1, 0} };

vector&lt;pair&lt;int, int>> _Theifs;

char _Map[MAX][MAX];
int _IsVisted[3][MAX][MAX];

struct Pos
{
	int _X, _Y, _Doors;
	Pos(int _x, int _y, int _doors) : _X(_x), _Y(_y), _Doors(_doors) {};
	bool operator&lt;(const Pos&amp; other) const {
		return _Doors > other._Doors;
	}
};

void BFSFuction(int _x, int _y, int _n)
{
	for (int i = 0; i &lt; MAX; i++)
		for (int j = 0; j &lt; MAX; j++)
			_IsVisted[_n][i][j] = INT32_MAX;
	priority_queue&lt;Pos> _BFSQueue;
	_BFSQueue.push(Pos(_x, _y, 0));
	_IsVisted[_n][_x][_y] = 0;
	while (!_BFSQueue.empty()) {
		int CntX = _BFSQueue.top()._X;
		int CntY = _BFSQueue.top()._Y;
		int Doors = _BFSQueue.top()._Doors;
		_BFSQueue.pop();
		if (_IsVisted[_n][CntX][CntY] &lt; Doors)
			continue;
		for (int i = 0; i &lt; 4; i++) {
			int dx = CntX + _DxDy[i][0];
			int dy = CntY + _DxDy[i][1];
			int doors = Doors;
			if (dx == -1 || dy == -1 || dx == _H + 2 || dy == _W + 2)
				continue;
			if (_Map[dx][dy] == '*')
				continue;
			else if (_Map[dx][dy] == '#')
				doors++;
			if (doors &lt; _IsVisted[_n][dx][dy]) {
				_BFSQueue.push(Pos(dx, dy, doors));
				_IsVisted[_n][dx][dy] = doors;
			}
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _T;
	while (_T--) {
		cin >> _H >> _W;
		_Theifs.clear();
		for (int i = 0; i &lt; MAX; i++)
			for (int j = 0; j &lt; MAX; j++)
				_Map[i][j] = ' ';
		for (int i = 1; i &lt;= _H; i++) {
			string _Str;
			cin >> _Str;
			for (int j = 0; j &lt; _W; j++) {
				_Map[i][j + 1] = _Str[j];
				if (_Str[j] == '$')
					_Theifs.push_back({ i, j + 1 });
			}
		}
		BFSFuction(_Theifs[0].first, _Theifs[0].second, 0);
		BFSFuction(_Theifs[1].first, _Theifs[1].second, 1);
		BFSFuction(0, 0, 2);
		int _Res = INT32_MAX;
		for (int i = 0; i &lt;= _H + 1; i++)
			for (int j = 0; j &lt;= _W + 1; j++) {
				if (_Map[i][j] == '*') continue;
				if (_IsVisted[0][i][j] == INT32_MAX || _IsVisted[1][i][j] == INT32_MAX || _IsVisted[2][i][j] == INT32_MAX)
					continue;
				int _sum = _IsVisted[0][i][j] + _IsVisted[1][i][j] + _IsVisted[2][i][j];
				if (_Map[i][j] == '#')
					_sum -= 2;
				_Res = min(_Res, _sum);
			}
		cout &lt;&lt; _Res &lt;&lt; "\n";
	}

	return 0;
}</pre>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1205" height="584" src="https://lycos7560.com/wp-content/uploads/2023/08/image.png" alt="" class="wp-image-36390" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image.png 1205w, https://lycos7560.com/wp-content/uploads/2023/08/image-300x145.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-768x372.png 768w" sizes="(max-width: 1205px) 100vw, 1205px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9376%eb%b2%88-%ed%83%88%ec%98%a5-c-baekjoon/36378/">백준 9376번 (탈옥, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9376%eb%b2%88-%ed%83%88%ec%98%a5-c-baekjoon/36378/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
