<?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>BFS Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/bfs/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Fri, 15 Mar 2024 15:58:39 +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>BFS Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 fetchpriority="high" 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>백준 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>백준 16197번 (두 동전, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16197%eb%b2%88-%eb%91%90-%eb%8f%99%ec%a0%84-c-baekjoon/36394/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16197%eb%b2%88-%eb%91%90-%eb%8f%99%ec%a0%84-c-baekjoon/36394/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 05 Aug 2023 14:48:05 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[16197]]></category>
		<category><![CDATA[16197번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></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[백준 16197]]></category>
		<category><![CDATA[백준 16197번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36394</guid>

					<description><![CDATA[<p>백준 16197번 '두 동전' 문제에 대한 글입니다. BFS 탐색을 이용하여 해결하였습니다. (This article is about BaekJoon Number 16197 "two coins" problem.<br />
It was solved using BFS.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16197%eb%b2%88-%eb%91%90-%eb%8f%99%ec%a0%84-c-baekjoon/36394/">백준 16197번 (두 동전, 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: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-601fdb45      "
					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><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#dfs-참고-코드" class="uagb-toc-link__trigger">DFS 참고 코드</a></ul></ol>					</div>
									</div>
				</div>
			


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



<h1 class="wp-block-heading"><strong>두 동전</strong></h1>



<p><a href="https://www.acmicpc.net/problem/16197" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/16197</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">9745</td><td class="has-text-align-left" data-align="left">4287</td><td class="has-text-align-left" data-align="left">2903</td><td class="has-text-align-left" data-align="left">42.250%</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>N×M 크기의 보드와 4개의 버튼으로 이루어진 게임이 있다. </p>



<p>보드는 1×1크기의 정사각형 칸으로 나누어져 있고,&nbsp;각각의 칸은 비어있거나, 벽이다. </p>



<p>두 개의 빈 칸에는 동전이 하나씩 놓여져 있고, 두 동전의 위치는 다르다.</p>



<p>버튼은 &#8220;왼쪽&#8221;, &#8220;오른쪽&#8221;, &#8220;위&#8221;, &#8220;아래&#8221;와 같이 4가지가 있다. </p>



<p>버튼을 누르면 두 동전이 버튼에 쓰여 있는 방향으로 동시에 이동하게 된다.</p>



<ul class="wp-block-list">
<li>동전이 이동하려는 칸이 벽이면, 동전은 이동하지 않는다.</li>



<li>동전이 이동하려는 방향에 칸이 없으면 동전은 보드 바깥으로 떨어진다.</li>



<li>그 외의 경우에는 이동하려는 방향으로 한 칸 이동한다.이동하려는 칸에 동전이 있는 경우에도 한 칸 이동한다.</li>
</ul>



<p>두 동전 중 하나만 보드에서 떨어뜨리기 위해 버튼을 최소 몇&nbsp;번 눌러야하는지 구하는 프로그램을 작성하시오.</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이 주어진다. (1 ≤ N, M ≤ 20)</p>



<p>둘째 줄부터 N개의 줄에는 보드의 상태가 주어진다.</p>



<ul class="wp-block-list">
<li><code>o</code>: 동전</li>



<li><code>.</code>: 빈 칸</li>



<li><code>#</code>: 벽</li>
</ul>



<p>동전의 개수는 항상 2개이다.</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번보다 많이 눌러야 한다면, -1을 출력한다.</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="">1 2
oo</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="">1</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 입력 2</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 2
.#
.#
.#
o#
o#
##</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 2</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:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 3</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 2
..
..
..
o#
o#
##</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 3</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:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 4</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 3
###
.o.
###
.o.
###</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 4</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:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 5</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 3
###
.o.
#.#
.o.
###</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 5</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: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/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</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/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/5" 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"><strong>통과된 코드</strong></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>

using namespace std;

int _N, _M, _Res = -1;
int _DxDy[4][2] = { { 1, 0}, { 0, -1}, { 0, 1}, { -1, 0} };
char _Map[20][20];
string _Str;

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

struct CntPos
{
	pair&lt;int,int> CoinOne;
	pair&lt;int,int> CoinTwo;
	int Cnt;

	CntPos(int _x1, int _y1, int _x2, int _y2, int _cnt) 
		: CoinOne(make_pair(_x1, _y1)), CoinTwo(make_pair(_x2, _y2)), Cnt(_cnt) {}
};

queue&lt;CntPos> BfsQueue;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N >> _M;
	for (int i = 0; i &lt; _N; i++) {
		cin >> _Str;
		for (int j = 0; j &lt; _M; j++) {
			_Map[i][j] = _Str[j];
			if (_Str[j] == 'o')
				_Coins.push_back({ i, j });
		}
	}

	BfsQueue.push(CntPos(_Coins[0].first, _Coins[0].second, _Coins[1].first, _Coins[1].second, 0));
	while (!BfsQueue.empty()) {
		CntPos _cntpos = BfsQueue.front();
		BfsQueue.pop();

		for (int i = 0; i &lt; 4; i++) {
			bool CheckOne = false, CheckTwo = false;
			_Coins[0].first = _cntpos.CoinOne.first + _DxDy[i][0];
			_Coins[0].second = _cntpos.CoinOne.second + _DxDy[i][1];
			_Coins[1].first = _cntpos.CoinTwo.first + _DxDy[i][0];
			_Coins[1].second = _cntpos.CoinTwo.second + _DxDy[i][1];

			// 동전의 위치가 같다면 하나만 떨어뜨리는 조건에 맞지 않는다.
			if (_Coins[0] == _Coins[1]) continue;

			// 두 동전 중 하나만 보드에서 떨어뜨리기 위함
			if (_Coins[0].first &lt; 0 || _Coins[0].second &lt; 0 || _Coins[0].first >= _N || _Coins[0].second >= _M)
				CheckOne = true;
			if (_Coins[1].first &lt; 0 || _Coins[1].second &lt; 0 || _Coins[1].first >= _N || _Coins[1].second >= _M)
				CheckTwo = true;

			// 동전이 이동하려는 칸이 벽이면, 동전은 이동하지 않는다.
			if (!CheckOne &amp;&amp; (_Map[_Coins[0].first][_Coins[0].second] == '#')) {
				_Coins[0].first = _cntpos.CoinOne.first;
				_Coins[0].second = _cntpos.CoinOne.second;
			}
			if (!(CheckTwo) &amp;&amp; _Map[_Coins[1].first][_Coins[1].second] == '#') {
				_Coins[1].first = _cntpos.CoinTwo.first;
				_Coins[1].second = _cntpos.CoinTwo.second;
			}

			int _TCnt = _cntpos.Cnt;
			if (_TCnt >= 10) {
				_Res = -1;
				while (!BfsQueue.empty())
					BfsQueue.pop();
				break;
			}

			if (CheckOne ^ CheckTwo) {
				_Res = _TCnt + 1;
				while (!BfsQueue.empty())
					BfsQueue.pop();
				break;
			}
			else if(CheckOne &amp;&amp; CheckTwo)
				continue;

			BfsQueue.push(CntPos(_Coins[0].first, _Coins[0].second, _Coins[1].first, _Coins[1].second, _TCnt + 1));
		}
	}

	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" src="https://lycos7560.com/wp-content/uploads/2023/08/image-1.png" alt="" class="wp-image-36405" width="905" height="77" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-1.png 1207w, https://lycos7560.com/wp-content/uploads/2023/08/image-1-300x26.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-1-768x66.png 768w" sizes="(max-width: 905px) 100vw, 905px" /></figure>



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



<p>생각보다 메모리를 많이 사용&#8230;</p>



<p>DFS 탐색을 이용한 백트레킹 방법이 시간과 메모리 부분에서 더 효율적이다.</p>



<div style="height:50px" 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)"/>



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



<h2 class="wp-block-heading has-large-font-size"><strong>DFS 참고 코드</strong></h2>



<p><a href="https://hyeo-noo.tistory.com/126" target="_blank" rel="noreferrer noopener">https://hyeo-noo.tistory.com/126</a></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>
#include &lt;cstring>
 
#define pii pair&lt;int, int>
 
using namespace std;
 
 
int N, M, result = 1e9+7;
int Map[21][21];    // 2 : 동전 위치   0 : 벽 위치   1 : 빈칸
vector&lt; pii > startP;
int dr[4] = {0, 0, 1, -1}, dc[4] = {1, -1, 0, 0};
 
void input(){
    // Map에서 벗어나는 경우는 -1로 설정
    memset(Map, -1, sizeof(Map));
    
    cin >> N >> M;
    string str;
    for(int i = 1; i &lt;= N; i++){
        cin >> str;
        for(int j = 0; j &lt; M; j++){
            if(str[j] == 'o'){
                Map[i][j+1] = 2;
                startP.push_back({i, j+1});
            }
            else if(str[j] == '#') Map[i][j+1] = 0;
            else Map[i][j+1] = 1;
        }
    }
}
 
bool isOut(const pii &amp;A){
    if(A.first &lt; 1 || A.first > N || A.second &lt; 1 || A.second > M) return true;
    return false;
}
 
void dfs(pii A, pii B, int cnt){
    // 현재 result보다 cnt가 크다면 더이상의 탐색은 불필요
    if(result &lt; cnt) return;
    
    // cnt 가 10보다 커지면 result값 갱신
    if(cnt > 10){
        result = min(result, cnt);
        return;
    }
    
    // 둘 다 떨어졌으면 되돌아감
    if(isOut(A) &amp;&amp; isOut(B)) return;
    // 하나만 떨어졌으면 result를 최소 cnt값으로 교체
    else if((isOut(A) &amp;&amp; !isOut(B)) || (!isOut(A) &amp;&amp; isOut(B))){
        result = min(result, cnt);
        return;
    }
    
    for(int i = 0; i &lt; 4; i++){
        pii An = {A.first + dr[i], A.second + dc[i]};
        pii Bn = {B.first + dr[i], B.second + dc[i]};
        if(!Map[An.first][An.second]) An = A;
        if(!Map[Bn.first][Bn.second]) Bn = B;
        
        dfs(An, Bn, cnt+1);
    }
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    input();
    dfs(startP[0], startP[1], 0);
    
    if(result > 10) cout &lt;&lt; -1;
    else cout &lt;&lt; result;
    
    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" src="https://lycos7560.com/wp-content/uploads/2023/08/image-2.png" alt="" class="wp-image-36424" width="903" height="74" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-2.png 1204w, https://lycos7560.com/wp-content/uploads/2023/08/image-2-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-2-768x63.png 768w" sizes="(max-width: 903px) 100vw, 903px" /></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-16197%eb%b2%88-%eb%91%90-%eb%8f%99%ec%a0%84-c-baekjoon/36394/">백준 16197번 (두 동전, 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-16197%eb%b2%88-%eb%91%90-%eb%8f%99%ec%a0%84-c-baekjoon/36394/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>
		<item>
		<title>백준 14442번 (벽 부수고 이동하기 2, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14442%eb%b2%88-%eb%b2%bd-%eb%b6%80%ec%88%98%ea%b3%a0-%ec%9d%b4%eb%8f%99%ed%95%98%ea%b8%b0-2-c-baekjoon/36181/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14442%eb%b2%88-%eb%b2%bd-%eb%b6%80%ec%88%98%ea%b3%a0-%ec%9d%b4%eb%8f%99%ed%95%98%ea%b8%b0-2-c-baekjoon/36181/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 17 Jul 2023 02:26:35 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[14442]]></category>
		<category><![CDATA[14442번]]></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[백준 14442]]></category>
		<category><![CDATA[백준 14442번]]></category>
		<category><![CDATA[벽 부수고 이동하기 2]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36181</guid>

					<description><![CDATA[<p>백준 14442번 '벽 부수고 이동하기 2' 문제에 대한 글입니다. 'BFS 탐색을 이용하여 해결하였습니다.' (This article is about Baekjoon 14442 'Breaking Walls and Moving 2'. 'Solved using BFS.')</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14442%eb%b2%88-%eb%b2%bd-%eb%b6%80%ec%88%98%ea%b3%a0-%ec%9d%b4%eb%8f%99%ed%95%98%ea%b8%b0-2-c-baekjoon/36181/">백준 14442번 (벽 부수고 이동하기 2, 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-dea3dc9c      "
					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="#벽-부수고-이동하기-2" class="uagb-toc-link__trigger">벽 부수고 이동하기 2</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">벽 부수고 이동하기 2</h1>



<p><a href="https://www.acmicpc.net/problem/14442" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/14442</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">24036</td><td class="has-text-align-left" data-align="left">6455</td><td class="has-text-align-left" data-align="left">4373</td><td class="has-text-align-left" data-align="left">26.864%</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>N×M의 행렬로 표현되는 맵이 있다. </p>



<p>맵에서 0은 이동할 수 있는 곳을 나타내고, 1은 이동할 수 없는 벽이 있는 곳을 나타낸다. </p>



<p>당신은 (1, 1)에서 (N, M)의 위치까지 이동하려 하는데, 이때 최단 경로로 이동하려 한다. </p>



<p>최단경로는 맵에서 가장 적은 개수의 칸을 지나는 경로를 말하는데, 이때 시작하는 칸과 끝나는 칸도 포함해서 센다.</p>



<p>만약에 이동하는 도중에 벽을 부수고 이동하는 것이 좀 더 경로가 짧아진다면, 벽을 K개 까지 부수고 이동하여도 된다.</p>



<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>첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 1,000), K(1 ≤ K ≤ 10)이 주어진다. </p>



<p>다음 N개의 줄에 M개의 숫자로 맵이 주어진다. </p>



<p>(1, 1)과 (N, M)은 항상 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>첫째 줄에 최단 거리를 출력한다. 불가능할 때는 -1을 출력한다.</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="">6 4 1
0100
1110
1000
0000
0111
0000</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="">15</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 입력 2</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 4 2
0100
1110
1000
0000
0111
0000</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 2</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="">9</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 입력 3</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 4 3
0111
1111
1111
1110</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 3</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: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/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a>, <a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a>, <a href="https://www.acmicpc.net/user/YunGoon" target="_blank" rel="noreferrer noopener">YunGoon</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/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>



<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;
bool _Map[1000][1000];
bool _IsVisted[11][1000][1000];
int _N, _M, _K, _Res = INT32_MAX;
string _Temp;
int _DxDy[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
queue&lt;pair&lt;int, pair&lt;int, pair&lt;int, int>>>> _BFSQueue;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N >> _M >> _K;
	for (int i = 0; i &lt; _N; i++) {
		cin >> _Temp;
		for (int j = 0; j &lt; _M; j++)
			if (_Temp[j] == '1')
				_Map[i][j] = true;
	}
	_BFSQueue.push(make_pair(0, make_pair(0, make_pair(0, 0))));
	_IsVisted[0][0][0] = true;
	while (!_BFSQueue.empty()) {
		int _Cnt = _BFSQueue.front().first;
		int _k = _BFSQueue.front().second.first;
		pair&lt;int, int> _Pos = _BFSQueue.front().second.second;
		_BFSQueue.pop();
		if (_Pos.first == _N - 1 &amp;&amp; _Pos.second == _M - 1) {
			_Res = _Cnt + 1;
			break;
		}

		for (int i = 0; i &lt; 4; i++) {
			int _dx = _Pos.first + _DxDy[i][0];
			int _dy = _Pos.second + _DxDy[i][1];
			if (_dx >= _N || _dy >= _M || _dx &lt; 0 || _dy &lt; 0)
				continue;
			if (_Map[_dx][_dy] &amp;&amp; _k &lt; _K &amp;&amp; !_IsVisted[_k + 1][_dx][_dy]) {
				_IsVisted[_k + 1][_dx][_dy] = true;
				_BFSQueue.push(make_pair(_Cnt + 1, make_pair(_k + 1, make_pair(_dx, _dy))));
			}
			if (!_Map[_dx][_dy] &amp;&amp;  !_IsVisted[_k][_dx][_dy]) {
				_IsVisted[_k][_dx][_dy] = true;
				_BFSQueue.push(make_pair(_Cnt + 1, make_pair(_k, make_pair(_dx, _dy))));
			}
		}
	}
	if (_Res == INT32_MAX) cout &lt;&lt; -1;
	else cout &lt;&lt; _Res;
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1202" height="276" src="https://lycos7560.com/wp-content/uploads/2023/07/image-40.png" alt="" class="wp-image-36194" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-40.png 1202w, https://lycos7560.com/wp-content/uploads/2023/07/image-40-300x69.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-40-768x176.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="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-14442%eb%b2%88-%eb%b2%bd-%eb%b6%80%ec%88%98%ea%b3%a0-%ec%9d%b4%eb%8f%99%ed%95%98%ea%b8%b0-2-c-baekjoon/36181/">백준 14442번 (벽 부수고 이동하기 2, 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-14442%eb%b2%88-%eb%b2%bd-%eb%b6%80%ec%88%98%ea%b3%a0-%ec%9d%b4%eb%8f%99%ed%95%98%ea%b8%b0-2-c-baekjoon/36181/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2665번 (미로만들기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2665%eb%b2%88-%eb%af%b8%eb%a1%9c%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/36123/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2665%eb%b2%88-%eb%af%b8%eb%a1%9c%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/36123/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 15 Jul 2023 00:08:27 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2665]]></category>
		<category><![CDATA[2665번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Search]]></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[백준 2665]]></category>
		<category><![CDATA[백준 2665번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36123</guid>

					<description><![CDATA[<p>백준 2665번 '미로만들기' 문제에 대한 글입니다. 'BFS 탐색' 이용하여 문제를 해결하였습니다. (This article is about Baekjun #2665 'Maze making' problem. I solved the problem using 'BFS'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2665%eb%b2%88-%eb%af%b8%eb%a1%9c%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/36123/">백준 2665번 (미로만들기, 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-b42197ed      "
					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/2665" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2665</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">11519</td><td class="has-text-align-left" data-align="left">6473</td><td class="has-text-align-left" data-align="left">4799</td><td class="has-text-align-left" data-align="left">56.519%</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>n×n 바둑판 모양으로 총 n<sup>2</sup>개의 방이 있다. </p>



<p>일부분은 검은 방이고 나머지는 모두 흰 방이다. </p>



<p>검은 방은 사면이 벽으로 싸여 있어 들어갈&nbsp;수 없다. </p>



<p>서로 붙어 있는 두 개의 흰 방 사이에는 문이 있어서 지나다닐 수 있다. </p>



<p>윗줄 맨 왼쪽 방은 시작방으로서 항상 흰 방이고, 아랫줄 맨 오른쪽 방은 끝방으로서 역시 흰 방이다.</p>



<p>시작방에서 출발하여 길을 찾아서 끝방으로 가는 것이 목적인데, 아래 그림의 경우에는 시작방에서 끝 방으로 갈 수가 없다. </p>



<p>부득이 검은 방 몇 개를 흰 방으로 바꾸어야 하는데 되도록 적은 수의 방의 색을 바꾸고 싶다.</p>



<p>아래 그림은 n=8인 경우의 한 예이다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="537" height="387" src="https://lycos7560.com/wp-content/uploads/2023/07/image-35.png" alt="" class="wp-image-36128" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-35.png 537w, https://lycos7560.com/wp-content/uploads/2023/07/image-35-300x216.png 300w" sizes="(max-width: 537px) 100vw, 537px" /></figure>



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



<p>위 그림에서는 두 개의 검은 방(예를 들어 (4,4)의 방과 (7,8)의 방)을 흰 방으로 바꾸면, </p>



<p>시작방에서 끝방으로 갈 수 있지만, 어느 검은 방 하나만을 흰 방으로 바꾸어서는 불가능하다. </p>



<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">입력</h3>



<p>첫 줄에는 한 줄에 들어가는 방의 수 n(1 ≤ n ≤ 50)이 주어지고, </p>



<p>다음 n개의 줄의 각 줄마다 0과 1이 이루어진 길이가 n인 수열이 주어진다. </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>



<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="">8
11100110
11010010
10011010
11101100
01000111
00110001
11011000
11000111</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:40px" 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/81" target="_blank" rel="noreferrer noopener">KOI 1997</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/402" target="_blank" rel="noreferrer noopener">고등부</a>&nbsp;2번</p>



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



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/yukariko" target="_blank" rel="noreferrer noopener">yukariko</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/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>



<p>우선 순위 Queue를 이용하여 방을 바꾸는 순서가 적은 순으로 탐색합니다.</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;
int _N, _Res;
int _DxDy[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };;
string _Str;
bool _Map[50][50];
bool _IsVisted[50][50];
priority_queue&lt;pair&lt;int, pair&lt;int, int>>, vector&lt;pair&lt;int, pair&lt;int, int>>>, greater&lt;pair&lt;int, pair&lt;int, int>>>> _BfsQueue;
int main()
{
	cin >> _N;
	for (int i = 0; i &lt; _N; i++) {
		cin >> _Str;
		for (int j = 0; j &lt; _N; j++)
			if (_Str[j] == '1')
				_Map[i][j] = true;		
	}

	_BfsQueue.push(make_pair(0, make_pair(0, 0)));
	_IsVisted[0][0] = true;
	while (!_BfsQueue.empty()) {
		pair&lt;int, int> _Pos = _BfsQueue.top().second;
		int _Cnt = _BfsQueue.top().first;
		_BfsQueue.pop();
		if (_Pos.first == _N - 1 &amp;&amp; _Pos.second == _N - 1) {
			_Res = _Cnt;
			break;
		}
		for (int i = 0; i &lt; 4; i++) {
			int _dx = _Pos.first + _DxDy[i][0];
			int _dy = _Pos.second + _DxDy[i][1];
			if (_dx >= _N || _dy >= _N || _dx &lt; 0 || _dy &lt; 0)
				continue;
			if (_IsVisted[_dx][_dy]) continue;
			_IsVisted[_dx][_dy] = true;
			if (!_Map[_dx][_dy]) _BfsQueue.push(make_pair(_Cnt + 1, make_pair(_dx, _dy)));
			else _BfsQueue.push(make_pair(_Cnt, make_pair(_dx, _dy)));
		}
	}

	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"><img decoding="async" width="1209" height="95" src="https://lycos7560.com/wp-content/uploads/2023/07/image-36.png" alt="" class="wp-image-36144" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-36.png 1209w, https://lycos7560.com/wp-content/uploads/2023/07/image-36-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-36-768x60.png 768w" sizes="(max-width: 1209px) 100vw, 1209px" /></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-2665%eb%b2%88-%eb%af%b8%eb%a1%9c%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/36123/">백준 2665번 (미로만들기, 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-2665%eb%b2%88-%eb%af%b8%eb%a1%9c%eb%a7%8c%eb%93%a4%ea%b8%b0-c-baekjoon/36123/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1981번 (배열에서 이동, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1981%eb%b2%88-%eb%b0%b0%ec%97%b4%ec%97%90%ec%84%9c-%ec%9d%b4%eb%8f%99-c-baekjoon/36048/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1981%eb%b2%88-%eb%b0%b0%ec%97%b4%ec%97%90%ec%84%9c-%ec%9d%b4%eb%8f%99-c-baekjoon/36048/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 14 Jul 2023 04:03:57 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Search]]></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[백준 1981]]></category>
		<category><![CDATA[백준 1981번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[이분탐색]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[탐색]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36048</guid>

					<description><![CDATA[<p>백준 1981번 '배열에서 이동' 문제에 대한 글입니다. 이분탐색과 BFS 탐색을 이용하여 해결하였습니다. (This article is about BaekJoon Number1981 'move in array' problem. It was solved using the binary search and the BFS search.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1981%eb%b2%88-%eb%b0%b0%ec%97%b4%ec%97%90%ec%84%9c-%ec%9d%b4%eb%8f%99-c-baekjoon/36048/">백준 1981번 (배열에서 이동, 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-02b04adf      "
					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/1981" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1981</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">8617</td><td class="has-text-align-left" data-align="left">2269</td><td class="has-text-align-left" data-align="left">1476</td><td class="has-text-align-left" data-align="left">24.449%</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>n×n짜리의 배열이 하나 있다. 이 배열의 (1, 1)에서 (n, n)까지 이동하려고 한다. </p>



<p>이동할 때는 상, 하, 좌, 우의 네 인접한 칸으로만 이동할 수 있다.</p>



<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>첫째 줄에 n(2 ≤ n ≤ 100)이 주어진다. </p>



<p>다음 n개의 줄에는 배열이 주어진다. </p>



<p>배열의 각 수는 0보다 크거나 같고, 200보다 작거나 같은 정수이다.</p>



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



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



<p>첫째 줄에 (최대 &#8211; 최소)가 가장 작아질 때의 그 값을 출력한다.</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 1 3 6 8
1 2 2 5 5
4 4 0 3 3
8 0 2 3 4
4 3 0 2 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="">2</pre>



<div style="height:40px" 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> > <a href="https://www.acmicpc.net/category/213" target="_blank" rel="noreferrer noopener">Tu-Darmstadt Programming Contest</a> > <a href="https://www.acmicpc.net/category/detail/903" target="_blank" rel="noreferrer noopener">TUD Contest 2006</a> 6번</p>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a> > <a href="https://www.acmicpc.net/category/106" target="_blank" rel="noreferrer noopener">USA Computing Olympiad</a> > <a href="https://www.acmicpc.net/category/267" target="_blank" rel="noreferrer noopener">2002-2003 Season</a> > <a href="https://www.acmicpc.net/category/754" target="_blank" rel="noreferrer noopener">USACO US Open 2003 Contest</a> > <a href="https://www.acmicpc.net/category/detail/3416" target="_blank" rel="noreferrer noopener">Green</a> 1번</p>



<ul class="wp-block-list">
<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/kdk8361" target="_blank" rel="noreferrer noopener">kdk8361</a>, <a href="https://www.acmicpc.net/user/yclock" target="_blank" rel="noreferrer noopener">yclock</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/12" 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>범위를 확인할 때 이분탐색을 이용하여 시간 복잡도를 낮추어야 한다.</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;

int _N, _Res = 1000;
int _inMax = -INT32_MAX, _inMin = INT32_MAX;
int _DxDy[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
int _Map[101][101], _IsVisted[101][101];
int _End, _Start, _Mid;

queue&lt;pair&lt;int, int>> _BFSQueue;

bool BFS(int _Dif)
{
	for (int i = _inMin; i &lt;= _inMax; i++) {
		for (int j = 1; j &lt;= _N; j++)
			for (int k = 1; k &lt;= _N; k++)
				_IsVisted[j][k] = true;

		// 해당 범위에 속한다면 방문체크  해제(탐색을 위함)  
		for (int j = 1; j &lt;= _N; j++)
			for (int k = 1; k &lt;= _N; k++)
				if (i &lt;= _Map[j][k] &amp;&amp; _Map[j][k] &lt;= i + _Dif)
					_IsVisted[j][k] = false;
		// 도착지점이 성립할 수 없는 경우
		if (i > _Map[_N][_N] || _Map[_N][_N] > i + _Dif) 
			continue;
		while (!_BFSQueue.empty())
			_BFSQueue.pop();
		_BFSQueue.push(make_pair(1, 1));
		while (!_BFSQueue.empty()) {
			pair&lt;int, int> _Pos = _BFSQueue.front();
			_BFSQueue.pop();
			if (_IsVisted[_Pos.first][_Pos.second]) continue;
			_IsVisted[_Pos.first][_Pos.second] = true;
			if (_Pos.first == _N &amp;&amp; _Pos.second == _N)
				return true;
			int _dx, _dy;
			for (int i = 0; i &lt; 4; i++) {
				_dx = _Pos.first + _DxDy[i][0];
				_dy = _Pos.second + _DxDy[i][1];
				if (_dx > _N || _dy > _N || _dx &lt;= 0 || _dy &lt;= 0) continue;
				_BFSQueue.push(make_pair(_dx, _dy));
			}
		}
	}

	return false;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N;
	int _in;
	for (int i = 1; i &lt;= _N; i++)
		for (int j = 1; j &lt;= _N; j++) {
			cin >> _in;
			_Map[i][j] = _in;
			_inMax = max(_in, _inMax);
			_inMin = min(_in, _inMin);
		}

	_End = _inMax - _inMin;
	_Start = 0;
	while (_Start &lt;= _End) {
		_Mid = (_End + _Start) / 2;
		if (BFS(_Mid)) _End = _Mid - 1;
		else _Start = _Mid + 1;
	}
	cout &lt;&lt; _End + 1;
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1064" height="86" src="https://lycos7560.com/wp-content/uploads/2023/07/image-32.png" alt="" class="wp-image-36054" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-32.png 1064w, https://lycos7560.com/wp-content/uploads/2023/07/image-32-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-32-768x62.png 768w" sizes="(max-width: 1064px) 100vw, 1064px" /></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-1981%eb%b2%88-%eb%b0%b0%ec%97%b4%ec%97%90%ec%84%9c-%ec%9d%b4%eb%8f%99-c-baekjoon/36048/">백준 1981번 (배열에서 이동, 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-1981%eb%b2%88-%eb%b0%b0%ec%97%b4%ec%97%90%ec%84%9c-%ec%9d%b4%eb%8f%99-c-baekjoon/36048/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
