<?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>dfs Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/dfs/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Tue, 20 May 2025 07:14:23 +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>dfs Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 11375번 (열혈강호, C++)</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 20 May 2025 07:14:20 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11375]]></category>
		<category><![CDATA[11375번]]></category>
		<category><![CDATA[Baekjoon]]></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[백준 11375]]></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>
		<guid isPermaLink="false">https://lycos7560.com/?p=40040</guid>

					<description><![CDATA[<p>열혈강호 https://www.acmicpc.net/problem/11375 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 256 MB 16854 7509 4363 42.261% 문제 강호네 회사에는 직원이 N명이 있고, 해야할 일이 M개가 있다. 직원은 1번부터 N번까지 번호가 매겨져 있고, 일은 1번부터 M번까지 번호가 매겨져 있다. 각 직원은 자신이 할 수 있는 일들 중 한 개의 일만 담당할 수 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/">백준 11375번 (열혈강호, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">열혈강호 <img decoding="async" width="30" height="38" class="wp-image-37908" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



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



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



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



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



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



<p>강호네 회사에는 직원이 N명이 있고, 해야할 일이 M개가 있다. </p>



<p>직원은 1번부터 N번까지 번호가 매겨져 있고, 일은 1번부터 M번까지 번호가 매겨져 있다.</p>



<p>각 직원은 자신이 할 수 있는 일들 중 한 개의 일만 담당할 수 있고, 각각의 일을 담당하는 사람은 1명이어야 한다.</p>



<p>각각의 직원이 할 수 있는 일의 목록이 주어졌을 때, M개의 일 중에서 최대 몇 개를 할 수 있는지 구하는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에 직원의 수 N과 일의 개수 M이 주어진다. (1 ≤ N, M ≤ 1,000)</p>



<p>둘째 줄부터 N개의 줄의 i번째 줄에는 i번 직원이 할 수 있는 일의 개수와 할 수 있는 일의 번호가 주어진다.</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">예제 입력 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 5
2 1 2
1 1
2 2 3
3 3 4 5
1 1</pre>



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



<h3 class="wp-block-heading">예제 출력 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</pre>



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



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



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



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



<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;cstring>

using namespace std;

int N, M;
vector&lt;int> employee[1001];
int works[1001];       
bool visited[1001];      

bool DFS(int n) 
{

    for (int work : employee[n]) 
    {
        if (visited[work]) 
            continue;

        visited[work] = true;

        if (works[work] == 0 || DFS(works[work])) 
        {
            works[work] = n;
            return true;
        }
    }

    return false;
}

int main() 
{

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

    cin >> N >> M;

    for (int i = 1; i &lt;= N; i++) 
    {
        int k, work;
        cin >> k;

        for (int j = 0; j &lt; k; j++) 
        {
            cin >> work;
            employee[i].push_back(work);
        }
    }

    int result = 0;

    for (int i = 1; i &lt;= N; i++) 
    {
        memset(visited, false, sizeof(visited));

        if (DFS(i)) 
            result++;
    }

    cout &lt;&lt; result &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"><img fetchpriority="high" decoding="async" width="1041" height="79" src="https://lycos7560.com/wp-content/uploads/2025/05/image-10.png" alt="" class="wp-image-40041" srcset="https://lycos7560.com/wp-content/uploads/2025/05/image-10.png 1041w, https://lycos7560.com/wp-content/uploads/2025/05/image-10-300x23.png 300w, https://lycos7560.com/wp-content/uploads/2025/05/image-10-768x58.png 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></figure>



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



<p>해당 문제와 코드가 같은 문제</p>



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



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/">백준 11375번 (열혈강호, C++)</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-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2188번 (축사 배정, C++) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 18 May 2025 19:39:58 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2188]]></category>
		<category><![CDATA[2188번]]></category>
		<category><![CDATA[Baekjoon]]></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[백준 2188]]></category>
		<category><![CDATA[백준 2188번]]></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>
		<guid isPermaLink="false">https://lycos7560.com/?p=40032</guid>

					<description><![CDATA[<p>축사 배정 https://www.acmicpc.net/problem/2188 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 128 MB 13113 6284 4114 48.785% 문제 농부 존은 소 축사를 완성하였다. 축사 환경을 쾌적하게 유지하기 위해서, 존은 축사를 M개의 칸으로 구분하고, 한 칸에는 최대 한 마리의 소만 들어가게 계획했다. 첫 주에는 소를&#160;임의 배정해서&#160;축사를 운영했으나, 곧 문제가 발생하게 되었다. 바로 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/">백준 2188번 (축사 배정, C++) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">축사 배정 <img decoding="async" width="30" height="38" class="wp-image-37908" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



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



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



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



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



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



<p>농부 존은 소 축사를 완성하였다. 축사 환경을 쾌적하게 유지하기 위해서, 존은 축사를 M개의 칸으로 구분하고, 한 칸에는 최대 한 마리의 소만 들어가게 계획했다.</p>



<p>첫 주에는 소를&nbsp;임의 배정해서&nbsp;축사를 운영했으나, 곧 문제가 발생하게 되었다. 바로 소가 자신이&nbsp;희망하는 몇 개의 축사 외에는 들어가기를 거부하는 것이다.</p>



<p>농부 존을&nbsp;도와 최대한 많은 수의 소가 축사에 들어갈 수 있도록 하는 프로그램을 작성하시오. </p>



<p>축사의 번호는 1부터 M까지 매겨져 있다.</p>



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



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



<p>첫째 줄에 소의 수 N과 축사의 수 M이 주어진다. (1 ≤ N, M ≤ 200)</p>



<p>둘째 줄부터 N개의 줄에는 각 소가 들어가기 원하는 축사에 대한 정보가 주어진다. </p>



<p>i번째 소가 들어가기 원하는 축사의 수 S<sub>i</sub>&nbsp;(0 ≤ S<sub>i</sub>&nbsp;≤ M)이 먼저 주어지고, 이후 S<sub>i</sub>개의 축사 번호가 주어진다. </p>



<p>같은 축사 번호가 두 번 이상 주어지는 경우는 없다.</p>



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



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



<p>첫째 줄에 축사에 들어갈 수 있는 소의 최댓값을 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2</pre>



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



<h3 class="wp-block-heading">예제 출력 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</pre>



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



<p><a href="https://www.acmicpc.net/board/view/137221" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/board/view/137221</a></p>



<h3 class="wp-block-heading">예제 입력 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 3
1 1
1 2
1 3
1 3</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">3</pre>



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



<h3 class="wp-block-heading">예제 입력 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="">4 3
0
0
0
0</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">0</pre>



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



<h3 class="wp-block-heading">예제 입력 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 3
1 1
1 2
2 1 2
2 1 2</pre>



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



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



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">50 40
2 24 21
1 39
4 30 31 15 18
2 21 3
4 21 40 39 30
1 20
5 20 30 4 29 25
3 39 9 33
1 32
5 27 22 36 2 11
1 9
5 35 29 14 38 1
3 28 22 1
4 18 20 29 22
3 5 11 38
2 36 30
5 9 17 35 10 19
3 18 31 7
3 4 36 30
4 24 12 9 14
3 28 2 27
3 13 6 2
3 6 37 39
4 5 26 15 16
3 25 6 36
2 9 31
3 17 6 7
5 27 4 13 29 30
1 1
1 12
5 3 1 38 8 5
2 14 12
3 17 39 12
3 30 15 26
2 21 3
3 7 6 11
2 28 13
2 25 16
2 19 16
1 13
5 39 24 34 29 33
2 40 10
3 6 35 40
1 37
2 24 35
4 11 22 3 29
3 19 7 16
1 14
1 38
5 31 14 28 19 6</pre>



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



<h3 class="wp-block-heading">예제 출력 D</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="">39</pre>



<div style="height:25px" 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/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a>,&nbsp;<a href="https://www.acmicpc.net/user/rubix" target="_blank" rel="noreferrer noopener">rubix</a></li>



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



<div style="height:25px" 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/13" target="_blank" rel="noreferrer noopener">이분 매칭</a></li>
</ul>



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



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



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

using namespace std;

int N, M;
vector&lt;int> cowInfo[201]; // 각 소가 원하는 축사 리스트
int barnMatch[201];       // barnMatch[b] = 소 번호 (b번 축사에 배정된 소)
bool visited[201];        // DFS 방문 체크

// 축사 매칭 시도
bool DFS(int cow) 
{

    for (int barn : cowInfo[cow]) 
    {
        if (visited[barn]) 
            continue;

        visited[barn] = true;

        // 이 축사가 비었거나, 이미 배정된 소가 다른 축사로 갈 수 있다면
        if (barnMatch[barn] == 0 || DFS(barnMatch[barn])) 
        {
            barnMatch[barn] = cow;
            return true;
        }
    }

    return false;
}

int main() 
{

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

    cin >> N >> M;

    for (int i = 1; i &lt;= N; i++) 
    {
        int k, barn;
        cin >> k;

        for (int j = 0; j &lt; k; j++) 
        {
            cin >> barn;
            cowInfo[i].push_back(barn);
        }
    }

    int result = 0;

    for (int i = 1; i &lt;= N; i++) 
    {
        memset(visited, false, sizeof(visited));

        if (DFS(i)) 
            result++;
    }

    cout &lt;&lt; result &lt;&lt; "\n";

    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="1043" height="40" src="https://lycos7560.com/wp-content/uploads/2025/05/image-9.png" alt="" class="wp-image-40035" srcset="https://lycos7560.com/wp-content/uploads/2025/05/image-9.png 1043w, https://lycos7560.com/wp-content/uploads/2025/05/image-9-300x12.png 300w, https://lycos7560.com/wp-content/uploads/2025/05/image-9-768x29.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></figure>



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



<p>백준에 해당 문제와 코드가 같은 문제</p>



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



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/">백준 2188번 (축사 배정, 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-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1240번 (노드사이의 거리, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1240%eb%b2%88-%eb%85%b8%eb%93%9c%ec%82%ac%ec%9d%b4%ec%9d%98-%ea%b1%b0%eb%a6%ac-c-baekjoon/37874/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1240%eb%b2%88-%eb%85%b8%eb%93%9c%ec%82%ac%ec%9d%b4%ec%9d%98-%ea%b1%b0%eb%a6%ac-c-baekjoon/37874/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 04 Mar 2024 15:51:29 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1240]]></category>
		<category><![CDATA[1240 C++]]></category>
		<category><![CDATA[1240번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold5]]></category>
		<category><![CDATA[Recursive]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드5]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[깊이]]></category>
		<category><![CDATA[깊이 우선 탐색]]></category>
		<category><![CDATA[노드사이의 거리]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1240]]></category>
		<category><![CDATA[백준 1240번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[재귀]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=37874</guid>

					<description><![CDATA[<p>백준 온라인 문제 1240번 문제에 대한 글입니다.<br />
DFS 재귀 함수를 사용하여 해결하였습니다.  (This is about question 1240 of Baekjun Online Problem.<br />
Resolved using the DFS recursive function.)</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1240%eb%b2%88-%eb%85%b8%eb%93%9c%ec%82%ac%ec%9d%b4%ec%9d%98-%ea%b1%b0%eb%a6%ac-c-baekjoon/37874/">백준 1240번 (노드사이의 거리, 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></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-37889" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_5.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_5.jpg 1250w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_5-1200x1536.jpg 1200w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



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



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



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



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



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



<p> N개의 노드로 이루어진 트리가 주어지고 M개의 두 노드 쌍을 입력받을 때 두 노드 사이의 거리를 출력하라.</p>



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



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



<p>첫째 줄에 노드의 개수&nbsp;N과 거리를 알고 싶은 노드 쌍의 개수&nbsp;M이 입력되고 다음&nbsp;N-1개의 줄에 트리 상에 연결된 두 점과 거리를 입력받는다. </p>



<p>그 다음 줄에는 거리를 알고 싶은&nbsp;M개의 노드 쌍이 한 줄에 한 쌍씩 입력된다.</p>



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



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



<p> M개의 줄에 차례대로 입력받은 두 노드 사이의 거리를 출력한다.</p>



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



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



<ul class="wp-block-list">
<li> 2 ≤ N ≤1,000 </li>



<li> 1 ≤ M ≤ 1,000 </li>



<li>트리 상에 연결된 두 점과 거리는&nbsp;10,000&nbsp;이하인 자연수이다.</li>



<li>트리 노드의 번호는&nbsp;1부터&nbsp;N까지 자연수이며, 두 노드가 같은 번호를 갖는 경우는 없다.</li>
</ul>



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



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<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 2
2 1 2
4 3 2
1 4 3
1 2
3 2</pre>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<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
7</pre>
</div>
</div>



<div style="height:113px" 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/hg7258" target="_blank" rel="noreferrer noopener">hg7258</a></li>
</ul>



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



<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>DFS를 이용하여 탐색</p>



<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;vector>

using namespace std;

constexpr int MAX = 1001;

int N, M, _End;

vector&lt;int> Nodes[MAX];
vector&lt;int> Distances[MAX][MAX];
bool IsVisted[MAX];


void DFS(int _Len, int _CurNode)
{
	if (_End == _CurNode) {
		cout &lt;&lt; _Len &lt;&lt; '\n';
		return;
	}

	for (int i = 0; i &lt; Nodes[_CurNode].size(); i++)
	{
		int NextNode = Nodes[_CurNode][i];
		if (IsVisted[NextNode]) 
			continue;
		IsVisted[NextNode] = true;
		DFS(_Len + *Distances[_CurNode][NextNode].begin(), NextNode);
		IsVisted[NextNode] = false;
	}

}

int main()
{
	cin >> N >> M;
	int _S, _E, _D;
	for (int i = 0; i &lt; N-1; i++) {
		cin >> _S >> _E >> _D;
		Nodes[_S].push_back(_E);
		Nodes[_E].push_back(_S);
		Distances[_S][_E].push_back(_D);
		Distances[_E][_S].push_back(_D);
	}

	for (int i = 0; i &lt; M; i++) {
		cin >> _S >> _E;
		_End = _E;
		IsVisted[_S] = true;
		DFS(0, _S);
		IsVisted[_S] = false;
	}


	return 0;
}</pre>



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



<p>방문 체크를 빼먹어서 틀렸다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="830" height="120" src="https://lycos7560.com/wp-content/uploads/2024/03/image.png" alt="" class="wp-image-37875" srcset="https://lycos7560.com/wp-content/uploads/2024/03/image.png 830w, https://lycos7560.com/wp-content/uploads/2024/03/image-300x43.png 300w, https://lycos7560.com/wp-content/uploads/2024/03/image-768x111.png 768w" sizes="(max-width: 830px) 100vw, 830px" /></figure>



<div style="height:50px" 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="">    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    cin.tie(NULL);
    cout.tie(NULL);</pre>



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



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

using namespace std;

constexpr int MAX = 1001;

int N, M, _End;

vector&lt;int> Nodes[MAX];
vector&lt;int> Distances[MAX][MAX];
bool IsVisted[MAX];


void DFS(int _Len, int _CurNode)
{
	if (_End == _CurNode) {
		cout &lt;&lt; _Len &lt;&lt; '\n';
		return;
	}

	for (int i = 0; i &lt; Nodes[_CurNode].size(); i++)
	{
		int NextNode = Nodes[_CurNode][i];
		if (IsVisted[NextNode]) 
			continue;
		IsVisted[NextNode] = true;
		DFS(_Len + *Distances[_CurNode][NextNode].begin(), NextNode);
		IsVisted[NextNode] = false;
	}

}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    cin.tie(NULL);
    cout.tie(NULL);
	cin >> N >> M;
	int _S, _E, _D;
	for (int i = 0; i &lt; N-1; i++) {
		cin >> _S >> _E >> _D;
		Nodes[_S].push_back(_E);
		Nodes[_E].push_back(_S);
		Distances[_S][_E].push_back(_D);
		Distances[_E][_S].push_back(_D);
	}

	for (int i = 0; i &lt; M; i++) {
		cin >> _S >> _E;
		_End = _E;
		IsVisted[_S] = true;
		DFS(0, _S);
		IsVisted[_S] = false;
	}


	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="834" height="84" src="https://lycos7560.com/wp-content/uploads/2024/03/image-1.png" alt="" class="wp-image-37877" srcset="https://lycos7560.com/wp-content/uploads/2024/03/image-1.png 834w, https://lycos7560.com/wp-content/uploads/2024/03/image-1-300x30.png 300w, https://lycos7560.com/wp-content/uploads/2024/03/image-1-768x77.png 768w" sizes="(max-width: 834px) 100vw, 834px" /></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></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1240%eb%b2%88-%eb%85%b8%eb%93%9c%ec%82%ac%ec%9d%b4%ec%9d%98-%ea%b1%b0%eb%a6%ac-c-baekjoon/37874/">백준 1240번 (노드사이의 거리, 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-1240%eb%b2%88-%eb%85%b8%eb%93%9c%ec%82%ac%ec%9d%b4%ec%9d%98-%ea%b1%b0%eb%a6%ac-c-baekjoon/37874/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1967번 (트리의 지름, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1967%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36529/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 17 Aug 2023 00:01:03 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1967]]></category>
		<category><![CDATA[1967번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[Gold]]></category>
		<category><![CDATA[Gold4]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[골드]]></category>
		<category><![CDATA[골드4]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그래프이론]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[깊이]]></category>
		<category><![CDATA[깊이 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1967]]></category>
		<category><![CDATA[백준 1967번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트리]]></category>
		<category><![CDATA[트리의 지름]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36529</guid>

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



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


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


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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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

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

vector&lt;Node> Nodes;

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

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


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

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

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



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



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



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



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

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



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


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


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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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

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

vector&lt;Node> Nodes;

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

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


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

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

</pre>



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



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



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



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="4386247858"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/">백준 1167번 (트리의 지름, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1167%eb%b2%88-%ed%8a%b8%eb%a6%ac%ec%9d%98-%ec%a7%80%eb%a6%84-c-baekjoon/36474/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 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>백준 2251번 (물통, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2251%eb%b2%88-%eb%ac%bc%ed%86%b5-c-baekjoon/36162/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2251%eb%b2%88-%eb%ac%bc%ed%86%b5-c-baekjoon/36162/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 17 Jul 2023 00:07:07 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2251]]></category>
		<category><![CDATA[2251번]]></category>
		<category><![CDATA[Baekjoon]]></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[너비 우선 탐색]]></category>
		<category><![CDATA[물통]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2251]]></category>
		<category><![CDATA[백준 2251번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36162</guid>

					<description><![CDATA[<p>백준 2251번 '물통' 문제에 대한 블로그 글입니다. (This is a blog post about the problem of 'water bottle' in Baekjoon Number 2251.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2251%eb%b2%88-%eb%ac%bc%ed%86%b5-c-baekjoon/36162/">백준 2251번 (물통, 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-a6905183      "
					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/2251" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2251</a></p>



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



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">14437</td><td class="has-text-align-left" data-align="left">7329</td><td class="has-text-align-left" data-align="left">5406</td><td class="has-text-align-left" data-align="left">51.276%</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>각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. </p>



<p>처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. </p>



<p>이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부을 수 있는데, </p>



<p>이때에는 한 물통이 비거나, 다른 한 물통이 가득 찰 때까지 물을 부을 수 있다. </p>



<p>이 과정에서 손실되는 물은 없다고 가정한다.</p>



<p>이와 같은 과정을 거치다보면 세 번째 물통(용량이 C인)에 담겨있는 물의 양이 변할 수도 있다. </p>



<p>첫 번째 물통(용량이 A인)이 비어 있을 때, </p>



<p>세 번째 물통(용량이 C인)에 담겨있을 수 있는 물의 양을 모두 구해내는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에 세 정수 A, B, C가 주어진다.</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 9 10</pre>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2 8 9 10</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> > <a href="https://www.acmicpc.net/category/106" target="_blank" rel="noreferrer noopener">USA Computing Olympiad</a> > <a href="https://www.acmicpc.net/category/262" target="_blank" rel="noreferrer noopener">2000-2001 Season</a> > <a href="https://www.acmicpc.net/category/761" target="_blank" rel="noreferrer noopener">USACO Winter 2001 Contest</a> > <a href="https://www.acmicpc.net/category/detail/3427" target="_blank" rel="noreferrer noopener">Orange</a> 4번</p>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
using namespace std;
int _A, _B, _C;
bool _Check[201][201][201];
bool _Numbers[201];

void DFS(int _a, int _b, int _c)
{
	if (_a == 0) {
		_Check[_a][_b][_c] = true;
		_Numbers[_c] = true;
	}
	int  _tA, _tB, _tC;
	if (_a != 0) {
		_tB = _a + _b;
		if (_tB > _B) {
			_tA = _tB - _B;
			_tB = _B;
		}
		else _tA = 0;
		if (!_Check[_tA][_tB][_c]) {
			_Check[_tA][_tB][_c] = true;
			DFS(_tA, _tB, _c);
		}
			
		_tC = _a + _c;
		if (_tC > _C) {
			_tA = _tC - _C;
			_tC = _C;
		}
		else _tA = 0;
		if (!_Check[_tA][_b][_tC]) {
			_Check[_tA][_b][_tC] = true;
			DFS(_tA, _b, _tC);
		}
	}

	if (_b != 0) {
		_tA = _b + _a;
		if (_tA > _A) {
			_tB = _tA - _A;
			_tA = _A;
		}
		else _tB = 0;
		if (!_Check[_tA][_tB][_c]) {
			_Check[_tA][_tB][_c] = true;
			DFS(_tA, _tB, _c);
		}

		_tC = _b + _c;
		if (_tC > _C) {
			_tB = _tC - _C;
			_tC = _C;
		}
		else _tB = 0;
		if (!_Check[_a][_tB][_tC]) {
			_Check[_a][_tB][_tC] = true;
			DFS(_a, _tB, _tC);
		}
	}

	if (_c != 0) {
		_tA = _c + _a;
		if (_tA > _A) {
			_tC = _tA - _A;
			_tA = _A;
		}
		else _tC = 0;
		if (!_Check[_tA][_b][_tC]) {
			_Check[_tA][_b][_tC] = true;
			DFS(_tA, _b, _tC);
		}

		_tB = _c + _b;
		if (_tB > _B) {
			_tC = _tB - _B;
			_tB = _B;
		}
		else _tC = 0;
		if (!_Check[_a][_tB][_tC]) {
			_Check[_a][_tB][_tC] = true;
			DFS(_a, _tB, _tC);
		}
	}


}


int main()
{
	cin >> _A >> _B >> _C;
	_Check[0][0][_C];
	_Numbers[_C];
	DFS(0, 0, _C);
	for (int i = 0; i &lt;= _C; i++)
		if (_Numbers[i])
			cout &lt;&lt; i &lt;&lt; " ";

	return 0;
}</pre>



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



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



<div style="height:50px" 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-2251%eb%b2%88-%eb%ac%bc%ed%86%b5-c-baekjoon/36162/">백준 2251번 (물통, 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-2251%eb%b2%88-%eb%ac%bc%ed%86%b5-c-baekjoon/36162/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 21736번 (헌내기는 친구가 필요해, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-21736%eb%b2%88-%ed%97%8c%eb%82%b4%ea%b8%b0%eb%8a%94-%ec%b9%9c%ea%b5%ac%ea%b0%80-%ed%95%84%ec%9a%94%ed%95%b4-c-baekjoon/36032/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-21736%eb%b2%88-%ed%97%8c%eb%82%b4%ea%b8%b0%eb%8a%94-%ec%b9%9c%ea%b5%ac%ea%b0%80-%ed%95%84%ec%9a%94%ed%95%b4-c-baekjoon/36032/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 12 Jul 2023 22:14:25 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[Recursive]]></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[재귀]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36032</guid>

					<description><![CDATA[<p>백준 21736번 '헌내기는 친구가 필요해' 문제에 대한 글입니다. 재귀를 이용한 'DFS Search'을 이용하여 해결하였습니다. (Baekjoon Number 21736 This is an article about the problem of "The old man needs a friend." It was solved by using 'DFS Search' using recursive.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-21736%eb%b2%88-%ed%97%8c%eb%82%b4%ea%b8%b0%eb%8a%94-%ec%b9%9c%ea%b5%ac%ea%b0%80-%ed%95%84%ec%9a%94%ed%95%b4-c-baekjoon/36032/">백준 21736번 (헌내기는 친구가 필요해, 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-e89d25e2      "
					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/21736" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/21736</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 초 (<a href="https://www.acmicpc.net/problem/21736#" target="_blank" rel="noreferrer noopener">하단 참고</a>)</td><td class="has-text-align-left" data-align="left">1024 MB</td><td class="has-text-align-left" data-align="left">3403</td><td class="has-text-align-left" data-align="left">1990</td><td class="has-text-align-left" data-align="left">1668</td><td class="has-text-align-left" data-align="left">60.943%</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>2020년에 입학한 헌내기 도연이가 있다. 도연이는 비대면 수업 때문에 학교에&nbsp;가지 못해 학교에 아는 친구가 없었다. </p>



<p>드디어 대면 수업을 하게 된 도연이는 어서 캠퍼스 내의 사람들과 친해지고 싶다.&nbsp;</p>



<p>도연이가 다니는 대학의 캠퍼스는&nbsp;N x M&nbsp;크기이며&nbsp;캠퍼스에서 이동하는&nbsp;방법은 벽이 아닌 상하좌우로 이동하는 것이다. </p>



<p>예를 들어, 도연이가 (x,&nbsp;y)에 있다면 이동할 수 있는 곳은 (x+1,&nbsp;y), (x,&nbsp;y+1), (x-1,&nbsp;y), (x,&nbsp;y-1)이다. </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>첫째 줄에는 캠퍼스의 크기를 나타내는 두 정수&nbsp;N&nbsp;(1 ≤ N ≤ 600),&nbsp;M&nbsp;(1 ≤ M ≤ 600)이 주어진다.</p>



<p>둘째 줄부터&nbsp;N개의 줄에는 캠퍼스의 정보들이 주어진다.&nbsp;</p>



<p><code>O</code>는 빈 공간,&nbsp;<code>X</code>는 벽,&nbsp;<code>I</code>는 도연이,&nbsp;<code>P</code>는 사람이다.&nbsp;</p>



<p><code>I</code>가 한 번만 주어짐이 보장된다.</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>단, 아무도 만나지 못한 경우&nbsp;<code>TT</code>를 출력한다.</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
OOOPO
OIOOX
OOOXP</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: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 3
IOX
OXP
XPP</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="">TT</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>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/527" target="_blank" rel="noreferrer noopener">숙명여자대학교</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/2539" target="_blank" rel="noreferrer noopener">제1회 숙명여자대학교 교내 알고리즘 경진대회 (SMUPC)</a>&nbsp;C번</p>



<ul class="wp-block-list">
<li>문제를 검수한 사람:&nbsp;<a href="https://www.acmicpc.net/user/cs71107" target="_blank" rel="noreferrer noopener">cs71107</a>,&nbsp;<a href="https://www.acmicpc.net/user/lky7674" target="_blank" rel="noreferrer noopener">lky7674</a>,&nbsp;<a href="https://www.acmicpc.net/user/plast" target="_blank" rel="noreferrer noopener">plast</a>,&nbsp;<a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a>,&nbsp;<a href="https://www.acmicpc.net/user/songfox00" target="_blank" rel="noreferrer noopener">songfox00</a>,&nbsp;<a href="https://www.acmicpc.net/user/wbcho0504" target="_blank" rel="noreferrer noopener">wbcho0504</a>,&nbsp;<a href="https://www.acmicpc.net/user/whaeun25" target="_blank" rel="noreferrer noopener">whaeun25</a></li>



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



<div style="height:62px" 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>



<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>DFS 탐색을 재귀로 구현하여 문제를 해결</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>
using namespace std;
int _Res = 0, _N, _M;
int _DxDy[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
char _Map[600][600];
string _Str;
char _Temp;
pair&lt;int, int> StartPos;

// 재귀를 이용한 DFS (Depth-First Search) 탐색 
void DFSSearch(int _CntX, int _CntY)
{
	for (int j = 0; j &lt; 4; j++) {
		int _dx = _CntX + _DxDy[j][0];
		int _dy = _CntY + _DxDy[j][1];
		if (_dx >= _N || _dy >= _M || _dx &lt; 0 || _dy &lt; 0)
			continue;
		if (_Map[_dx][_dy] == 'X')
			continue;
		if (_Map[_dx][_dy] == 'P')
			_Res++;
		// 방문처리 대신에 X(벽)으로 마킹
		_Map[_dx][_dy] = 'X';
		DFSSearch(_dx, _dy);
	}
}

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] == 'I') {
				StartPos = make_pair(i, j);
				_Map[i][j] = 'X';
			}
		}
	}
	
	DFSSearch(StartPos.first, StartPos.second);

	if (_Res == 0) cout &lt;&lt; "TT"; 
	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="1064" height="45" src="https://lycos7560.com/wp-content/uploads/2023/07/image-31.png" alt="" class="wp-image-36040" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-31.png 1064w, https://lycos7560.com/wp-content/uploads/2023/07/image-31-300x13.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-31-768x32.png 768w" sizes="(max-width: 1064px) 100vw, 1064px" /></figure>



<div style="height:50px" 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-21736%eb%b2%88-%ed%97%8c%eb%82%b4%ea%b8%b0%eb%8a%94-%ec%b9%9c%ea%b5%ac%ea%b0%80-%ed%95%84%ec%9a%94%ed%95%b4-c-baekjoon/36032/">백준 21736번 (헌내기는 친구가 필요해, 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-21736%eb%b2%88-%ed%97%8c%eb%82%b4%ea%b8%b0%eb%8a%94-%ec%b9%9c%ea%b5%ac%ea%b0%80-%ed%95%84%ec%9a%94%ed%95%b4-c-baekjoon/36032/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 9205번 (맥주 마시면서 걸어가기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9205%eb%b2%88-%eb%a7%a5%ec%a3%bc-%eb%a7%88%ec%8b%9c%eb%a9%b4%ec%84%9c-%ea%b1%b8%ec%96%b4%ea%b0%80%ea%b8%b0-c-baekjoon/33247/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9205%eb%b2%88-%eb%a7%a5%ec%a3%bc-%eb%a7%88%ec%8b%9c%eb%a9%b4%ec%84%9c-%ea%b1%b8%ec%96%b4%ea%b0%80%ea%b8%b0-c-baekjoon/33247/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 18 Feb 2023 10:13:06 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[algorithm]]></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[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 9205]]></category>
		<category><![CDATA[백준 9205번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33247</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 9205번 '맥주 마시면서 걸어가기' 문제에 대한 글입니다. DFS 탐색을 이용하여 해결하였습니다. (This is an article on the issue of "Walking while Drinking Beer" in BAEKJOON No. 9205. Resolved using DFS Search.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9205%eb%b2%88-%eb%a7%a5%ec%a3%bc-%eb%a7%88%ec%8b%9c%eb%a9%b4%ec%84%9c-%ea%b1%b8%ec%96%b4%ea%b0%80%ea%b8%b0-c-baekjoon/33247/">백준 9205번 (맥주 마시면서 걸어가기, 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-a215ebf2      "
					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></ol>					</div>
									</div>
				</div>
			


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



<h1 class="wp-block-heading">맥주 마시면서 걸어가기</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/9205" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/9205</a></p>



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



<figure id="problem-info" 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">24063</td><td class="has-text-align-left" data-align="left">9720</td><td class="has-text-align-left" data-align="left">7109</td><td class="has-text-align-left" data-align="left">39.499%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size">송도에 사는 상근이와 친구들은 송도에서 열리는 펜타포트 락 페스티벌에 가려고 한다. </p>



<p class="has-medium-font-size">올해는 맥주를 마시면서 걸어가기로 했다. 출발은 상근이네 집에서 하고, 맥주 한 박스를 들고 출발한다. </p>



<p class="has-medium-font-size">맥주 한 박스에는 맥주가 20개 들어있다. 목이 마르면 안되기 때문에 50미터에 한 병씩 마시려고 한다. </p>



<p class="has-medium-font-size">즉, 50미터를 가려면 그 직전에 맥주 한 병을 마셔야 한다.</p>



<p class="has-medium-font-size">상근이의 집에서 페스티벌이 열리는 곳은 매우 먼 거리이다. </p>



<p class="has-medium-font-size">따라서, 맥주를 더 구매해야 할 수도 있다. </p>



<p class="has-medium-font-size">미리 인터넷으로 조사를 해보니 다행히도 맥주를 파는 편의점이 있다. </p>



<p class="has-medium-font-size">편의점에 들렸을 때, 빈 병은 버리고 새 맥주 병을 살 수 있다. </p>



<p class="has-medium-font-size">하지만, 박스에 들어있는 맥주는 20병을 넘을 수 없다. </p>



<p class="has-medium-font-size">편의점을 나선 직후에도 50미터를 가기 전에 맥주 한 병을 마셔야 한다.</p>



<p class="has-medium-font-size">편의점, 상근이네 집, 펜타포트 락 페스티벌의 좌표가 주어진다. </p>



<p class="has-medium-font-size">상근이와 친구들이 행복하게 페스티벌에 도착할 수 있는지 구하는 프로그램을 작성하시오.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 테스트 케이스의 개수 t가 주어진다. (t ≤ 50)</p>



<p class="has-medium-font-size">각 테스트 케이스의 첫째 줄에는 맥주를 파는 편의점의 개수 n이 주어진다. (0 ≤ n ≤ 100).</p>



<p class="has-medium-font-size">다음 n+2개 줄에는 상근이네 집, 편의점, 펜타포트 락 페스티벌 좌표가 주어진다. </p>



<p class="has-medium-font-size">각 좌표는 두 정수 x와 y로 이루어져 있다. (두 값 모두 미터, -32768 ≤ x, y ≤ 32767)</p>



<p class="has-medium-font-size">송도는 직사각형 모양으로 생긴 도시이다. </p>



<p class="has-medium-font-size">두 좌표 사이의 거리는 x 좌표의 차이 + y 좌표의 차이 이다. (맨해튼 거리)</p>



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



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



<p class="has-medium-font-size">각 테스트 케이스에 대해서 상근이와 친구들이 행복하게 페스티벌에 갈 수 있으면 &#8220;</p>



<p class="has-medium-font-size">happy&#8221;, 중간에 맥주가 바닥나서 더 이동할 수 없으면 &#8220;sad&#8221;를 출력한다. </p>



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



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



<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
2
0 0
1000 0
1000 1000
2000 1000
2
0 0
1000 0
2000 1000
2000 2000</pre>



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



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



<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="">happy
sad</pre>



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



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



<p><a href="https://www.acmicpc.net/category/1" target="_blank" rel="noreferrer noopener">ICPC</a> > <a href="https://www.acmicpc.net/category/7">Reg</a><a href="https://www.acmicpc.net/category/7" target="_blank" rel="noreferrer noopener">i</a><a href="https://www.acmicpc.net/category/7">onals</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/47" target="_blank" rel="noreferrer noopener">German Collegiate Programming Contest</a> > <a href="https://www.acmicpc.net/category/detail/1110" target="_blank" rel="noreferrer noopener">GCPC 2013</a> D번</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/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>



<li>어색한 표현을 찾은 사람: <a href="https://www.acmicpc.net/user/ljh6274" target="_blank" rel="noreferrer noopener">ljh6274</a></li>



<li>문제의 오타를 찾은 사람: <a href="https://www.acmicpc.net/user/mwy3055" target="_blank" rel="noreferrer noopener">mwy3055</a></li>



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



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



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



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



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



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



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



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



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



<p class="has-medium-font-size">50미터마다 맥주를 1병씩 소모,  최대 수량은 20병, 따라서 최대로 움직일 수 있는 거리는 1000미터다.</p>



<p class="has-medium-font-size">입력 받은 편의점의 위치에서 1000미터 내로 도달할 수 있는 편의점을 탐색 후 </p>



<p class="has-medium-font-size">해당 위치에서 다시 편의점을 찾는 방법으로 해결하였다. (재귀로 구현)</p>



<p class="has-medium-font-size">다른 분들의 풀이를 보면 BFS 탐색으로 풀 수 있는 듯 하다.</p>



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



<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;cmath>

using namespace std;

constexpr int MAX = 101;

int T, N;

vector&lt;pair&lt;int, int>> graph[MAX];

bool isVisted[MAX], check;

pair&lt;int, int> Start, Temp;

void DFSSearch(int x, int y)
{
	// 도착이 가능하다면 마킹하고 리턴
	if (x == graph[N][0].first &amp;&amp; y == graph[N][0].second) {
		check = true;
	}

	for (int i = 0; i &lt;= N; i++) {

		// 방문 했다면 넘어간다. 
		if (isVisted[i] == true) continue;
		// 거리의 절대값으로 도착이 가능한지 불가능한지 체크
		if (abs(graph[i][0].first - x) + abs(graph[i][0].second - y) &lt;= 1000) {

			isVisted[i] = true; // 방문 체크

			DFSSearch(graph[i][0].first, graph[i][0].second);
			// isVisted[i] = false; // 방문 체크를 해제하면 시간초과
		}
	}
}


int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> T;

	while (T--) {

		// 조건 초기화
		fill(isVisted, isVisted + MAX, false);

		for (int i = 0; i &lt; MAX; i++) graph[i].clear();

		check = false; // 가능한지 불가능한지 탐색

		// 시작 위치를 입력
		cin >> N >> Start.first >> Start.second;	

		for (int i = 0; i &lt;= N; i++) {

			// 편의점의 위치를 입력받는다.
			cin >> Temp.first >> Temp.second;
			// 마지막은 도착 위치를 입력받는다.
			graph[i].push_back(make_pair(Temp.first, Temp.second));

		}

		DFSSearch(Start.first, Start.second);
		
		if (check) cout &lt;&lt; "happy\n";
		else cout &lt;&lt; "sad\n";

	}

	return 0;
}</pre>



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



<p class="has-medium-font-size">재귀 탐색에서 사용하는 방문 체크 해제에 대한 문제로 시간 초과가 발생하였다.</p>



<p class="has-medium-font-size">DFS를 나갈 때 visited를 다시 false 변경할 필요가 있는지 생각</p>



<div style="height:20px" 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/02/image-84.png" alt="" class="wp-image-33251" width="1044" height="193" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-84.png 909w, https://lycos7560.com/wp-content/uploads/2023/02/image-84-300x55.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-84-768x142.png 768w" sizes="(max-width: 1044px) 100vw, 1044px" /></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="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-9205%eb%b2%88-%eb%a7%a5%ec%a3%bc-%eb%a7%88%ec%8b%9c%eb%a9%b4%ec%84%9c-%ea%b1%b8%ec%96%b4%ea%b0%80%ea%b8%b0-c-baekjoon/33247/">백준 9205번 (맥주 마시면서 걸어가기, 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-9205%eb%b2%88-%eb%a7%a5%ec%a3%bc-%eb%a7%88%ec%8b%9c%eb%a9%b4%ec%84%9c-%ea%b1%b8%ec%96%b4%ea%b0%80%ea%b8%b0-c-baekjoon/33247/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11724번 (연결 요소의 개수, C++, DFS) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11724%eb%b2%88-%ec%97%b0%ea%b2%b0-%ec%9a%94%ec%86%8c%ec%9d%98-%ea%b0%9c%ec%88%98-c-dfs-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5214/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11724%eb%b2%88-%ec%97%b0%ea%b2%b0-%ec%9a%94%ec%86%8c%ec%9d%98-%ea%b0%9c%ec%88%98-c-dfs-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5214/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 22 Jan 2023 01:58:07 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11724]]></category>
		<category><![CDATA[11724번]]></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[너비 우선 탐색]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11724]]></category>
		<category><![CDATA[백준 11724번]]></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>
		<guid isPermaLink="false">https://lycos7560.com/?p=5214</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 11724번 '연결 요소의 개수' 문제에 대한 글입니다. 'DFS'를 이용하여 해결하였습니다. (This is an article about the problem of 'number of connecting elements' in BAEKJOON 11724. I solved it using 'DFS'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11724%eb%b2%88-%ec%97%b0%ea%b2%b0-%ec%9a%94%ec%86%8c%ec%9d%98-%ea%b0%9c%ec%88%98-c-dfs-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5214/">백준 11724번 (연결 요소의 개수, C++, DFS) / 추가 반례 [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>



<h1 class="wp-block-heading">연결 요소의 개수</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/11724" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11724</a></p>



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



<figure id="problem-info" 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">3 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">86464</td><td class="has-text-align-left" data-align="left">39394</td><td class="has-text-align-left" data-align="left">25976</td><td class="has-text-align-left" data-align="left">42.560%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size">방향 없는 그래프가 주어졌을 때, 연결 요소 (Connected Component)의 개수를 구하는 프로그램을 작성하시오.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 정점의 개수 N과 간선의 개수 M이 주어진다. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) </p>



<p class="has-medium-font-size">둘째 줄부터 M개의 줄에 간선의 양 끝점 u와 v가 주어진다. (1 ≤ u, v ≤ N, u ≠ v) </p>



<p class="has-medium-font-size">같은 간선은 한 번만 주어진다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 연결 요소의 개수를 출력한다.</p>



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



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



<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 5
1 2
2 5
5 1
3 4
4 6</pre>



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



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



<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:60px" aria-hidden="true" class="wp-block-spacer"></div>



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



<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 8
1 2
2 5
5 1
3 4
4 6
5 4
2 4
2 3</pre>



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



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



<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:60px" aria-hidden="true" class="wp-block-spacer"></div>



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



<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 2
1 2
3 2</pre>



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



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



<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:60px" aria-hidden="true" class="wp-block-spacer"></div>



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



<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 3
3 4
4 6
9 6</pre>



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



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



<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</pre>



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



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



<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="">1000 0</pre>



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



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



<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="">1000</pre>



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



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



<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 0</pre>



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



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



<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:60px" aria-hidden="true" class="wp-block-spacer"></div>



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



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



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



<li>잘못된 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/songjuh" target="_blank" rel="noreferrer noopener">songjuh</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" 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>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1524" height="988" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230122_104023186.jpg" alt="" class="wp-image-5218" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230122_104023186.jpg 1524w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230122_104023186-300x194.jpg 300w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230122_104023186-768x498.jpg 768w" sizes="(max-width: 1524px) 100vw, 1524px" /></figure>



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



<p class="has-medium-font-size">자료구조는 2차원 배열을 사용하였다.</p>



<p class="has-medium-font-size">arr[N][0] 은 방문 처리 용도</p>



<p class="has-medium-font-size">arr[A][B] 가 true면 A &#8211; B (연결)을 나타낸다.</p>



<p class="has-medium-font-size">이 문제는 시간 제한과 메모리 제한을 보면 BFS / DFS 등 어느 것을 사용해도 상관없는 문제</p>



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



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



<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>

using namespace std;

int N, M, cnt = 0;

pair&lt;int, int> tempPair;

bool arr[1001][1001];

void CheckNode(int A) // DFS
{
	arr[A][0] = false; // 방문처리
	for (int i = 1; i &lt;= N; i++) {
		if (A == i) continue;

		// 양방향 탐색
		if ((arr[i][0] == true &amp;&amp; arr[i][A] == true) || (arr[i][0] == true &amp;&amp; arr[A][i] == true)) 
			CheckNode(i);
	}
}


int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> M;

	// 자기 자신은 항상 자기 자신의 연결 요소에 속한다.
	for (int i = 1; i &lt;= N; i++) arr[i][0] = true;


	for (int i = 0; i &lt; M; i++) {
		cin >> tempPair.first >> tempPair.second;
		arr[tempPair.first][tempPair.second] = true;
		arr[tempPair.first][0] = true;
		arr[tempPair.second][0] = true;
	}

	for (int i = 1; i &lt;= N; i++) {
		if (arr[i][0] == false) continue; // 방문처리가 되어있다면 넘어간다.
		else 
		{
			CheckNode(i);
			cnt++; // 연결요소 카운트 + 1
		}
	}

	cout &lt;&lt; cnt;

	return 0;
}</pre>



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



<p class="has-medium-font-size">자기 자신만 있는 경우를 연결 요소로 <em>카운트하지</em> 않아서 방황했다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1043" height="139" src="https://lycos7560.com/wp-content/uploads/image-1028.png" alt="" class="wp-image-5217" srcset="https://lycos7560.com/wp-content/uploads/image-1028.png 1043w, https://lycos7560.com/wp-content/uploads/image-1028-300x40.png 300w, https://lycos7560.com/wp-content/uploads/image-1028-768x102.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></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="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-11724%eb%b2%88-%ec%97%b0%ea%b2%b0-%ec%9a%94%ec%86%8c%ec%9d%98-%ea%b0%9c%ec%88%98-c-dfs-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5214/">백준 11724번 (연결 요소의 개수, C++, DFS) / 추가 반례 [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-11724%eb%b2%88-%ec%97%b0%ea%b2%b0-%ec%9a%94%ec%86%8c%ec%9d%98-%ea%b0%9c%ec%88%98-c-dfs-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5214/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
