<?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>Backtracking Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/backtracking/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Wed, 09 Aug 2023 02:48:32 +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>Backtracking Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 fetchpriority="high" 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>백준 1799번 (비숍, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1799%eb%b2%88-%eb%b9%84%ec%88%8d-c-baekjoon/36346/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1799%eb%b2%88-%eb%b9%84%ec%88%8d-c-baekjoon/36346/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 27 Jul 2023 08:42:42 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1799]]></category>
		<category><![CDATA[1799번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1799]]></category>
		<category><![CDATA[백준 1799번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36346</guid>

					<description><![CDATA[<p>비숍 https://www.acmicpc.net/problem/1799 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 10 초 128 MB 22527 5606 3885 24.324% 문제 서양 장기인 체스에는 대각선 방향으로 움직일 수 있는 비숍(bishop)이 있다. &#60; 그림 1 >과 같은 정사각형 체스판 위에 B라고 표시된 곳에 비숍이 있을 때 비숍은 대각선 방향으로 움직여 O로 표시된 칸에 있는 다른 말을 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1799%eb%b2%88-%eb%b9%84%ec%88%8d-c-baekjoon/36346/">백준 1799번 (비숍, 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-5013bcb9      "
					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="#통과된-코드" 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/1799" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1799</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">10 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">22527</td><td class="has-text-align-left" data-align="left">5606</td><td class="has-text-align-left" data-align="left">3885</td><td class="has-text-align-left" data-align="left">24.324%</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>서양 장기인 체스에는 대각선 방향으로 움직일 수 있는 비숍(bishop)이 있다. </p>



<p>&lt; 그림 1 >과 같은 정사각형 체스판 위에 B라고 표시된 곳에 비숍이 있을 때 </p>



<p>비숍은 대각선 방향으로 움직여 O로 표시된 칸에 있는 다른 말을 잡을 수 있다.</p>



<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/07/image-52.png" alt="" class="wp-image-36347" width="293" height="335" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-52.png 391w, https://lycos7560.com/wp-content/uploads/2023/07/image-52-263x300.png 263w" sizes="(max-width: 293px) 100vw, 293px" /></figure>



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



<p>그런데 체스판 위에는 비숍이 놓일 수 없는 곳이 있다. </p>



<p>&lt; 그림 2 >에서 체스판에 색칠된 부분은 비숍이 놓일 수 없다고 하자. </p>



<p>이와 같은 체스판에 서로가 서로를 잡을 수 없도록 하면서 비숍을 놓는다면 &lt; 그림 3 >과 같이 최대 7개의 비숍을 놓을 수 있다.  </p>



<p>색칠된 부분에는 비숍이 놓일 수 없지만 지나갈 수는 있다.</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/07/image-53.png" alt="" class="wp-image-36352" width="296" height="597" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-53.png 395w, https://lycos7560.com/wp-content/uploads/2023/07/image-53-149x300.png 149w" sizes="(max-width: 296px) 100vw, 296px" /></figure>



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



<p>정사각형 체스판의 한 변에 놓인 칸의 개수를 체스판의 크기라고 한다. </p>



<p>체스판의 크기와 체스판 각 칸에 비숍을 놓을 수 있는지 없는지에 대한 정보가 주어질 때, </p>



<p>서로가 서로를 잡을 수 없는 위치에 놓을 수 있는 비숍의 최대 개수를 구하는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에 체스판의 크기가 주어진다. </p>



<p>체스판의 크기는 10이하의 자연수이다. </p>



<p>둘째 줄부터 아래의 예와 같이 체스판의 각 칸에 비숍을 놓을 수 있는지 없는지에 대한 정보가 체스판 한 줄 단위로 한 줄씩 주어진다. </p>



<p>비숍을 놓을 수 있는 곳에는 1, 비숍을 놓을 수 없는 곳에는 0이 빈칸을 사이에 두고 주어진다.</p>



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



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



<p>첫째 줄에 주어진 체스판 위에 놓을 수 있는 비숍의 최대 개수를 출력한다.</p>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
1 1 0 1 1
0 1 0 0 0
1 0 1 0 1
1 0 0 0 0
1 0 1 1 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="">7</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/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a> > <a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a> > <a href="https://www.acmicpc.net/category/68" target="_blank" rel="noreferrer noopener">지역본선 2007</a> > <a href="https://www.acmicpc.net/category/detail/361" target="_blank" rel="noreferrer noopener">초등부</a> 5번</p>



<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/5">백트래킹</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)"/>



<p>비숍은 같은 색깔의 비숍만 잡을 수 있다.</p>



<p>다른 색에 있는 비숍까지 모든 경우의 수를 고려한다면 시간 초과가 발생</p>



<p>아래의 코드는 재귀적으로 BackTracking을 구현한 코드</p>



<p>N x N 크기의 체스판에서 N개의 퀸을 배치하는 모든 경우를 탐색</p>



<p>각 칸마다 O(N) 시간이 소요되고, N x N 크기의 체스판에 N개의 퀸을 배치하므로 </p>



<p>총 시간 복잡도는 O(N^N) => 시간초과 </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>
using namespace std;
constexpr int _MAX = 10;
int _N, _Res;
bool _Map[_MAX][_MAX];
bool _Dx[19], _Dy[19];

void BackTracking(int _Cnt, int _x)
{
	int _dxy, _sum;
	for (int i = _x; i &lt; _N; i++)
		for (int j = 0; j &lt; _N; j++) {
			if (!_Map[i][j]) continue;
			_dxy = i - j + _N - 1;
			_sum = i + j;
			if (_Dx[_dxy] || _Dy[i + j]) continue;
			_Dx[_dxy] = true;
			_Dy[_sum] = true;
			BackTracking(_Cnt + 1, i);
			_Dx[_dxy] = false;
			_Dy[_sum] = false;
		}

	_Res = max(_Res, _Cnt);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N;
	for (int i = 0; i &lt; _N; i++)
		for (int j = 0; j &lt; _N; j++)
			cin >> _Map[i][j];
	BackTracking(0, 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" src="https://lycos7560.com/wp-content/uploads/2023/07/image-55.png" alt="" class="wp-image-36367" width="1088" height="59" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-55.png 1450w, https://lycos7560.com/wp-content/uploads/2023/07/image-55-300x16.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-55-768x42.png 768w" sizes="(max-width: 1088px) 100vw, 1088px" /></figure>



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



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



<p>아래의 코드는 하얀 칸에 올 수 있는 비숍의 최댓값과  </p>



<p>검은칸에 올 수 있는 비숍의 최댓값을 따로 구한 후 합한 결과를 출력해준다.</p>



<p>비숍을 해당 칸에 놓는 경우 / 안 놓는 경우 => 2가지</p>



<p>N / 2 칸을 N^2으로 탐색하지만 흑/백 2번이므로 => N^2</p>



<p>약 O(2^N^2)의 시간 복잡도를 가진다.</p>



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



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
using namespace std;
constexpr int _MAX = 10;
int _N, _Res, _ResSum;
bool _Map[_MAX][_MAX];
bool _CheckX[19], _CheckY[19];

void CheckBishop(int _bishopCnt, int _x, bool _isOdd)
{
	int _dxy, _sum;
	for (int i = _x; i &lt; _N; i++)
		for (int j = 0; j &lt; _N; j++) {
			if (!_Map[i][j]) continue;
			_dxy = i - j + _N - 1;
			_sum = i + j;
			if (_CheckX[_dxy] || _CheckY[i + j] || _sum % 2 == _isOdd) continue;
			_CheckX[_dxy] = true;
			_CheckY[_sum] = true;
			CheckBishop(_bishopCnt + 1, i, _isOdd);
			_CheckX[_dxy] = false;
			_CheckY[_sum] = false;
		}

	_Res = max(_Res, _bishopCnt);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N;
	for (int i = 0; i &lt; _N; i++)
		for (int j = 0; j &lt; _N; j++)
			cin >> _Map[i][j];
	CheckBishop(0, 0, false);
	_ResSum += _Res;
	_Res = 0;
	CheckBishop(0, 0, true);
	_ResSum += _Res;
	cout &lt;&lt; _ResSum;
	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/07/image-54.png" alt="" class="wp-image-36360" width="1083" height="84" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-54.png 1444w, https://lycos7560.com/wp-content/uploads/2023/07/image-54-300x23.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-54-768x60.png 768w" sizes="(max-width: 1083px) 100vw, 1083px" /></figure>



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



<p>제출된 답 중에서 0ms 도 있다&#8230; 세상에는 괴물이 많다.</p>



<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-1799%eb%b2%88-%eb%b9%84%ec%88%8d-c-baekjoon/36346/">백준 1799번 (비숍, 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-1799%eb%b2%88-%eb%b9%84%ec%88%8d-c-baekjoon/36346/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 9663번 (N-Queen, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9663%eb%b2%88-n-queen-c-baekjoon/34814/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9663%eb%b2%88-n-queen-c-baekjoon/34814/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 19 May 2023 04:37:46 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[<algorithm>]]></category>
		<category><![CDATA[9663]]></category>
		<category><![CDATA[9663번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BruteForce]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N-Queen]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 9663]]></category>
		<category><![CDATA[백준 9663번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[브루트 포스]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34814</guid>

					<description><![CDATA[<p>백준 9663번 'N-Queen' 문제에 대한 글입니다. 백트레킹을 이용하여 해결하였습니다. (This article is about the question of "N-Queen" in Baekjun 9663. It was solved using backtracking.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9663%eb%b2%88-n-queen-c-baekjoon/34814/">백준 9663번 (N-Queen, 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-b04bb7c8      "
					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="#n-queen" class="uagb-toc-link__trigger">N-Queen</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a><li class="uagb-toc__list"><a href="#더-효율적인-코드" class="uagb-toc-link__trigger">더 효율적인 코드</a></ol>					</div>
									</div>
				</div>
			


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



<h1 class="wp-block-heading">N-Queen</h1>



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



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



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



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



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



<p class="has-medium-font-size">N-Queen 문제는&nbsp;크기가 N × N인 체스판 위에 퀸 N개를&nbsp;서로 공격할 수 없게 놓는&nbsp;문제이다.</p>



<p class="has-medium-font-size">N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 N이 주어진다. (1 ≤ N &lt; 15)</p>



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



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



<p class="has-medium-font-size">첫째 줄에 퀸 N개를 서로 공격할 수 없게 놓는&nbsp;경우의 수를 출력한다.</p>



<div style="height:62px" 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="">5 17</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:40px" aria-hidden="true" class="wp-block-spacer"></div>



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



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon">baekjoon</a></li>
</ul>



<div style="height:62px" 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/125">브루트포스 알고리즘</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/5">백트래킹</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)"/>



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



<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/05/KakaoTalk_20230519_111808847.jpg" alt="" class="wp-image-34817" width="1139" height="1440" srcset="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230519_111808847.jpg 1519w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230519_111808847-237x300.jpg 237w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230519_111808847-768x971.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230519_111808847-1215x1536.jpg 1215w" sizes="(max-width: 1139px) 100vw, 1139px" /></figure>



<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;cmath>
using namespace std;
constexpr int Max = 15; // 체스판의 최대 
int _N, _Res, _Queen[Max];

bool CheckQueens(int _row)
{
	for (int i = 0; i &lt; _row; i++) {
		// 절대값으로 대각선으로 전부 확인
		if (_Queen[i] == _Queen[_row] || ( abs(_Queen[i] - _Queen[_row]) == abs(i - _row))) {
			return false;
		}
	}
	return true;
}

void BackTracing(int _row) {
	// N번째까지 왔다면 경우의 수 추가
	if (_row == _N) {
		_Res++;
		return; 
	}

	for (int _col = 0; _col &lt; _N; _col++) {
		_Queen[_row] = _col;
		//해당 행이 가능한지 확인한다.
		if (CheckQueens(_row)) 
			BackTracing(_row + 1);
	}
}

int main()
{
	cin >> _N; // 체스판의 크기 입력
	BackTracing(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" src="https://lycos7560.com/wp-content/uploads/2023/05/image-88.png" alt="" class="wp-image-34820" width="904" height="73" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-88.png 1205w, https://lycos7560.com/wp-content/uploads/2023/05/image-88-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-88-768x62.png 768w" sizes="(max-width: 904px) 100vw, 904px" /></figure>



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



<h1 class="wp-block-heading">더 효율적인 코드</h1>



<p class="has-medium-font-size">DFS 탐색을 이용한 백트레킹</p>



<p class="has-medium-font-size">dy dx를 대각선으로 표현하여 검사</p>



<figure class="wp-block-image size-full"><img decoding="async" width="400" height="396" src="https://lycos7560.com/wp-content/uploads/2023/05/image-90.png" alt="" class="wp-image-34825" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-90.png 400w, https://lycos7560.com/wp-content/uploads/2023/05/image-90-300x297.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-90-150x150.png 150w" sizes="(max-width: 400px) 100vw, 400px" /></figure>



<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;
constexpr int Max = 15;
int _N, _Res;
// 체스판 
bool MapArr[Max];
//체스판의 대각선을 표현하기위한 변수
//대각선은 2N - 1개 필요
bool dx[29], dy[29];

void BackTracking(int level)
{
	if (level == _N) {
		_Res++;
		return;
	}
	for (int i = 0; i &lt; _N; i++) {
		//현재 열, 왼쪽 대각선 및 오른쪽 대각선에 이미 배치된 여왕이 없는지 확인
		// dx, dy는 체스판의 특정한 위치를 나타내는 변수가 아니다.
		// dx, dy를 이용하여 대각선을 추적하는 것
		// 배열의 각 인덱스는 dx대각선을 나타내며 해당 인덱스의 값은 대각선이 여왕에 의해 점유되는지 여부를 나타냅니다. 
		if (!MapArr[i] &amp;&amp; !dx[i - level + _N] &amp;&amp; !dy[i + level]) {
			
			MapArr[i] = dx[i - level + _N] = dy[i + level] = true;
			BackTracking(level + 1);
			MapArr[i] = dx[i - level + _N] = dy[i + level] = false;
		}
	}
}

int main() {
	_Res = 0;
	cin >> _N;
	BackTracking(0);
	cout &lt;&lt; _Res;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1038" height="99" src="https://lycos7560.com/wp-content/uploads/2023/05/image-89.png" alt="" class="wp-image-34821" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-89.png 1038w, https://lycos7560.com/wp-content/uploads/2023/05/image-89-300x29.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-89-768x73.png 768w" sizes="(max-width: 1038px) 100vw, 1038px" /></figure>



<p></p>



<p></p>



<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-9663%eb%b2%88-n-queen-c-baekjoon/34814/">백준 9663번 (N-Queen, 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-9663%eb%b2%88-n-queen-c-baekjoon/34814/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 15666번 (N과 M (12), C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15666%eb%b2%88-n%ea%b3%bc-m-12-c-baekjoon/34467/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15666%eb%b2%88-n%ea%b3%bc-m-12-c-baekjoon/34467/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 08 May 2023 10:19:23 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[15666]]></category>
		<category><![CDATA[15666번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N과 M (12)]]></category>
		<category><![CDATA[SET]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 15666]]></category>
		<category><![CDATA[백준 15666번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34467</guid>

					<description><![CDATA[<p>백준 15666번 'N과 M (12)' 문제에 대한 글입니다. (This is an article on the question "N and M (12)" of Baekjoon  Number 15666.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15666%eb%b2%88-n%ea%b3%bc-m-12-c-baekjoon/34467/">백준 15666번 (N과 M (12), 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-d69170fb      "
					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="#n과-m-12" class="uagb-toc-link__trigger">N과 M (12)</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">N과 M (12)</h1>



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



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



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



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



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



<p class="has-medium-font-size">N개의 자연수와 자연수 M이 주어졌을 때, </p>



<p class="has-medium-font-size">아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>



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



<p>N개의 자연수 중에서 M개를 고른 수열</p>



<p>같은 수를 여러 번 골라도 된다.</p>



<p>고른 수열은 비내림차순이어야 한다.</p>



<p> => 길이가 K인 수열 A가 A<sub>1</sub> ≤ A<sub>2</sub> ≤ &#8230; ≤ A<sub>K-1</sub> ≤ A<sub>K</sub>를 만족하면, 비내림차순이라고 한다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>



<p class="has-medium-font-size">둘째 줄에 N개의 수가 주어진다. </p>



<p class="has-medium-font-size">입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</p>



<div style="height:62px" 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">수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p>



<div style="height:62px" 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="">3 1
4 4 2</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
4</pre>



<div style="height:50px" 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="">4 2
9 7 9 1</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 1
1 7
1 9
7 7
7 9
9 9</pre>



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



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



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



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



<div style="height:50px" 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/chogahui05" target="_blank" rel="noreferrer noopener">chogahui05</a>, <a href="https://www.acmicpc.net/user/thak00" target="_blank" rel="noreferrer noopener">thak00</a></li>
</ul>



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



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



<p class="has-medium-font-size">자료구조 set을 이용하여 숫자의 중복을 제거 및 정렬</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;set>

using namespace std;

int _N, _M, _Temp, _Arr[8], _Num[8];

void RecursiveFunction(int _X, int _Len) 
{
    if (_Len == _M) {
        for (int i = 0; i &lt; _M; i++)
            cout &lt;&lt; _Arr[i] &lt;&lt; " ";
        cout &lt;&lt; "\n";
        return;
    }

    for (int i = _X; i &lt; _N; i++) {
        _Arr[_Len] = _Num[i];
        RecursiveFunction(i, _Len + 1);
    }
    return;
}

int main()
{
    set&lt;int> _MySet;
    cin >> _N >> _M;
    for (int i = 0; i &lt; _N; i++) {
        cin >> _Temp;
        _MySet.insert(_Temp);
    }

    int _Cnt = 0;

    for (auto&amp; iter : _MySet) 
        _Num[_Cnt++] = iter;
    
    _N = _MySet.size();
    RecursiveFunction(0, 0);
    return 0;
}</pre>



<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/05/image-10.png" alt="" class="wp-image-34469" width="969" height="96" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-10.png 829w, https://lycos7560.com/wp-content/uploads/2023/05/image-10-300x30.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-10-768x76.png 768w" sizes="(max-width: 969px) 100vw, 969px" /></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-15666%eb%b2%88-n%ea%b3%bc-m-12-c-baekjoon/34467/">백준 15666번 (N과 M (12), 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-15666%eb%b2%88-n%ea%b3%bc-m-12-c-baekjoon/34467/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 15657번 (N과 M (8), C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15657%eb%b2%88-n%ea%b3%bc-m-8-c-baekjoon/34430/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15657%eb%b2%88-n%ea%b3%bc-m-8-c-baekjoon/34430/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 07 May 2023 14:26:07 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[15657]]></category>
		<category><![CDATA[15657번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N과 M (8)]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 15657]]></category>
		<category><![CDATA[백준 15657번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34430</guid>

					<description><![CDATA[<p>백준 15657번 'N과 M (8) ' 문제에 대한 글입니다. (This is an article on the issue of "N and M (8)" in Baekjoon Number 15657)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15657%eb%b2%88-n%ea%b3%bc-m-8-c-baekjoon/34430/">백준 15657번 (N과 M (8), 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-e51ad4a7      "
					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="#n과-m-8" class="uagb-toc-link__trigger">N과 M (8)</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">N과 M (8)</h1>



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



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



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



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



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



<p class="has-medium-font-size">N개의 자연수와 자연수 M이 주어졌을 때, </p>



<p class="has-medium-font-size">아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. </p>



<p class="has-medium-font-size">N개의 자연수는 모두 다른 수이다.</p>



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



<p>N개의 자연수 중에서 M개를 고른 수열</p>



<p>같은 수를 여러 번 골라도 된다.</p>



<p>고른 수열은 비내림차순이어야 한다.</p>



<p>  =&gt; 길이가 K인 수열 A가 A<sub>1</sub>&nbsp;≤ A<sub>2</sub>&nbsp;≤ &#8230; ≤ A<sub>K-1</sub>&nbsp;≤ A<sub>K</sub>를 만족하면, 비내림차순이라고 한다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>



<p class="has-medium-font-size">둘째 줄에 N개의 수가 주어진다. </p>



<p class="has-medium-font-size">입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</p>



<div style="height:62px" 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">수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p>



<div style="height:62px" 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="">3 1
4 5 2</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
4
5</pre>



<div style="height:100px" 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="">4 2
9 8 7 1</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 1
1 7
1 8
1 9
7 7
7 8
7 9
8 8
8 9
9 9</pre>



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



<h2 class="wp-block-heading">예제 입력 3</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="">4 4
1231 1232 1233 1234</pre>



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



<h2 class="wp-block-heading">예제 출력 3</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="">1231 1231 1231 1231
1231 1231 1231 1232
1231 1231 1231 1233
1231 1231 1231 1234
1231 1231 1232 1232
1231 1231 1232 1233
1231 1231 1232 1234
1231 1231 1233 1233
1231 1231 1233 1234
1231 1231 1234 1234
1231 1232 1232 1232
1231 1232 1232 1233
1231 1232 1232 1234
1231 1232 1233 1233
1231 1232 1233 1234
1231 1232 1234 1234
1231 1233 1233 1233
1231 1233 1233 1234
1231 1233 1234 1234
1231 1234 1234 1234
1232 1232 1232 1232
1232 1232 1232 1233
1232 1232 1232 1234
1232 1232 1233 1233
1232 1232 1233 1234
1232 1232 1234 1234
1232 1233 1233 1233
1232 1233 1233 1234
1232 1233 1234 1234
1232 1234 1234 1234
1233 1233 1233 1233
1233 1233 1233 1234
1233 1233 1234 1234
1233 1234 1234 1234
1234 1234 1234 1234</pre>



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



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



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon">baekjoon</a></li>
</ul>



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



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



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



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



<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;algorithm>
using namespace std;
int _N, _M, _Arr[8], _Num[8];
void RecursiveFunction(int _X, int _Len) {
    if (_Len == _M) {
        for (int i = 0; i &lt; _M; i++) 
            cout &lt;&lt; _Arr[i] &lt;&lt; " ";
        cout &lt;&lt; "\n";
        return;
    }

    for (int i = _X; i &lt; _N; i++) {
        _Arr[_Len] = _Num[i];
        RecursiveFunction(i, _Len + 1);
    }
    return;
}
int main() 
{
    cin >> _N >> _M;
    for (int i = 0; i &lt; _N; i++)
        cin >> _Num[i];
    sort(_Num, _Num + _N); // 정렬
    RecursiveFunction(0, 0);
    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/05/image-7.png" alt="" class="wp-image-34436" width="931" height="89" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-7.png 833w, https://lycos7560.com/wp-content/uploads/2023/05/image-7-300x29.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-7-768x74.png 768w" sizes="(max-width: 931px) 100vw, 931px" /></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-15657%eb%b2%88-n%ea%b3%bc-m-8-c-baekjoon/34430/">백준 15657번 (N과 M (8), 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-15657%eb%b2%88-n%ea%b3%bc-m-8-c-baekjoon/34430/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 15650번 (N과 M (2), C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15650%eb%b2%88-n%ea%b3%bc-m-2-c-baekjoon/34440/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15650%eb%b2%88-n%ea%b3%bc-m-2-c-baekjoon/34440/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 02 May 2023 14:43:23 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[15650]]></category>
		<category><![CDATA[15650번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N과 M (2)]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 15650]]></category>
		<category><![CDATA[백준 15650번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34440</guid>

					<description><![CDATA[<p>백준 15650번 'N과 M (2)' 문제에 대한 글입니다. (This is an article on the question "N and M (2)" of Baekjoon Number 15650.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15650%eb%b2%88-n%ea%b3%bc-m-2-c-baekjoon/34440/">백준 15650번 (N과 M (2), C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



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


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-a571c40a      "
					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="#n과-m-2" class="uagb-toc-link__trigger">N과 M (2)</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">N과 M (2)</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/15650">https://www.acmicpc.net/problem/15650</a></p>



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



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



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



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



<p class="has-medium-font-size">자연수 N과 M이 주어졌을 때, </p>



<p class="has-medium-font-size">아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>



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



<p>1부터 N까지&nbsp;자연수 중에서 중복 없이 M개를 고른 수열</p>



<p>고른 수열은 오름차순이어야 한다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 자연수 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>



<div style="height:62px" 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">각 수열은 공백으로 구분해서 출력해야 한다.</p>



<p class="has-medium-font-size">수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p>



<div style="height:62px" 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="">3 1</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="">1
2
3</pre>



<div style="height:50px" 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="">4 2</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 2
1 3
1 4
2 3
2 4
3 4</pre>



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



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



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



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



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



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



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



<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, _Arr[8], _Num[8];

void RecursiveFunction(int _X, int _Len) {
    if (_Len == _M) {
        for (int i = 0; i &lt; _M; i++) 
            cout &lt;&lt; _Arr[i] &lt;&lt; " ";
        cout &lt;&lt; "\n";
        return;
    }

    for (int i = _X; i &lt; _N; i++) {
        _Arr[_Len] = _Num[i];
        RecursiveFunction(i + 1, _Len + 1);
    }
    return;
}

int main() 
{
    cin >> _N >> _M;
    for (int i = 0; i &lt; _N; i++)
        _Num[i] = i + 1;
    RecursiveFunction(0, 0);
    return 0;
}</pre>



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



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/05/image-8.png" alt="" class="wp-image-34444" width="912" height="85" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-8.png 834w, https://lycos7560.com/wp-content/uploads/2023/05/image-8-300x28.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-8-768x72.png 768w" sizes="(max-width: 912px) 100vw, 912px" /></figure>



<div style="height:62px" 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-15650%eb%b2%88-n%ea%b3%bc-m-2-c-baekjoon/34440/">백준 15650번 (N과 M (2), C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15650%eb%b2%88-n%ea%b3%bc-m-2-c-baekjoon/34440/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 15654번 (N과 M (5), C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15654%eb%b2%88-n%ea%b3%bc-m-5-c-baekjoon/34460/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15654%eb%b2%88-n%ea%b3%bc-m-5-c-baekjoon/34460/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 16:42:24 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[15654]]></category>
		<category><![CDATA[15654번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N과 M (5)]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 15654]]></category>
		<category><![CDATA[백준 15654번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34460</guid>

					<description><![CDATA[<p>백준 15654번 'N과 M (5)' 문제에 대한 글입니다. (This is an article on the question "N and M (5)" of Baekjoon Number 15654)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15654%eb%b2%88-n%ea%b3%bc-m-5-c-baekjoon/34460/">백준 15654번 (N과 M (5), 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-194d3ee3      "
					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="#n과-m-5" class="uagb-toc-link__trigger">N과 M (5)</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">N과 M (5)</h1>



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



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



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



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



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



<p>N개의 자연수와 자연수 M이 주어졌을 때, </p>



<p>아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. </p>



<p>N개의 자연수는 모두 다른 수이다.</p>



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



<p>N개의 자연수 중에서 M개를 고른 수열</p>



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



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



<p>첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>



<p>둘째 줄에 N개의 수가 주어진다. </p>



<p>입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</p>



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



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



<p>한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. </p>



<p>중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.</p>



<p>수열은 사전 순으로 증가하는 순서로 출력해야 한다.</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="">3 1
4 5 2</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
4
5</pre>



<div style="height:100px" 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="">4 2
9 8 7 1</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 7
1 8
1 9
7 1
7 8
7 9
8 1
8 7
8 9
9 1
9 7
9 8</pre>



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



<h2 class="wp-block-heading">예제 입력 3</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="">4 4
1231 1232 1233 1234</pre>



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



<h2 class="wp-block-heading">예제 출력 3</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="">1231 1232 1233 1234
1231 1232 1234 1233
1231 1233 1232 1234
1231 1233 1234 1232
1231 1234 1232 1233
1231 1234 1233 1232
1232 1231 1233 1234
1232 1231 1234 1233
1232 1233 1231 1234
1232 1233 1234 1231
1232 1234 1231 1233
1232 1234 1233 1231
1233 1231 1232 1234
1233 1231 1234 1232
1233 1232 1231 1234
1233 1232 1234 1231
1233 1234 1231 1232
1233 1234 1232 1231
1234 1231 1232 1233
1234 1231 1233 1232
1234 1232 1231 1233
1234 1232 1233 1231
1234 1233 1231 1232
1234 1233 1232 1231</pre>



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



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



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



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



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



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



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



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

using namespace std;

int _N, _M, _Arr[8], _Num[8];
bool _NumBool[8];
void RecursiveFunction(int _X, int _Len) {
    if (_Len == _M) {
        for (int i = 0; i &lt; _M; i++)
            cout &lt;&lt; _Arr[i] &lt;&lt; " ";
        cout &lt;&lt; "\n";
        return;
    }

    for (int i = 0; i &lt; _N; i++) {
        if (_NumBool[i]) continue;
        _Arr[_Len] = _Num[i];
        _NumBool[i] = true;
        RecursiveFunction(i, _Len + 1);
        _NumBool[i] = false;
    }

    return;
}

int main()
{
    cin >> _N >> _M;
    for (int i = 0; i &lt; _N; i++)
        cin >> _Num[i];
    sort(_Num, _Num + _N); // 정렬
    RecursiveFunction(0, 0);
    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="828" height="81" src="https://lycos7560.com/wp-content/uploads/2023/05/image-9.png" alt="" class="wp-image-34462" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-9.png 828w, https://lycos7560.com/wp-content/uploads/2023/05/image-9-300x29.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-9-768x75.png 768w" sizes="(max-width: 828px) 100vw, 828px" /></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-15654%eb%b2%88-n%ea%b3%bc-m-5-c-baekjoon/34460/">백준 15654번 (N과 M (5), 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-15654%eb%b2%88-n%ea%b3%bc-m-5-c-baekjoon/34460/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 15663번 (N과 M (9), C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15663%eb%b2%88-n%ea%b3%bc-m-9-c-baekjoon/34472/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15663%eb%b2%88-n%ea%b3%bc-m-9-c-baekjoon/34472/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 16 Apr 2023 15:48:19 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[15663]]></category>
		<category><![CDATA[15663번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N과 M (9)]]></category>
		<category><![CDATA[SET]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 15663]]></category>
		<category><![CDATA[백준 15663번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34472</guid>

					<description><![CDATA[<p>백준 15663번 'N과 M (9)' 문제에 대한 글입니다. (This is an article on the question of BAEKJOON Number 15663 'N and M (9)')</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15663%eb%b2%88-n%ea%b3%bc-m-9-c-baekjoon/34472/">백준 15663번 (N과 M (9), 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-b0f74a2d      "
					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="#n과-m-9" class="uagb-toc-link__trigger">N과 M (9)</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">N과 M (9)</h1>



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



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



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">29219</td><td class="has-text-align-left" data-align="left">14418</td><td class="has-text-align-left" data-align="left">10819</td><td class="has-text-align-left" data-align="left">49.001%</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">N개의 자연수와 자연수 M이 주어졌을 때, </p>



<p class="has-medium-font-size">아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>



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



<p>N개의 자연수 중에서 M개를 고른 수열</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 ≤ M ≤ N ≤ 8)</p>



<p class="has-medium-font-size">둘째 줄에 N개의 수가 주어진다. </p>



<p class="has-medium-font-size">입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</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>



<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">예제 입력 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="">3 1
4 4 2</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
4</pre>



<div style="height:50px" 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="">4 2
9 7 9 1</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 7
1 9
7 1
7 9
9 1
9 7
9 9</pre>



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



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



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



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



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



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



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



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/kid7ho" target="_blank" rel="noreferrer noopener">kid7ho</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/5">백트</a><a href="https://www.acmicpc.net/problem/tag/5" target="_blank" rel="noreferrer noopener">래</a><a href="https://www.acmicpc.net/problem/tag/5">킹</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)"/>



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



<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/05/KakaoTalk_20230509_012310585.jpg" alt="" class="wp-image-34479" width="1358" height="1409" srcset="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_012310585.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_012310585-289x300.jpg 289w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_012310585-768x797.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_012310585-1480x1536.jpg 1480w" sizes="(max-width: 1358px) 100vw, 1358px" /></figure>



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

using namespace std;

int _N, _M, _Arr[8], _Num[8];
bool _NumBool[8]; // 중복을 확인

void RecursiveFunction(int _Len) 
{
    if (_Len == _M) {
        for (int i = 0; i &lt; _M; i++)
            cout &lt;&lt; _Arr[i] &lt;&lt; " ";
        cout &lt;&lt; "\n";
        return;
    }

    int _Temp = 0;
    for (int i = 0; i &lt; _N; i++) {
        if (!_NumBool[i] &amp;&amp; _Num[i] != _Temp) {
            _Arr[_Len] = _Num[i];
            _Temp = _Arr[_Len];
            _NumBool[i] = true;
            RecursiveFunction(_Len + 1);
            _NumBool[i] = false;    
        }
    }
    return;
}

int main()
{
    cin >> _N >> _M;
    for (int i = 0; i &lt; _N; i++) 
        cin >> _Num[i];
    sort(_Num, _Num + _N);
    RecursiveFunction(0);
    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/05/image-11.png" alt="" class="wp-image-34474" width="943" height="173" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-11.png 828w, https://lycos7560.com/wp-content/uploads/2023/05/image-11-300x55.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-11-768x141.png 768w" sizes="(max-width: 943px) 100vw, 943px" /></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-15663%eb%b2%88-n%ea%b3%bc-m-9-c-baekjoon/34472/">백준 15663번 (N과 M (9), 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-15663%eb%b2%88-n%ea%b3%bc-m-9-c-baekjoon/34472/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 15652번 (N과 M (4), C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15652%eb%b2%88-n%ea%b3%bc-m-4-c-baekjoon/34449/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15652%eb%b2%88-n%ea%b3%bc-m-4-c-baekjoon/34449/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 14 Apr 2023 15:00:35 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[15652]]></category>
		<category><![CDATA[15652번]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[N과 M (4)]]></category>
		<category><![CDATA[N과 M (8)]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 15652]]></category>
		<category><![CDATA[백준 15652번]]></category>
		<category><![CDATA[백트레킹]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34449</guid>

					<description><![CDATA[<p>백준 15652번 'N과 M (4)' 문제에 대한 글입니다. (This is an article on the question "N and M (4)" of Baekjoon Number 15652)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-15652%eb%b2%88-n%ea%b3%bc-m-4-c-baekjoon/34449/">백준 15652번 (N과 M (4), 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-114a9fef      "
					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="#n과-m-4" class="uagb-toc-link__trigger">N과 M (4)</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">N과 M (4)</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/15652" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/15652</a></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이 주어졌을 때, </p>



<p class="has-medium-font-size">아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>



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



<p>1부터 N까지&nbsp;자연수 중에서 M개를 고른 수열</p>



<p>같은 수를 여러 번 골라도 된다.</p>



<p>고른 수열은 비내림차순이어야 한다.</p>



<p> =&gt; 길이가 K인 수열 A가 A<sub>1</sub>&nbsp;≤ A<sub>2</sub>&nbsp;≤ &#8230; ≤ A<sub>K-1</sub>&nbsp;≤ A<sub>K</sub>를 만족하면, 비내림차순이라고 한다.</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 ≤ M ≤ N ≤ 8)</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>



<p class="has-medium-font-size">중복되는 수열을 여러 번 출력하면 안되며, </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">예제 입력 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="">3 1</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="">1
2
3</pre>



<div style="height:50px" 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="">4 2</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 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4</pre>



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



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



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



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



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



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



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



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



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



<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, _Arr[8], _Num[8];

void RecursiveFunction(int _X, int _Len) {
    if (_Len == _M) {
        for (int i = 0; i &lt; _M; i++) 
            cout &lt;&lt; _Arr[i] &lt;&lt; " ";
        cout &lt;&lt; "\n";
        return;
    }

    for (int i = _X; i &lt; _N; i++) {
        _Arr[_Len] = _Num[i];
        RecursiveFunction(i, _Len + 1);
    }
    return;
}

int main() 
{
    cin >> _N >> _M;
    for (int i = 0; i &lt; _N; i++)
        _Num[i] = i + 1;
    RecursiveFunction(0, 0);
    return 0;
}</pre>



<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-15652%eb%b2%88-n%ea%b3%bc-m-4-c-baekjoon/34449/">백준 15652번 (N과 M (4), 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-15652%eb%b2%88-n%ea%b3%bc-m-4-c-baekjoon/34449/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2661번 (좋은수열, C++, Backtracking) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/</link>
					<comments>https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 28 Oct 2022 11:55:17 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2661]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2661]]></category>
		<category><![CDATA[백준 2661번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[좋은수열]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.synology.me/?p=532</guid>

					<description><![CDATA[<p>좋은수열 https://www.acmicpc.net/problem/2661 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 128 MB 11169 5455 4188 50.144% 문제 숫자 1, 2, 3으로만 이루어지는 수열이 있다. 임의의 길이의 인접한 두 개의 부분 수열이 동일한 것이 있으면, 그 수열을 나쁜 수열이라고 부른다. 그렇지 않은 수열은 좋은 수열이다. 다음은 나쁜 수열의 예이다. 다음은 좋은 수열의 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/">백준 2661번 (좋은수열, C++, Backtracking) [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>



<h1 class="wp-block-heading"><strong>좋은수열</strong></h1>



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



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



<figure id="problem-info" class="wp-block-table is-style-stripes"><table class="has-ast-global-color-8-color has-ast-global-color-6-background-color has-text-color has-background"><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">11169</td><td class="has-text-align-left" data-align="left">5455</td><td class="has-text-align-left" data-align="left">4188</td><td class="has-text-align-left" data-align="left">50.144%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size">숫자 1, 2, 3으로만 이루어지는 수열이 있다. <br>임의의 길이의 인접한 두 개의 부분 수열이 동일한 것이 있으면, 그 수열을 나쁜 수열이라고 부른다. <br>그렇지 않은 수열은 좋은 수열이다.</p>



<p class="has-medium-font-size">다음은 나쁜 수열의 예이다.</p>



<ul class="wp-block-list">
<li>33</li>



<li>32121323</li>



<li>123123213</li>
</ul>



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



<p class="has-medium-font-size">다음은 좋은 수열의 예이다.</p>



<ul class="wp-block-list">
<li>2</li>



<li>32</li>



<li>32123</li>



<li>1232123</li>
</ul>



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



<p class="has-medium-font-size">길이가 N인 좋은 수열 들을 N자리의 정수로 보아 그 중 가장 작은 수를 나타내는 <br>수열을 구하는 프로그램을 작성하라. <br><br>예를 들면, 1213121과 2123212는 모두 좋은 수열이지만 <br>그 중에서 작은 수를 나타내는 수열은 1213121이다.</p>



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



<p class="has-normal-font-size">입력은 숫자 N하나로 이루어진다. N은 1 이상 80 이하이다.</p>



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



<p class="has-normal-font-size">첫 번째 줄에 1, 2, 3으로만 이루어져 있는 길이가 N인 좋은 수열 들 중에서 가장 작은 수를 <br>나타내는 수열만 출력한다. <br>수열을 이루는 1, 2, 3 들 사이에는 빈칸을 두지 않는다.</p>



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



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



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



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



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



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



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



<h2 class="has-medium-font-size wp-block-heading">출처</h2>



<p class="has-normal-font-size"><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a> > <a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a> > <a href="https://www.acmicpc.net/category/81" target="_blank" rel="noreferrer noopener">KOI 1997</a> > <a href="https://www.acmicpc.net/category/detail/401" target="_blank" rel="noreferrer noopener">중등부</a> 1번</p>



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



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/5" target="_blank" rel="noreferrer noopener">백트래킹</a></li>
</ul>



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



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



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



<p>1차 시도 <br>BFS로 모든 경우를 다 만들어 놓고 시작하려니 N이 80 이 되는 순간 프로그램이 나락을 간다.</p>



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



<h1 class="wp-block-heading"><strong>실패코드</strong></h1>



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



<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;queue>
#include &lt;cmath> 

using namespace std;

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

int N;

void BFS(int N)
{
	myQueue.push({0, N});

	while (!myQueue.empty())
	{
		int x = myQueue.front().first;
		int n = myQueue.front().second;
		myQueue.pop();

		cout &lt;&lt; n &lt;&lt; " \n";
		if (n == 0)
		{
			//cout &lt;&lt; x &lt;&lt; " \n";
			continue;
		}
		for (int i = 1; i &lt;= 3; i++)
		{
			if (i == 1)
			{
				myQueue.push({ x + 1 * pow(10, n - 1), n-1 });
			}
			else if (i == 2)
			{
				myQueue.push({ x + 2 * pow(10, n - 1), n - 1 });
			}
			else if (i == 3)
			{
				myQueue.push({ x + 3 * pow(10, n - 1), n - 1 });
			}
		}
	}

}


int main()
{
	cin >> N;
	BFS(N);
	return 0;
}</pre>



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



<p class="has-medium-font-size">계산 중 깊이를 측정해보니 이건 노답 이라는 것을 깨달았다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="931" height="545" src="https://lycos7560.com/wp-content/uploads/image-72.png" alt="" class="wp-image-2293" srcset="https://lycos7560.com/wp-content/uploads/image-72.png 931w, https://lycos7560.com/wp-content/uploads/image-72-300x176.png 300w, https://lycos7560.com/wp-content/uploads/image-72-768x450.png 768w" sizes="(max-width: 931px) 100vw, 931px" /></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>



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



<p class="has-medium-font-size">DFS로 변경<br> 목표는 가장 낮은 숫자를 찾는 것 =&gt; 1부터 시작해서 탐색 </p>



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



<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;queue>
#include &lt;cmath> 

using namespace std;

int N;

void DFS(int n, int value)
{
	if (n == 0)
	{
		cout &lt;&lt; value &lt;&lt; "\n";
		return;
	}

	for (int i = 1; i &lt;= 3; i++)
	{
		if (i == 1)
		{
			DFS(n - 1, value + 1 * pow(10, n - 1));
		}
		else if (i == 2)
		{
			DFS(n - 1, value + 2 * pow(10, n - 1));
		}
		else if (i == 3)
		{
			DFS(n -1, value + 3 * pow(10, n - 1));
		}
	}
}

int main()
{
	cin >> N;
	DFS(N, 0);
	cout &lt;&lt; "end";
	return 0;
}</pre>



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



<p class="has-medium-font-size">N은 80까지 가능<br>80 자리…<br>string 으로 변환…. </p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="939" height="752" src="https://lycos7560.com/wp-content/uploads/image-73.png" alt="" class="wp-image-2294" srcset="https://lycos7560.com/wp-content/uploads/image-73.png 939w, https://lycos7560.com/wp-content/uploads/image-73-300x240.png 300w, https://lycos7560.com/wp-content/uploads/image-73-768x615.png 768w" sizes="(max-width: 939px) 100vw, 939px" /></figure>



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



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

using namespace std;

int N;

void DFS(int n, string value)
{
	if (n == 0)
	{
		cout &lt;&lt; value &lt;&lt; "\n";
		return;
	}

	
	for (int i = 1; i &lt;= 3; i++)
	{
		if (i == 1)
		{
			DFS(n - 1, value + '1');
		}
		else if (i == 2)
		{
			DFS(n - 1, value + '2');
		}
		else if (i == 3)
		{
			DFS(n - 1, value + '3');
		}
	}
}

int main()
{
	cin >> N;
	DFS(N, "");
	cout &lt;&lt; "end";
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="871" height="453" src="https://lycos7560.com/wp-content/uploads/image-74.png" alt="" class="wp-image-2295" srcset="https://lycos7560.com/wp-content/uploads/image-74.png 871w, https://lycos7560.com/wp-content/uploads/image-74-300x156.png 300w, https://lycos7560.com/wp-content/uploads/image-74-768x399.png 768w" sizes="(max-width: 871px) 100vw, 871px" /></figure>



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



<p class="has-medium-font-size">조건에 부합하는 가장 처음 값이 정답</p>



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



<h2 class="wp-block-heading"><strong> 성공 코드(DFS) </strong></h2>



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



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

using namespace std;

int N;
string number;

void DFS(char ch, int cnt)
{
	// 제일 먼저 조건에 부합하는 숫자가 답
	if (cnt - 1 == N) { 
		cout &lt;&lt; number; 
		exit(0);
	}
	
	number += ch;
	
	for (int i = 1; i &lt;= cnt/2; i++)
	{
		string a = number.substr(cnt - i, i);
		string b = number.substr(cnt - i * 2, i);

		if (a == b){ 
			// 나쁜 수열이면 지우고 리턴
			number.erase(cnt - 1);
			return;
		}
	}

	for (int i = 1; i &lt;= 3; i++)
	{
		DFS(i + '0', cnt + 1);
	}

	// cnt - 1 자리가 성립하지 않을 경우
	number.erase(cnt - 1);
}

int main()
{
	cin >> N;
	for (int i = 1; i &lt;= 3; i++)
	{
		// '0' 에서 i 만큼 더하면 i
		DFS(i + '0', 1);
	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="794" height="81" src="https://lycos7560.com/wp-content/uploads/image-75.png" alt="" class="wp-image-2296" srcset="https://lycos7560.com/wp-content/uploads/image-75.png 794w, https://lycos7560.com/wp-content/uploads/image-75-300x31.png 300w, https://lycos7560.com/wp-content/uploads/image-75-768x78.png 768w" sizes="(max-width: 794px) 100vw, 794px" /></figure>



<div style="height:89px" 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; 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>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/">백준 2661번 (좋은수열, C++, Backtracking) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
