<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>PROJECT REBAS</title>
    <link>https://rebas.tistory.com/</link>
    <description>여기저기에 호기심이 충만한 유사-프로그래머</description>
    <language>ko</language>
    <pubDate>Sun, 26 Jul 2026 16:21:14 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>레바스</managingEditor>
    <image>
      <title>PROJECT REBAS</title>
      <url>https://t1.daumcdn.net/cfile/tistory/99E97233598685B015</url>
      <link>https://rebas.tistory.com</link>
    </image>
    <item>
      <title>자료구조 &amp;middot; C++로 구현한 덱</title>
      <link>https://rebas.tistory.com/797</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9989C13E5C9F58C508&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9989C13E5C9F58C508&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;다운로드.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;b&gt;덱 (Deque)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;덱은 Double-Ended Queue의 약자이며, 양쪽에서 원소의 삽입과 삭제가 가능한 선형 자료구조이다.&lt;/p&gt;&lt;p&gt;큐와 스택을 합친 형태라고 생각하면 된다. 원형 큐(Circular Queue)와 비슷하게 구현하므로, &lt;a href=&quot;https://rebas.kr/795&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;이전 글&lt;/a&gt;을 참조.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;한쪽에 push 하고 같은 쪽에서 pop 하면, 스택처럼 사용할 수 있다.&lt;/li&gt;&lt;li&gt;한쪽에 push 하고&amp;nbsp;반대쪽에서 pop 하면, 큐처럼 사용할 수 있다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9952BB4C5C9F5DE41C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9952BB4C5C9F5DE41C&quot; width=&quot;860&quot; height=&quot;645&quot; filename=&quot;캡처5.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Deque C++ 구현 소스코드&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; MAX &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e5&lt;/span&gt;;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;Deque&lt;/span&gt; {
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;private:&lt;/span&gt;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; data[MAX];
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_front;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_back;
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;public:&lt;/span&gt;
    Deque();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;empty&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push_front&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push_back&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop_front&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop_back&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;front&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;back&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;size&lt;/span&gt;();
};

Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;Deque() {
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;empty() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; index_back;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;push_front(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    data[index_front] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1+&lt;/span&gt;MAX) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;push_back(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
    data[index_back] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;pop_front() {
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;pop_back() {
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1+&lt;/span&gt;MAX) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;front() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[(index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;back() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[index_back];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Deque&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;size() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;MAX)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; main() {
    Deque dq;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) dq.push_front(i);  &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push front 1~3&lt;/span&gt;
    dq.pop_front();     &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop front&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=6&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) dq.push_back(i);   &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push back 4~6&lt;/span&gt;
    dq.pop_back();      &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop back&lt;/span&gt;
    dq.push_front(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;7&lt;/span&gt;);   &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push front 7&lt;/span&gt;
    Deque dq2 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dq;     &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Copy deque&lt;/span&gt;
    cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;* Pop Back: &quot;&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;dq.empty()) {
        cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; dq.back() &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot; -&amp;gt; &quot;&lt;/span&gt;;
        dq.pop_back();  &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop back all&lt;/span&gt;
    }
    cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;* Pop Front: &quot;&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;dq2.empty()) {
        cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; dq2.front() &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot; -&amp;gt; &quot;&lt;/span&gt;;
        dq2.pop_front();  &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop front all&lt;/span&gt;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# main 함수 출력 결과&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;* Size: 5
* Pop Back: 5 -&amp;gt; 4 -&amp;gt; 1 -&amp;gt; 2 -&amp;gt; 7 -&amp;gt;
* Pop Front: 7 -&amp;gt; 2 -&amp;gt; 1 -&amp;gt; 4 -&amp;gt; 5 -&amp;gt;&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 배열로 구현한 덱이다. 예외 처리는 하지 않았다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Deque 클래스 설명&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; MAX &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e5&lt;/span&gt;;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;Deque&lt;/span&gt; {
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;private:&lt;/span&gt;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; data[MAX];
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_front;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_back;
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;public:&lt;/span&gt;
    Deque();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;empty&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push_front&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push_back&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop_front&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop_back&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;front&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;back&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;size&lt;/span&gt;();
};&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ MAX는 덱의 최대 사이즈이다. 값을 바꾸면 최대 사이즈를 조절할 수 있다.&lt;/p&gt;&lt;p&gt;✓ Data는 덱에 넣은 원소를 저장하는 배열이다.&lt;/p&gt;&lt;p&gt;✓ index_front는 앞에 넣은 원소의 위치를 가리키는 변수다.&lt;/p&gt;&lt;p&gt;✓ index_back은 뒤에 넣은 원소의 위치를 가리키는 변수다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/997E1B495C9F5FE82B&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F997E1B495C9F5FE82B&quot; width=&quot;860&quot; height=&quot;1376&quot; filename=&quot;캡쳐.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# 생성자와 empty 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;Deque&lt;/span&gt;() {
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;empty&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; index_back;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 생성자를 이용하여 Front 인덱스, Back 인덱스를 둘 다 0으로 초기화한다.&lt;/p&gt;&lt;p&gt;✓ empty 함수는 덱이 비어있는지 확인하는 함수이다. Front 인덱스와 Back 인덱스가 서로 같다면, 덱이 비어있는 상태이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# push_front, push_back 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;push_front&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Full check&lt;/span&gt;
    data[index_front] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1+&lt;/span&gt;MAX) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;push_back&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Full check&lt;/span&gt;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
    data[index_back] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 덱에 원소를 집어넣는다. push_front 함수는 덱의 앞에 원소를 넣으며, push_back 함수는 덱의 뒤에 원소를 넣는다.&lt;/p&gt;&lt;p&gt;✓ 원소를 넣기 전에, 덱이 꽉 찬 상태인지 확인하여 예외 처리를 해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# pop_front, pop_back 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;pop_front&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;pop_back&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1+&lt;/span&gt;MAX) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 덱에서 원소를 제거한다. pop_front 함수는 덱의 가장 앞에 있는 원소를 빼고, pop_back 함수는 덱의 가장 뒤에 있는 원소를 뺀다.&lt;/p&gt;&lt;p&gt;✓ 원소를 빼기 전에, 덱이 비어있는 상태인지 확인하여 예외 처리를 해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# front, back 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;front&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[(index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;back&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[index_back];
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ front 함수는 가장 앞에 넣은 원소가 무엇인지 보는 함수이다.&lt;/p&gt;&lt;p&gt;✓ back 함수는 가장 뒤에 넣은 원소가 무엇인지 보는 함수이다.&lt;/p&gt;&lt;p&gt;✓ 두 함수 모두, 덱이 비어있는 상태인지 확인하여 예외 처리를 해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# size 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;size&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;MAX)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 덱에 몇 개의 원소가 들어있는지 확인하는 함수이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/자료구조</category>
      <category>data structure</category>
      <category>자료구조</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/797</guid>
      <comments>https://rebas.tistory.com/797#entry797comment</comments>
      <pubDate>Tue, 30 Apr 2019 14:23:27 +0900</pubDate>
    </item>
    <item>
      <title>자료구조 &amp;middot; C++로 구현한 스택</title>
      <link>https://rebas.tistory.com/796</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99807D405C9F345610&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99807D405C9F345610&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;다운로드.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;스택 (Stack)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;스택은 후입선출(LIFO; Last in First out) 방식의 선형 자료구조이다.&lt;/p&gt;&lt;p&gt;후입선출이란, 나중에 들어간 것이 먼저 나온다는 뜻이다. 즉, 스택은 나중에 들어간 데이터를&amp;nbsp;먼저 처리하는 자료구조이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99DCBD3E5C9F36100B&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99DCBD3E5C9F36100B&quot; width=&quot;860&quot; height=&quot;491&quot; filename=&quot;캡처3.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Stack C&lt;/span&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;++ 구현 소스코드&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; MAX &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e5&lt;/span&gt;;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;Stack&lt;/span&gt; {
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;private:&lt;/span&gt;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; data[MAX];
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index;
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;public:&lt;/span&gt;
    Stack();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;empty&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;top&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;size&lt;/span&gt;();
};

Stack&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;Stack() {
    index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; Stack&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;empty() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Stack&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;push(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    data[index] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Stack&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;pop() {
    index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Stack&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;top() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[index];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Stack&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;size() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; main() {
    Stack s;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=10&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) s.push(i); &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push 1~10&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) s.pop(); &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop 10~7&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) s.push(i); &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push 1~4&lt;/span&gt;
    cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;* Size: &quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; s.size() &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'\n'&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;s.empty()) {
        cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;Pop: &quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; s.top() &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'\n'&lt;/span&gt;;
        s.pop();    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop all&lt;/span&gt;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# main 함수 출력 결과&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;* Size: 10
Pop: 4
Pop: 3
Pop: 2
Pop: 1
Pop: 6
Pop: 5
Pop: 4
Pop: 3
Pop: 2
Pop: 1&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 배열로 구현한 스택이다. 예외 처리는 하지 않았다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Stack 클래스 설명&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; MAX &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e5&lt;/span&gt;;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;Stack&lt;/span&gt; {
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;private:&lt;/span&gt;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; data[MAX];
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index;
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;public:&lt;/span&gt;
    Stack();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;empty&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;top&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;size&lt;/span&gt;();
};&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ MAX는 스택의 최대 사이즈이다. 값을 바꾸면 최대 사이즈를 조절할 수 있다.&lt;/p&gt;&lt;p&gt;✓ Data는 스택에 넣은 원소를 저장하는 배열이다.&lt;/p&gt;&lt;p&gt;✓ index는 가장 마지막에 넣은 원소의 위치를 가리키는 변수다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9928EC365C9F37E704&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9928EC365C9F37E704&quot; width=&quot;860&quot; height=&quot;480&quot; filename=&quot;캡처4.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# 생성자와 empty 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;Stack&lt;/span&gt;() {
    index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;empty&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 생성자를 이용하여 인덱스를 -1로 초기화한다.&lt;/p&gt;&lt;p&gt;✓ empty 함수는 스택이&amp;nbsp;비어있는지 확인하는 함수이다. 인덱스가 -1이면, 스택이 비어있는 상태이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# push 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;push&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Full check, if index == MAX-1&lt;/span&gt;
    index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    data[index] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 스택에 원소를 집어넣는다. 인덱스를 1 증가시키고, 그 위치에 원소를 저장한다.&lt;/p&gt;&lt;p&gt;✓ 원소를 넣기 전에, 스택이&amp;nbsp;꽉 찬 상태인지 확인하여 예외 처리를 해야 한다. index&amp;nbsp;== MAX-1 로 확인하면 된다. 스택&amp;nbsp;사이즈를 넉넉하게 두는 편이 좋다.&lt;/p&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# pop 함수&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;pop&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    index &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
}&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 스택에서 가장 나중에&amp;nbsp;넣은 원소를 뺀다. 인덱스를 1 감소시키면 된다.&lt;/p&gt;&lt;p&gt;✓ 원소를 빼기 전에, 스택이&amp;nbsp;비어있는 상태인지 확인하여 예외 처리를 해야 한다. 위에서 구현한 empty() 함수를 이용하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# top 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;top&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[index];
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ top&amp;nbsp;함수는 가장 나중에&amp;nbsp;넣은 원소가 무엇인지 보는 함수이다.&lt;/p&gt;&lt;p&gt;✓ 스택이&amp;nbsp;비어있는 상태인지 확인하여 예외 처리를 해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# size 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;size&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 스택에 몇 개의 원소가 들어있는지 확인하는 함수이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/자료구조</category>
      <category>data structure</category>
      <category>자료구조</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/796</guid>
      <comments>https://rebas.tistory.com/796#entry796comment</comments>
      <pubDate>Tue, 30 Apr 2019 14:23:10 +0900</pubDate>
    </item>
    <item>
      <title>자료구조 &amp;middot; C++로 구현한 큐</title>
      <link>https://rebas.tistory.com/795</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99B6BA395C9F1A212A&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99B6BA395C9F1A212A&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;다운로드.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;큐 (Queue)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;큐는 선입선출(FIFO; First in First out) 방식의 선형 자료구조이다.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;선입선출이란, 먼저 들어간 것이 먼저&amp;nbsp;나온다는 뜻이다. 즉, 큐는 먼저 들어간 데이터를 먼저 처리하는 자료구조이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99B9033C5C9F1B2B28&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99B9033C5C9F1B2B28&quot; width=&quot;860&quot; height=&quot;632&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Queue C++ 구현 소스코드&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; MAX &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e5&lt;/span&gt;;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;Queue&lt;/span&gt; {
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;private:&lt;/span&gt;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; data[MAX];
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_front;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_back;
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;public:&lt;/span&gt;
    Queue();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;empty&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;front&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;back&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;size&lt;/span&gt;();
};

Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;Queue() {
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;empty() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; index_back;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;push(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
    data[index_back] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;pop() {
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;front() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[(index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;back() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[index_back];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; Queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;::&lt;/span&gt;size() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;MAX)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; main() {
    Queue q;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=10&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) q.push(i); &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push 1~10&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) q.pop(); &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop 1~4&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) q.push(i); &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Push 1~4&lt;/span&gt;
    cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;* Size: &quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; q.size() &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'\n'&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        cout &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;Pop: &quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; q.front() &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'\n'&lt;/span&gt;;
        q.pop();    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Pop all&lt;/span&gt;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# main 함수 출력 결과&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;* Size: 10
Pop: 5
Pop: 6
Pop: 7
Pop: 8
Pop: 9
Pop: 10
Pop: 1
Pop: 2
Pop: 3
Pop: 4&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 배열로 구현한 원형 큐(Circular Queue)이다. 선형 큐(Linear Queue)보다 메모리 공간 활용이 더 효율적이다.&lt;/p&gt;&lt;p&gt;✓ 모듈러 연산을 통해 큐 내부를 순환하면 된다.&lt;/p&gt;&lt;p&gt;✓ 예외 처리는 하지 않았다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Queue 클래스 설명&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; MAX &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e5&lt;/span&gt;;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;Queue&lt;/span&gt; {
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;private:&lt;/span&gt;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; data[MAX];
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_front;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; index_back;
&lt;span style=&quot;color: rgb(160, 160, 0);&quot;&gt;public:&lt;/span&gt;
    Queue();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;empty&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;push&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x);
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;pop&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;front&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;back&lt;/span&gt;();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;size&lt;/span&gt;();
};&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ MAX는 큐의 최대 사이즈이다. 값을 바꾸면 최대 사이즈를 조절할 수 있다.&lt;/p&gt;&lt;p&gt;✓ Data는&amp;nbsp;큐에 넣은 원소를 저장하는 배열이다.&lt;/p&gt;&lt;p&gt;✓ index_front는 가장 먼저&amp;nbsp;넣은 원소의 위치를 가리키는 변수다.&lt;/p&gt;&lt;p&gt;✓ index_back은 가장 나중에 넣은 원소의 위치를 가리키는 변수다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9910AA4F5C9F215D0E&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9910AA4F5C9F215D0E&quot; width=&quot;860&quot; height=&quot;639&quot; filename=&quot;캡처2.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# 생성자와 empty 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;Queue&lt;/span&gt;()&lt;/span&gt; {
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;empty&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; index_back;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 생성자를 이용하여 Front 인덱스, Back 인덱스를 둘 다 0으로 초기화한다.&lt;/p&gt;&lt;p&gt;✓ empty 함수는 큐가 비어있는지 확인하는 함수이다. Front 인덱스와 Back 인덱스가 서로 같다면, 큐가 비어있는 상태이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# push 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;push&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x) {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Full check, if (index_front == (index_back+1)%MAX)&lt;/span&gt;
    index_back &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
    data[index_back] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 큐에 원소를 집어넣는다. Back 인덱스를 1 증가시키고, 그 위치에 원소를 저장한다.&lt;/p&gt;&lt;p&gt;✓ 원소를 넣기 전에, 큐가 꽉 찬 상태인지 확인하여&amp;nbsp;예외 처리를 해야 한다.&amp;nbsp;index_front == (index_back+1) % MAX 로 확인하면 된다. 큐 사이즈를 넉넉하게 두는 편이 좋다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# pop 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;pop&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    index_front &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; MAX;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 큐에서 가장 먼저 넣은 원소를 뺀다. Front 인덱스를 1증가시키면 된다.&lt;/p&gt;&lt;p&gt;✓ 원소를 빼기 전에, 큐가 비어있는 상태인지 확인하여&amp;nbsp;예외 처리를 해야 한다. 위에서 구현한 empty() 함수를 이용하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# front, back 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;front&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[(index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;back&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Empty check&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; data[index_back];
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ front 함수는 가장 먼저 넣은 원소가&amp;nbsp;무엇인지 보는 함수이다.&lt;/p&gt;&lt;p&gt;✓ back 함수는 가장 나중에 넣은&amp;nbsp;원소가 무엇인지 보는 함수이다.&lt;/p&gt;&lt;p&gt;✓ 두 함수 모두, 큐가 비어있는 상태인지 확인하여 예외 처리를 해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;# size 함수&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;size&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; (index_back&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;index_front&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;MAX)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;MAX;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 큐에 몇 개의 원소가 들어있는지 확인하는 함수이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/자료구조</category>
      <category>data structure</category>
      <category>자료구조</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/795</guid>
      <comments>https://rebas.tistory.com/795#entry795comment</comments>
      <pubDate>Tue, 30 Apr 2019 14:22:43 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 교착상태 (데드락)</title>
      <link>https://rebas.tistory.com/859</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9929A0505CC3EC6606&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9929A0505CC3EC6606&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;교착&lt;/span&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;상태 (Deadlock)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;운영체제(또는 소프트웨어)에서 자원을 사용할 때, 두 개 이상의 작업이 순차적으로 상대방의 작업이 끝나기를 기다리면서, 아무것도 진행되지 않는 상태를 말한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 400px; width: 400px; height: 373px;; height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9916AE4B5CC459DB31&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9916AE4B5CC459DB31&quot; width=&quot;400&quot; height=&quot;373&quot; filename=&quot;os7.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;width: 400px; height: 373px;&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; width: 400px; height: 373px;;&quot;&gt;[그림 1] 교통을 통해 표현한 교착상태&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;교착상태 발생 조건&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 교착상태는 다음 네 가지 조건이 모두 성립될 때 발생한다.&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;상호 배제 (Mutual exclusion)&lt;/b&gt; : 한 번에 하나의 프로세스만 공유 자원을 사용할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;점유 대기 (Hold and wait) &lt;/b&gt;: 각 프로세스가 하나의 자원을 점유하고 있으면서, 다른&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;프로세스가 사용하&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;고 있는 자원을 추가로 점유하기 위해 대기한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;비선점 (No preemption)&lt;/b&gt; : 각 프로세스는&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;다른 프로세스가 사용하고 있는 자원을 강제로 빼앗을 수 없다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;순환 대기 (Circular wait)&lt;/b&gt; : 각 프로세스는 순환적으로(원형 형태)&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;다음 프로세스가 요구하는 자원을 가지고 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;교착상태 해결 방법&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;예방 기법 (Deadlock Prevention)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;교착상태가 발생되지 않도록 사전에 예방하는 방법이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;교착상태 발생&amp;nbsp;조건 네 가지 중&amp;nbsp;적어도 하나가 성립하지 않게 만드는 방법이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;순환 대기를 막는 방법이 네 가지 중 가장 효율적이다. 자원에 순서를 부여하고,&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;각 프로세스가 오름차순으로 자원을 요청하도록 구현한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;예방 기법은 일반적으로 자원 사용 효율성이 떨어지고 비용이 많이 드는 문제점이 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;회피 기법 (Deadlock &lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;Avoidance)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;교착상태가 발생하는 조건인지 확인하면서&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;적절히 회피하는&amp;nbsp;방법이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;Circular wait가 발생하지 않도록 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;자원 할당 상태를 검사하면서, 시스템이 안전 상태(Safe state)인지 확인한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ol style=&quot;list-style-type: decimal;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;자원 할당 그래프 알고리즘 (Resource-Allocation Graph Algorithm) : 자원 할당에 대한 그래프를 구성한 후, 그래프에 사이클이 없을 때에만 자원을 할당한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;은행원 알고리즘 (Banker's Algorithm) &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;: 자원 할당 그래프 알고리즘은 프로세스가 하나의 자원만&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;사용하는 시스템에서 사용된다. 이에 반해 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;은행원 알고리즘은 프로세스가 자원을&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;여러 개 사용하는&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;경우에도 사용할 수 있는 알고리즘이다.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;탐지&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;와 회복&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;기법 (Deadlock Detection &amp;amp; Recovery)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;시스템에 교착 상태가 발생했는지 확인하기 위해 시스템의 상태를 검사한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;탐지 후, 교착 상태를 일으킨 프로세스를 종료하거나, 교착&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;상태의 프로세스에 할당된 자원을 선점하여 교착상태를&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;제거&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;무시 기법&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;(Deadlock Ignorance)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;교착상태 자체를 무시하고,&amp;nbsp;특별한&amp;nbsp;조치를 취하지 않는다. 교착상태의 발생 확률이 낮은 상황에서 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;효율적이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;만약 교착상태가 발생한다면, 프로세스를 종료하거나 자원을 선점하여&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;회복한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;UNIX, Windows 등 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;대부분의 운영체제가 이 방법을 사용한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/859</guid>
      <comments>https://rebas.tistory.com/859#entry859comment</comments>
      <pubDate>Mon, 29 Apr 2019 18:51:04 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 스핀락</title>
      <link>https://rebas.tistory.com/858</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/991457385CC3E2722C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F991457385CC3E2722C&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;스핀락 (Spinlock)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 임계 구역에 진입이 불가능&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;할 때, 진입 가능할 때까지 루프를 돌면서 진입을 재시도하는 방식으로 구현된 락&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 임계 구역에 진입하기 위해서는 락(Lock)이 필요하기 때문에, 락을 획득할 때까지 해당 프로세스(스레드)가 돌고 있다(Spin)는 것을 의미한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 스핀락은 바쁜 대기(Busy waiting)의 한 종류이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 스핀락은 운영체제의 스케줄링을 지원받지 않기 때문에, 해당 프로세스(스레드)에 대한 Context switch가 일어나지 않는다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;장점&lt;/b&gt; : 임계 구역 진입을 짧은 시간 내에 할 수 있다면, Context switch를 하지 않아도 되므로&amp;nbsp;효율적이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;단점&lt;/b&gt; : 오랜 시간 동안 스핀락을 진행하고 있다면, 스레드의 대기 시간이 길어지므로&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;비효율적이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;바쁜 대기 (Busy waiting)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 어떤 특정 공유 자원에 대하여 두 개 이상의 프로세스(스레드)가 접근 권한을 얻고자 하는 동기화 상황일 때, 그 권한 획득을 위한 과정에서 일어나는 현상&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ FCFS 등의 우선순위 스케줄링 기반의 싱글 프로세서 시스템에서는 비효율적이다. 멀티 프로세서 환경에서 사용하는 것이 좋다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/858</guid>
      <comments>https://rebas.tistory.com/858#entry858comment</comments>
      <pubDate>Mon, 29 Apr 2019 18:50:54 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 세마포어, 뮤텍스</title>
      <link>https://rebas.tistory.com/857</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/996017375CC3A4312A&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F996017375CC3A4312A&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;b&gt;세마포어와 뮤텍스의 차이점&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;세마포어&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;: Signaling mechanism / &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;일종의 카운터로서 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;크리티컬 섹션에 프로세스(스레드)가 동시에 N개 접근할 수 있다. 카운터 값이 0과 1로만 제한되면 Binary s&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;emaphore이며, 그 이상의 값을 가지면 Counting semaphore이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;뮤텍스&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : Locking mechanism /&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;락(Lock)을 가진 하나의 프로세스(스레드)만 크리티컬 섹션에 접근할 수 있다. Binary semaphore로 구현될 수 있다.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;b&gt;세마포어 (Semaphore)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 각 프로세스에 제어 신호를 전달하여 순서대로 작업을 수행하도록 하는 동기화 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;기법이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 세마포어 S는 P와 V 연산으로만 접근 가능한 카운터 변수이며, 0 이상의 값을 가질 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ S의 값이 0과 1로만 제한되면 이진 세마포어(Binary semaphore)이며, 그 이상의 값을 가지면 카운팅 세마포어(Counting semaphore)이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ Binary semaphore를 통해 Mutex를 구현할 수 있지만, Mutex로 Semaphore를 구현할 수는 없다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;P 연산&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : Wait 동작 /&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;자원을 사용하려는 프로세스들의 진입 여부를 자원 카운트(S)를 통해 결정한다. 카운트를 감소시켜서(S--&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;) 자원 점유를 알린다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;V 연산&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : Signal 동작 / 대기 중인 프로세스를 깨우는 신호이다. 카운트를 증가시켜서(S++&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;) 자원 반납을 알린다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Semaphore structure&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;typedef&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; value;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; process &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;list;
} Semaphore;
&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// P&lt;/span&gt;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;Wait&lt;/span&gt;(Semaphore &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;S) {
    S&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (S&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&amp;gt;&lt;/span&gt;value &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Push the process into S-&amp;gt;list. */&lt;/span&gt;
        Block();
    }
}
&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// V&lt;/span&gt;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;Signal&lt;/span&gt;(Semaphore &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;S) {
    S&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (S&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&amp;gt;&lt;/span&gt;value &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Pop process P from S-&amp;gt;list. */&lt;/span&gt;
        WakeUp(P);
    }
}

&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Semaphore implementation&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    Wait(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;S);
    &lt;span style=&quot;color: rgb(241, 95, 95); font-style: italic;&quot;&gt;/* Critical Section */&lt;/span&gt;
    Signal(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;S);
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Remainder Section */&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;뮤텍스 (Mutex)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 특정 프로세스(스레드)가 공유 자원을 사용하고 있는 상황이라면, 다른 프로세스가 공유 자원을 사용하지 못하도록 하는 동기화 기법이다.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 공유 자원 점유한 프로세스는 락(Lock)을 지니며, 자원 사용을 마치면 락을 반납한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// P and V&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;wait&lt;/span&gt;(S) {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (S &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;);
    S&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;;
}
&lt;span style=&quot;color: rgb(0, 85, 255);&quot;&gt;signal&lt;/span&gt;(S) {
    S&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Mutex implementation in binary semaphore&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    wait(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;mutex);
    &lt;span style=&quot;color: rgb(241, 95, 95); font-style: italic;&quot;&gt;/* Critical Section */&lt;/span&gt;
    signal(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;mutex);
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Remainder Section */&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/857</guid>
      <comments>https://rebas.tistory.com/857#entry857comment</comments>
      <pubDate>Mon, 29 Apr 2019 18:50:27 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 프로세스 동기화</title>
      <link>https://rebas.tistory.com/856</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9966604B5CC15D6A1F&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9966604B5CC15D6A1F&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스 동기화 (Process Synchronization)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 협력하는 프로세스 사이에서 실행 순서 규칙을 정하여 공유 자원의 일관성을 보장하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 프로세스가 서로 협력하며 공유 자원을&amp;nbsp;사용하는 상황에서, 경쟁 조건이 발생하면 공유 자원을 신뢰할 수 없게 만들 수 있다.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;이를 방지하기 위해 프로세스들이 공유 자원을&amp;nbsp;사용할 때 특별한 규칙을 만드는 것이 프로세스 동기화이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;경쟁 조건 (Race Condition)&lt;/b&gt; : 여러 프로세스(또는 스레드)가 공유 자원에&amp;nbsp;동시에 접근할 때, 공유 자원에 대한 접근 순서에 따라 실행 결과가 달라질 수 있는 상황&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;임계 구역 (Critical Section)&lt;/b&gt; : 여러 프로세스(또는 스레드)가 자원을&amp;nbsp;공유하는 상황에서, 하나의 프로세스(스레드)만 접근할 수 있도록 제한해둔 코드 영역&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Entry Section&lt;/span&gt;
    &lt;span style=&quot;color: rgb(241, 95, 95); font-style: italic;&quot;&gt;// Critical Section&lt;/span&gt;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Exit Section&lt;/span&gt;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Remainder Section&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;동기화와 관련한 고전적인&amp;nbsp;문제&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;은행 계좌 문제 (Back Account Problem)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;부모는 은행 계좌에 입금을 한다. 자식은 은행 계좌에서 출금한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;입금과 출금 과정이 별도로 이루어져야 한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;크리티컬 섹션&lt;/b&gt; : 은행 계좌&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;독자 저자 문제 (Readers Writers Problem)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;독자는 책(공유 데이터베이스)에 쓰여있는&amp;nbsp;글을&amp;nbsp;읽는다. 저자는 책에 글을 써서&amp;nbsp;추가한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;독자가 글을 읽고 있다면, 독자는 추가적으로 글을&amp;nbsp;읽을 수 있지만, 저자는 글을 쓸 수 없다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;저자가 글을 쓰고 있다면, 독자는 글을 읽을&amp;nbsp;수 없으며, 저자 또한 추가적으로 글을 쓸 수 없다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;크리티컬 섹션&lt;/b&gt;&amp;nbsp;: 책 (공유 데이터베이스)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;생산자 소비자 문제 (Producer Consumer Problem)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;한정 버퍼 문제(Bounded Buffer Problem)라고도 한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;생산자는 물건을 생산하여 창고(버퍼)에 넣는다. 소비자는 창고에서 물건을 꺼내서 소비한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;창고가 가득 차면 생산자는 물건을 넣을 수 없고, 창고가 비어 있으면 소비자는 물건을 소비할 수 없다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;크리티컬 섹션&lt;/b&gt;&amp;nbsp;: 창고 (버퍼)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;식사하는 철학자 문제 (Dining Philosopher Problem)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;원형 테이블에 철학자들이 앉아있고 철학자의 수만큼 젓가락이 철학자 사이에 하나씩 놓여있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;철학자들이 식사를 하기 위해서는 양쪽에 하나씩 놓여있는 젓가락을 둘 다 들어서 사용해야 한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;어떤 철학자가 젓가락을 사용 중이라면, 다른 어떤 철학자는 식사를 할 수 없다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;크리티컬 섹션&lt;/b&gt;&amp;nbsp;: 젓가락&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;임계 구역 문제의 해결 조건&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;상호 배제 (Mutual Exclusion)&lt;/b&gt; : 어떤 프로세스(또는 스레드)가 임계 구역에서 작업 중일 때, 다른 프로세스는 임계 구역으로 접근할 수 없다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;진행 (Progress)&lt;/b&gt; : 임계 구역에서 작업 중인 프로세스가 없다면, 임계 구역으로 진입하려는 프로세스 중 하나를 적절히 선택하여 임계 구역에 진입할 수 있게 해야 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;유한&amp;nbsp;대기 (Bounded Waiting)&lt;/b&gt; : 다른 프로세스의 기아(Starvation)을 방지하기 위해, 임계 구역에 한 번 접근했던 프로세스는 다시 임계 구역에 들어갈 때 제한을 두어야 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;임계 구역 문제의 해결 방안&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;소프트웨어 동기화 방법&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;피터슨의 알고리즘 (Peterson's Algorithm) :&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;피터슨의 해결안(Peterson's Solution)이라고도 하며, 2개의 프로세스만 있을 때 사용할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;데커의 알고리즘 (Dekker's Algorithm) : 피터슨의 알고리즘과 비슷하며, 2개의 프로세스만 있을 때 사용할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;램포트의 빵집 알고리즘 (&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;Lamport's bakery algorithm) : 2개 이상의 프로세스&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;에서 사용할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: rgb(255, 0, 127);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(255, 0, 127);&quot;&gt; Peterson Algorithm
&lt;/span&gt;&lt;/b&gt;
&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Shared data&lt;/span&gt;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; turn;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; flag[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;];

&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Process i&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    flag[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    turn &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; j;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (flag[j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; turn &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; j);
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Critical Section */&lt;/span&gt;
    flag[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Remainder Section */&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);

&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Process j&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    flag[j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    turn &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (flag[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; turn &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; i);
    &lt;span style=&quot;color: rgb(241, 95, 95); font-style: italic;&quot;&gt;/* Critical Section */&lt;/span&gt;
    flag[j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Remainder Section */&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;하드웨어&amp;nbsp;동기화 방법&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;락(lock)을 이용한 해결 방안이다. 프로세스가 락을 획득해야만 임계 구역에 진입할 수 있고, 임계 구역을 벗어나면 락을 반납하여, 임계 구역 문제를 해결한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;싱글 프로세서 환경에서는 공유 데이터가 변경되는 동안 인터럽트를 막으면&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;임계 구역 문제를 해결할 수 있다. 비선점형 커널에서도 이 방법을 사용한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;하지만, 멀티 프로세서 환경에서는 시스템 효율성 때문에 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;인터럽트를 막을 수 없다. 멀티 프로세서&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;환경에서는 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;동기화 하드웨어로&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;임계 구역 문제를 해결할 수 있다. 주로 TestAndSet()과 Swap() 명령어로 이를 구현한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: rgb(255, 0, 127);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(255, 0, 127);&quot;&gt; TestAndSet
&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;TestAndSet&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;target) {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; ret &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;target;
    &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;target &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; ret;
}
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (TestAndSet(lock));
    &lt;span style=&quot;color: rgb(241, 95, 95); font-style: italic;&quot;&gt;/* Critical Section */&lt;/span&gt;
    lock &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Remainder Section */&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);

&lt;b&gt;&lt;span style=&quot;color: rgb(255, 0, 127);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(255, 0, 127);&quot;&gt; Swap
&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;Swap&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;a, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;b) {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; temp &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;a;
    &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;b;
    &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; temp;
}
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
    key &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (key &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;) Swap(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;lock, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;key);
    &lt;span style=&quot;color: rgb(241, 95, 95); font-style: italic;&quot;&gt;/* Critical Section */&lt;/span&gt;
    lock &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* Remainder Section */&lt;/span&gt;
} &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/856</guid>
      <comments>https://rebas.tistory.com/856#entry856comment</comments>
      <pubDate>Mon, 29 Apr 2019 18:50:19 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; CPU 스케줄링</title>
      <link>https://rebas.tistory.com/863</link>
      <description>&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/990C494C5CC59C172C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F990C494C5CC59C172C&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;CPU 스케줄링 (CPU Scheduling, Processor Scheduling)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : CPU(프로세서)를 사용하려 하는 프로세스들 사이에서 우선순위를 관리하는 일&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 작업 처리율과 CPU 이용률을 향상시키기 위해 필요한 기법이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 비선점 스케줄링과 선점 스케줄링으로 나뉜다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;비선점 스케줄링 (Non-preemptive Scheduling)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 어떤 프로세스가 사용 중인 CPU를 다른 프로세스가 강제로 빼앗아 사용할 수 없는 스케줄링&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 일괄 처리 시스템에 적합하며, 응답 시간 예측이 쉬운 장점이 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 중요한 작업(짧은 작업)이 중요하지 않은 작업(긴 작업)을 기다리는 경우가 발생할 수 있는 단점이 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;FCFS 스케줄링 (First-Come First-Served Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;FIFO 스케줄링이라고도 하며, 준비 큐(Ready Queue)에 먼저 도착한 순서에 따라 CPU를 할당하는 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;먼저 들어온 프로세스가 먼저 처리되어 공평성은 유지되나, 중요한 작업이 중요하지 않은 작업을 기다릴 가능성이 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;SJF 스케줄링 (Shortest Job First Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;실행 시간이 가장 짧은 프로세스에 먼저 CPU를 할당하는 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스들의 평균 대기 시간이 가장 짧다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;기아 상태가&amp;nbsp;발생할 수 있다. 기아는 에이징으로 해결할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;HRRN 스케줄링&amp;nbsp;(Highest Reponse Ratio Next Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;HRN 스케줄링이라고도 하며, SJF 스케줄링의 문제점을 보완한&amp;nbsp;스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;실행 시간과&amp;nbsp;대기 시간을 둘 다&amp;nbsp;고려해 우선순위를 정하고, 각 프로세스의 우선순위에 따라 CPU를 할당한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;우선순위 = (대기 시간 + 실행 시간) / 실행 시간&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;우선순위 스케줄링 (Priority Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;각 프로세스마다 우선순위를 부여하고, 각 프로세스의 우선순위에 따라 CPU를 할당하는 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;우선순위는 시간&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;제한, 메모리 요구, 열린 파일의 수, 평균 실행 시간, 프로세스의 중요도, 자원 사용량 등을 복합적으로 고려하여 정한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;SJF 스케줄링과 마찬가지로 기아 상태가 발생할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;선점 스케줄링 (Preemptive Scheduling)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 어떤 프로세스가 사용 중인 CPU를 다른 프로세스가 강제로 빼앗아 사용할 수 있는 스케줄링&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 시분할 시스템에 적합하며, 응답 시간이 빠른 장점이 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ CPU를 선점할 프로세스에게 일정한 시간을 배정하기 위해 인터럽트용 타이머가 필요하다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 비선점 스케줄링에 비해 오버헤드가 큰 단점이 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;RR 스케줄링 (Round Robin Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;라운드 로빈 스케줄링은 시분할 시스템을 위해 설계된 스케줄링이다.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;FCFS 스케줄링과 유사하며, 선점이 가능하도록 시간 단위(Time Quantum, Time Slice)&amp;nbsp;개념이 추가돼있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;FCFS 처럼 준비 큐에 먼저 들어온 프로세스가 먼저 CPU를 할당받지만, 각 프로세스는 Time Quantum 만큼만 실행된 후 다시 준비 큐로 돌아간다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;Time Quantum이 커지면&amp;nbsp;FCFS 스케줄링과 같아지고, Time Quantum이 작으면 Context Switch가 자주 발생하여 오버헤드가 커진다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;SRT 스케줄링 (Shortest Remaining Time Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;SJF 스케줄링을 선점이 가능하도록 변형한 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;실행 중인 프로세스의 남은 실행 시간과 준비 큐에 도착한 프로세스의 실행 시간을 비교하고, 그중 실행 시간이 짧은 프로세스에게 CPU를 할당하는 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;MLQ 스케줄링 (Multilevel Queue Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;멀티레벨 큐 스케줄링(다단계 큐 스케줄링)은 프로세스를 특정 그룹으로 분류하고, 각 그룹에 따라 각기 다른 준비 큐를 이용하는 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;각 준비 큐는 각기 다른 스케줄링 알고리즘을 가질 수 있으며, 큐와 큐 사이에도 스케줄링 알고리즘이 필요하다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;MLFQ 스케줄링 (Multilevel Feedback Queue Scheduling)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;멀티레벨 피드백 큐 스케줄링(다단계 피드백 큐)는 멀티레벨 큐를 변형한 스케줄링이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;MLQ에서는 프로세스가 큐와 큐 사이를 이동할 수 없으나, MLFQ에서는 프로세스가 큐와 큐 사이를 이동할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;예를 들어, 특정 프로세스가 CPU를 너무 많이 사용하면(실행 시간이 길면), 우선순위가 낮은 큐로 이동된다. 또한, 우선순위가 낮은 큐에서 너무 오래 대기하는 프로세스가 있다면, 해당 프로세스는 우선순위가 높은 큐로 이동된다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;&lt;b&gt;스케줄링 이해를 돕기 위한 도표&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ &lt;b&gt;주어지는 조건&lt;/b&gt; : 4개의 프로세스와, 각 프로세스의 도착 시간(Arrival Time)과 실행 시간(Burst Time)&lt;/p&gt;&lt;p&gt;✓ &lt;b&gt;조건을 통해&amp;nbsp;구할 수 있는 것&lt;/b&gt; : 각 프로세스의&amp;nbsp;대기 시간(Waiting Time)과 소요 시간(Turnaround Time)&lt;/p&gt;&lt;p&gt;✓ Gantt 차트(막대 모양)를 통해 각 프로세스의 스케줄을 표현했다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 835px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99FBAB405CC6C63029&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99FBAB405CC6C63029&quot; width=&quot;835&quot; height=&quot;561&quot; filename=&quot;sc1.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 835px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/999F5B405CC6C63014&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F999F5B405CC6C63014&quot; width=&quot;835&quot; height=&quot;561&quot; filename=&quot;sc2.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 836px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99BB8E405CC6C6302D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99BB8E405CC6C6302D&quot; width=&quot;836&quot; height=&quot;562&quot; filename=&quot;sc3.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 836px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99EB0D405CC6C6312A&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99EB0D405CC6C6312A&quot; width=&quot;836&quot; height=&quot;560&quot; filename=&quot;sc4.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;용어 설명&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;도착 시간 (Arrival Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 준비 큐(Ready Queue)에 도착한 시각.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;완료 시간 (Completion Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 작업을 마친 시각.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;스케줄 시간 (Schedule Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 CPU에서 실행된 시각.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;응답 시간 (Response Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 도착 시간부터 CPU에서 실행되기&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;까지 걸린 시간. [도착 시간 - 스케줄 시간]과 같다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;실행 시간 (Execution Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 CPU에서 실행된 총 시간. 버스트 시간(Burst Time)이라고도 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;대기 시간 (Waiting Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 준비 큐에서 기다린 총 시간.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;소요 시간 (Turnaround Time)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스가 모든 작업을 마치는데 걸린 시간. [완료 시간 - 도착 시간] 또는 [실행 시간 + 대기 시간]과 같다. 반환 시간, 총 처리 시간이라고도 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;기아 (Starvation)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 실행 시간이 긴 프로세스가 자원을 계속 할당받지 못하는 문제이다.&amp;nbsp;실행 시간이 긴 프로세스가 대기하는 상황에서, 실행 시간이 짧은 프로세스가 준비 큐에 계속 들어오면, 실행 시간이 긴 프로세스는 계속 자원을 할당받지 못하는 문제가 생긴다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;에이징 (Aging)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;: 준비 큐에 있는 프로세스에 나이를 부여하는 방법이다. 특정 프로세스가 얼마나 오래 기다렸는지&amp;nbsp;고려하고,&amp;nbsp;기다린 시간이 길다면 자원을 할당해 준다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/863</guid>
      <comments>https://rebas.tistory.com/863#entry863comment</comments>
      <pubDate>Mon, 29 Apr 2019 18:48:47 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 프로세스 스케줄링</title>
      <link>https://rebas.tistory.com/860</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99A8264F5CC4FF6D04&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99A8264F5CC4FF6D04&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스 스케줄링 (Process Scheduling)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 멀티 프로그래밍 환경에서 사용되는 것으로,&amp;nbsp;프로세스들에게 자원 할당&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;을 적절히 함으로써 시스템의 성능을 개선하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ CPU 이용률을 극대화하여 다수의 프로세스를 처리할 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;스케줄러의 종류&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;장기 스케줄러 (Long-term Scheduler)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;잡 스케줄러(Job Scheduler)라고도 하며, 디스크에 있는 프로세스들을 메모리로 적재하는 역할을 한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;즉, 장기 스케줄링은 생성된 프로세스들&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;중 어떤 프로세스에 메모리를 할당하여 준비 큐(Ready Queue)로 보내는 것이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;Unix, Microsoft Windows 등의 시분할 시스템에는 장기 스케줄러가 없다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;단기 스케줄러 (Short-term Scheduler)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;CPU 스케줄러(CPU Scheduler)라고도 하며, 준비 상태의 프로세스를 하나 선택하여 CPU에 할당하는 역할을 한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;즉, 단기 스케줄링은 준비 큐에 있는 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스 중 어떤 프로세스를 실행(Running) 시킬&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;지 결정하는 것이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;장기 스케줄러에 비해 실행 간격이 짧으며, 실행 빈도가 높다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;중기 스케줄러 (Mid-term Scheduler)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;메모리에서 프로세스를 제거하여 멀티 프로그래밍의 정도를 완화시키는 역할을 한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;스와핑 작업을 통해 프로세스 혼합 상태를 개선하거나, 가용 메모리를 확보한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;스와핑(Swapping) : 메모리에 적재된 프로세스를 디스크로 옮기고(스왑 아웃; Swap out), 디스크에 있는 프로세스를 메모리로 다시 적재하는(스왑 인; Swap in) 작업이다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99F2E5355CC5359515&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99F2E5355CC5359515&quot; width=&quot;860&quot; height=&quot;590&quot; filename=&quot;os8.jpg&quot; filemime=&quot;image/jpeg&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; &quot;&gt;[그림 1] 프로세스 상태에 따른 스케줄러&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/860</guid>
      <comments>https://rebas.tistory.com/860#entry860comment</comments>
      <pubDate>Sun, 28 Apr 2019 21:15:53 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 프로세스 간 통신</title>
      <link>https://rebas.tistory.com/854</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9917FD435CC132053C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9917FD435CC132053C&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스 간 통신 (IPC,&amp;nbsp;Inter Process Communication)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; :&amp;nbsp;프로세스끼리&amp;nbsp;자원이나 데이터를 서로 주고받는 행위 또는 그에 대한 방법이나 경로&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 프로세스는 본래 독립적이나, 상황에 따라 프로세스끼리 협력해야 하는 경우가 있다. 이 경우, 프로세스 간 자원/데이터 공유가 필요한데, 이때 필요한 것이 IPC이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;IPC가 필요한 이유&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정보 공유&lt;/b&gt; (Information sharing) : 여러 사용자가 동일한&amp;nbsp;정보를 필요로 할 수&amp;nbsp;있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;계산 가속화&lt;/b&gt; (Computation speedup) : 특정 작업(task)를 빠르게 실행하기 위해, 해당 작업을 부분 작업(서브 태스크)으로 나눠서 병렬로 실행하게 할 수 있다.&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;모듈성&lt;/b&gt; (Modularity) : 특정한 시스템 기능을 별도의 프로세스(스레드)로 구분하여 모듈식 형태로 시스템을 구성할 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;편의성&lt;/b&gt; (Convenience) : 여러 사용자들이 동시에 많은 작업을 수행할 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;IPC의 종류&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;IPC 기법은 크게 메시지 전달과 공유 메모리로 나뉜다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;메시지 전달 (Message passing)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;특징&lt;/b&gt; : IPC를 위해 커널을 통해 메시지를 전달하는 방식으로 자원이나 데이터를 주고받는다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;장점&lt;/b&gt; : 별도로 다른 것을 구축할 필요 없이 커널을 이용하기 때문에 구현이 비교적 쉽다.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;단점&lt;/b&gt; : 커널을 이용하기 때문에, 시스템 콜(System call)이 필요하며 이로 인해 오버헤드가 발생한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;종류&lt;/b&gt; : 파이프&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;, 시그널&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;, 메시지 큐&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;, 소켓 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;등&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;공유 메모리 (Shared memory)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;특징&lt;/b&gt; : IPC를 위해 공유 메모리 영역을 구축하고, 공유 영역을 통해 자원이나 데이터를 주고받는다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;장점&lt;/b&gt; : 커널 의존성이 낮기 때문에 속도가 빠르다. 유저 레벨에서 IPC가 가능하기 때문에, 통신이 자유롭다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;단점&lt;/b&gt; : 자원과 데이터를 공유하기 때문에 동기화 이슈가 발생한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 700px; width: 700px; height: 481px;; height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/997804425CC3DBE627&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F997804425CC3DBE627&quot; width=&quot;700&quot; height=&quot;481&quot; filename=&quot;os6.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;width: 700px; height: 481px;&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; width: 700px; height: 481px;;&quot;&gt;[그림 1] 메시지 전달과 공유 메모리&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;메시지 전달 모델&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;파이프 (Pipe)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;특징&lt;/b&gt; : 하나의 프로세스가 파이프를 통해 다른 프로세스로 메시지를 직접 전달하는 방식&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;Half-duplex 방식이기 때문에, 데이터는 한쪽 방향으로만 이동한다. 양방향 통신을 하기 위해서는 두 개의 파이프가 필요하다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;파이프에 용량 제한이 있기 때문에 이용에 제약이 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;시그널 (Signal)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;특징&lt;/b&gt; : 프로세스 ID를 통해 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;특정 프로세스에게 메시지를 전달하는 방식&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;시그널 ID에 따라 어떤 이벤트인지 알 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;메시지 큐 (Message Queue)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;특징&lt;/b&gt; : 고정 크기의 메시지를 연결 리스트를 통해 통신하는 방식&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;메시지 단위의 통신이며, 메시지 큐 ID를 통해 통신을 한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;소켓 (Socket)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;특징&lt;/b&gt; : 네트워크 상에서 프로세스 간에 통신하는 방식&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;Local 뿐만 아니라, Remote 통신이 가능하다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/854</guid>
      <comments>https://rebas.tistory.com/854#entry854comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:08:28 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 문맥 교환 (컨텍스트 전환)</title>
      <link>https://rebas.tistory.com/855</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99FEA03E5CC156670D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99FEA03E5CC156670D&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;컨텍스트 전환 (Context Switch)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 하나의 프로세스가 CPU를 점유한 상태에서, 다른 프로세스가 CPU를 사용하게 만들기 위해, 현재 프로세스의 정보&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;(Context)를 저장하고, 새로운 프로세스의 정보&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;를 CPU에 적재하는 작업&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ Context Switch를 직역하면 '문맥 교환'이지만, '&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;컨텍스트 스위치' 또는 '컨텍스트 전환'으로 부르는 편이 명확하다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;프로세스의 Context는 프로세스 제어 블록(PCB)에 저장되어 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;문제점&lt;/b&gt; : Context Switching 중에는 CPU가&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;작업을 수행할 수 없기 때문에, 문맥 교환이 잦으면 오버헤드가 커진다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;Context Switch의 시점&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 멀티 태스킹&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 인터럽트 핸들링&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 유저 모드와 커널 모드 간 전환&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/855</guid>
      <comments>https://rebas.tistory.com/855#entry855comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:08:09 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 인터럽트, 트랩</title>
      <link>https://rebas.tistory.com/862</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9974F8365CC5768904&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9974F8365CC5768904&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;인터럽트와 트랩의 차이점&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;인터럽트&lt;/b&gt; : 하드웨어적 흐름의 변화 / 프로그램 외부(&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;I/O 장치, 디스크 등)에서 발생하며, 발생 시점이 일정하지 않기 때문에 비동기적이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;트랩&lt;/b&gt; : 소프트웨어적 흐름의 변화 / 소프트웨어 인터럽트라고도 하며, 프로그램 내부에서 일어나는 에러이다. 발생 시점이 프로그램의 일정한 지점이기 때문에 동기적이다. 즉, 고정된 영역에서 일어난다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;인터럽트의 종류&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;하드웨어 인터럽트 (Hardware Interrupt)&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;인터럽트는 외부 인터럽트와 내부 인터럽트로 구분하는데, &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;외부 인터럽트를 하드웨어 인터럽트라 한다. 그냥 인터럽트라 하면, 일반적으로&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;하드웨어 인터럽트를 의미한다. 내부 인터럽트는 소프트웨어 인터럽트와 같은 개념이라고 보면 된다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;외부 인터럽트는 프로그램 외부에서 일어나는 인터럽트이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;기계 검사 인터럽트, 입출력 인터럽트, 전원 이상 인터럽트 등이 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;소프트웨어 인터럽트 (SWI, Software Interrupt)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;트랩(Trap)이라고도 하며, 프로그램 내부에서 일어나는 인터럽트이다. &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;예외와 시스템 콜이 있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;예외 (Exception)&lt;/b&gt; : &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;메모리 참조 오류, 0으로 나누기, Overflow, Underflow 등의&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;경우에서 발생하는 인터럽트&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;시스템 콜 (System Call)&lt;/b&gt; : 사용자가 의도적으로 일으킨 인터럽트&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/862</guid>
      <comments>https://rebas.tistory.com/862#entry862comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:07:12 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 프로세스 상태</title>
      <link>https://rebas.tistory.com/852</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99F010505CC0043C14&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99F010505CC0043C14&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스 상태 (Process State)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스는 실행의 흐름에 따라 상태(스테이트)가 변한다. 상태를 정의하는 이름은 운영체제에 따라 다르지만, 대부분 서로 비슷한 개념으로 구성되어 있다. 프로세스의 흐름은 일반적으로 5개의 상태로 정의된다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9974B4465CC131A60E&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9974B4465CC131A60E&quot; width=&quot;860&quot; height=&quot;455&quot; filename=&quot;os4.jpg&quot; filemime=&quot;image/jpeg&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; &quot;&gt;[그림 1] 프로세스 상태 전이도&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스의 5가지 상태&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;생성 (New)&lt;/b&gt; : 프로세스&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;생성 상태&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;실행 (Running)&lt;/b&gt; : 프로세스가 CPU에 할당되어&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;실행 중인 상태&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓&amp;nbsp;&lt;b&gt;준비 (Ready)&lt;/b&gt; : 프로세스가 CPU에 할당되기를 기다리는 상태&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;대기 (Waiting)&lt;/b&gt; : 보류(Block)라고도 하며, 프로세스가 입출력이나 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;이벤트를 기다리는 상태&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;종료 (Terminated)&lt;/b&gt; : 프로세스 종료 상태&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스의 상태 전이&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;승인 (Admitted)&lt;/b&gt; : 프로세스 생성이 가능하여 승인됨.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;스케줄러 디스패치 (Scheduler Dispatch)&lt;/b&gt; : 준비 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;상태에 있는 프로세스 중 하나를 선택하여 실행&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;시키는 것.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;인터럽트 (Interrupt)&lt;/b&gt; : &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;예외, &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;입출력, 이벤트 등이 발생하여 현재 실행 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;중인 프로세스를 준비 상태&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;로 바꾸고, 해당 작업을 먼저 처리하는 것.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;입출력 또는 이벤트 대기 (I/O or Event wait)&lt;/b&gt; : 실행 중인&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;프로세스가 입출력이나 이벤트를 처리해야 하는 경우, 입출력/이벤트가 모두 끝날 때까지 대기 상태로 만드는 것.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;입출력 또는 이벤트 완료 (I/O or Event Completion)&lt;/b&gt; : 입출력/이벤트가 끝난 프로세스를 준비 상태로 전환하여 스케줄러에 의해 선택될 수 있도록 만드는 것.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/852</guid>
      <comments>https://rebas.tistory.com/852#entry852comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:05:21 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 프로세스 제어 블록</title>
      <link>https://rebas.tistory.com/853</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99BCED415CC0423D0C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99BCED415CC0423D0C&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스 제어 블록 (PCB, Process Control Block)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 프로세스에 관한 다양한 정보를 가지고 있는 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;커널의 자료 구조&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 작업 제어 블록(Task Control Block)이라고도 하며, &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;운영체제가 프로세스를 표현한 형태이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스에 관한 중요한 정보를 포함하&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;기 때문에, &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;사용자가 접근하지 못하도록 보호된 메모리 영역 안에 위치한다&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;. 일부 운영체제에서는 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;커널 스택의 처음에 위치한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ Linux 운영체제에서는 &amp;lt;linux/sched.h&amp;gt; 헤더 파일&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;의 struct task_struct로 표현된다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;/* PCB Example (task_struct) */&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; task_struct {
	&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;volatile&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt;	state;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt;		&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;stack;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;atomic_t&lt;/span&gt;		usage;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;		recent_used_cpu;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;		wake_cpu;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;		prio;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;	rt_priority;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;pid_t&lt;/span&gt;		pid;
	&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;pid_t&lt;/span&gt;		tgid;
	&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; task_struct __rcu	&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;parent;
	&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; list_head		children;
	&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; list_head		sibling;
	u64				utime;
	u64				stime;
};&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;PCB가 포함하는 정보&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스 ID&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;(Process ID) : 프로세스를 식별하기 위한 ID&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스 상태&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; (Process State) : 생성(New&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;), 준비(Ready), 실행(Running), 대기(Waiting), 종료(Terminated)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로그램 카운터&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; (Process Counter) : 해당 프로세스가 다음에 실행할 명령어의 주소 가리킴.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;메모리 관리 정보&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 페이지 테이블 또는 세그먼트 테이블, 베이스 레지스터 등&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스 계정 정보&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : CPU가 사용한 양, 사용된 실제 시간, 계정 번호&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;등&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;입출력 상태 정보&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 프로세스에 할당된 입출력장치 목록, 열린 파일 목록 등&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;CPU 스케줄링 정보&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 스케줄링 우선순위, 스케줄 큐 포인터 등&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;CPU 레지스터&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 누산기, 인덱스, 스택 레지스터 등&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;기타 프로세스 관련 정보&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/853</guid>
      <comments>https://rebas.tistory.com/853#entry853comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:05:04 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 멀티 스레드, 멀티 스레딩</title>
      <link>https://rebas.tistory.com/851</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99C315425CBFE11903&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99C315425CBFE11903&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;멀티 프로세스&lt;/span&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;와 멀티 스레드&lt;/span&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;의 차이점&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;멀티 프로세스&lt;/b&gt; : &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;독립적인 다수의 프로세스, 메모리 공간(&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;코드, 데이터, 힙, 스택)&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;을 공유하지 않음.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;멀티 스레드&lt;/b&gt; : 하나의 프로세스 내에 존재하는 다수의 스레드, 메모리 공간(코드, 데이터, 힙)을 공유하지만, 스택 영역은 공유하지 않음.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 700px; width: 700px; height: 355px;; height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99DFB0435CC3DA6224&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99DFB0435CC3DA6224&quot; width=&quot;700&quot; height=&quot;355&quot; filename=&quot;os5.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;width: 700px; height: 355px;&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; width: 700px; height: 355px;;&quot;&gt;[그림 1] 싱글 스레드와 멀티 스레드&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;멀티 스레딩 (Multi-threading)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 하나의 프로세스를 다수의 스레드로 구성하여 서로 자원을 공유하고, 이를 통해 자원을 효율적으로 사용하여 작업 처리 속도를 향상시키는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 각 스레드는 프로세스 내에 존재하며, 코드/데이터/힙 영역을 공유하면서 데이터를 주고받을 수 있다. 이러한 장점 때문에, 프로세스 간 통신(IPC)에 비해 통신 오버헤드가 적고 속도가 빠르다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 멀티 스레딩을 통해 시스템의 처리량이 향상되고, 자원 소모가 줄어들어 프로그램의 응답시간이 단축된다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;멀티 스레딩의 문제점&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 자원을 공유하기 때문에, 다수의 스레드가 특정 자원을 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;동시에 사용하는 것을&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;고려해야 한다. 즉, 동기화 처리가 필요하다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 동기화를 통해 스레드의 작업 처리 순서와 공유 자원에 대한 접근을 제어해야 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 일반적으로 락(Lock)을 이용하여 동기화 처리를 하는데, 락이 과도하면 병목 현상이 발생하여 성능 저하를 유발할 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/851</guid>
      <comments>https://rebas.tistory.com/851#entry851comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:03:15 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 멀티 프로세싱, 멀티 프로그래밍, 멀티 태스킹</title>
      <link>https://rebas.tistory.com/850</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99159A355CBF42D22C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99159A355CBF42D22C&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;멀티 프로세싱,&amp;nbsp;멀티 프로그래밍, 멀티 태스킹의 차이점&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;멀티 프로세싱&lt;/b&gt; : 다수의 프로세서가 다수의 프로세스를&amp;nbsp;동시에 처리하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;멀티 프로그래밍&lt;/b&gt; : 다수의 프로세스를&amp;nbsp;메모리에 적재하여 프로세스를 번갈아가면서 처리하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;멀티 태스킹&lt;/b&gt; : 다수의 작업을&amp;nbsp;운영체제 스케줄링에 의해 번갈아가면서 처리하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;멀티 프로세싱 (Multi-processing)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 다수의 프로세서가 다수의 프로세스를&amp;nbsp;협력적으로 동시에 처리하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 프로세서(Processor)는 CPU라고 생각하면 되며, 프로세스(Process)와 다른 개념이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 각 프로세서는 다수의 프로세스를&amp;nbsp;처리하며, 각 프로세스는&amp;nbsp;다수의 프로세서에 의해 처리된다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 각 프로세서가 자원을 공유하면서 프로세스를&amp;nbsp;처리하기 때문에, 하나의 프로세서가 고장 나더라도 작업은 정지되지 않는다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;멀티 프로그래밍 (Multi-programming)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 하나의 프로세서에 대하여 다수의 프로세스를 메모리에 적재해&amp;nbsp;작업을 처리하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 특정 프로세서가 프로세스 A를 처리할 때, 다른 프로세스 B, C 등을 처리하게 만드는 것을 말한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 멀티 프로그래밍을 통해 프로세서를 효율적으로 사용할 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;멀티 태스킹 (Multi-tasking)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 다수의 작업(Task)을 운영체제 스케줄링에 의해 번갈아가면서 처리하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;✓ Task란 작업의 단위를 말하며, 프로세스, 스레드가 모두 작업의 단위가 될 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 멀티 태스킹은 시분할 시스템에서 사용되며, 사용자에게 다수의 작업이 동시에 처리되는 것처럼 느끼게 할 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/850</guid>
      <comments>https://rebas.tistory.com/850#entry850comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:03:05 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 프로세스, 스레드</title>
      <link>https://rebas.tistory.com/849</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99D6CF3C5CBF22371B&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99D6CF3C5CBF22371B&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스와 스&lt;/span&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;레드의 차이점&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;clear: none; float: none;&quot;&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;✓&lt;/span&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;프로세스&lt;/b&gt; : 운영체제로부터 자원을 할당받는 작업의 단위&lt;/p&gt;&lt;p style=&quot;clear: none; float: none;&quot;&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;✓&lt;/span&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;스레드&lt;/b&gt; : 프로세스가 할당받은 자원을 이용하는 실행의 단위&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;div&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;프로세스 (Process)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px; width: 600px; height: 516px;; height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/992EEF3F5CBF2F5024&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F992EEF3F5CBF2F5024&quot; width=&quot;600&quot; height=&quot;516&quot; filename=&quot;os1.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;width: 600px; height: 516px;&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; width: 600px; height: 516px;;&quot;&gt;[그림 1] 독립적인 프로세스&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;✓ &lt;/span&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정의&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; : 실행 중인 프로그램에 대한 인스턴스&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 프로세스는 운영체제로부터 각각 독립된 자원(code, data, stack, heap, PC register 등)을 할당받는다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 각 프로세스는 최소 1개 이상의 쓰레드를 가지고 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 다른 프로세스의 자원에 접근하려면, 프로세스 간 통신(IPC)을 이용해야 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 프로세스와 프로그램의 차이&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로그램 : Passive entity, 명령어 리스트를 지닌 실행 파일 클래스&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스 : Active entity, 메모리에 적재되어 프로그램 카운터와 자원을 가진 인스턴스&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px; width: 600px; height: 368px;; height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99EAFE4A5CC1316B0B&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99EAFE4A5CC1316B0B&quot; width=&quot;600&quot; height=&quot;368&quot; filename=&quot;os3.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;width: 600px; height: 368px;&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; width: 600px; height: 368px;;&quot;&gt;[그림 2] 메모리 상의 프로세스&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;스레드 (Thread)&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px; width: 600px; height: 401px;; height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99DE833E5CBF2FD72C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99DE833E5CBF2FD72C&quot; width=&quot;600&quot; height=&quot;401&quot; filename=&quot;os2.jpg&quot; filemime=&quot;image/jpeg&quot; style=&quot;width: 600px; height: 401px;&quot; original=&quot;yes&quot;/&gt;&lt;span class=&quot;cap1&quot; style=&quot;display: block; max-width:100%; width: 600px; height: 401px;;&quot;&gt;[그림 3] 프로세스 내의 스레드&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 한 프로세스 내에서 동작되는 여러 실행의 흐름&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;✓ 프로세스의 특성을 가지고 있기 때문에, 경량 프로세스(LWP, Lightweight Process)라고도 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 프로세스 내의 코드, 데이터, 힙 등의 자원을 공유하면서 병렬성을 높일 수 있다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;✓ 스택과 레지스터는 각 스레드가 별도로&amp;nbsp;할당받으며, 이는 독립적인 실행 흐름을 가능하게 한다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 기본적으로 하나의 프로세스가 생성되면 하나의 스레드가 같이 생성되며, 이를 메인 스레드라고 부른다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ 하나의 프로세스는 여러 개의 스레드를 가질 수 있으며, 이를 멀티 스레드라고 부른다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;스레드의 장단점&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;장점 : 자원 공유를 통해 시스템 자원 소모를 줄일 수 있음, Context Switch에 대한 오버헤드가 감소.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;단점 : 동기화 문제가 발생할 수 있음, 프로그램 디버깅이 어려울 수 있음.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/849</guid>
      <comments>https://rebas.tistory.com/849#entry849comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:02:47 +0900</pubDate>
    </item>
    <item>
      <title>운영체제 &amp;middot; 운영체제 개념</title>
      <link>https://rebas.tistory.com/861</link>
      <description>&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 626px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99771A465CC5538132&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99771A465CC5538132&quot; width=&quot;626&quot; height=&quot;457&quot; filename=&quot;sketch.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;운영체제 (OS, Operating System)&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;정의&lt;/b&gt; : 컴퓨터 시스템의 자원들을 효율적으로 관리하고, 사용자가 컴퓨터를 편리하게 사용할 수 있도록 환경을 제공하는 것&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;종류&lt;/b&gt; : Unix, Linux, MS-DOS, Windows, m&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;acOS 등&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;운영체제의 역할&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;하드웨어와 사용자 간의 인터페이스 제공 (CUI, GUI&amp;nbsp;등)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로세스 처리 및 스케줄링&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;CPU, 입출력장치, 파일 등의 자원 관리&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;하드웨어, 네트워크 제어 등 다양한 역할 수행&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;운영체제의 목적&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;처리 능력(Throughput) 향상&lt;/b&gt; : 처리 능력이란 일정 시간 내에 처리할 수 있는 작업량을 뜻하며, 시스템 전체의 생산성을 측정하는 단위이다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;응답 시간(Turn Around Time) 단축&lt;/b&gt; : 응답 시간이란 사용자가 처리를 요구한 시점부터 결과를 얻을 때까지 걸리는 시간을 말하며, 값이 낮을&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;수록 좋다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;사용 가능도(Availability) 향상&lt;/b&gt; : 원하는 시간 내에 시스템을 얼마나 빨리 사용할 수 있는지에 대한 정도를 나타낸다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;신뢰도(Reliability) 향상&lt;/b&gt; : 시스템이 주어진 문제를 얼마나 정확하게 수행하는지에 대한 정도를 나타낸다.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;운영체제 방식&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;일괄 처리 시스템 (Batch Processing System)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;데이터를 일정한 양이나 일정한 기간만큼 모은 후, 한꺼번에 처리하는 시스템이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;데이터를 순차적으로 처리하며, 하나의 작업이 끝나기 전까지는 다른 작업을 수행할 수 없다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;시분할 시스템 (Time Sharing System)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;CPU 스케줄링과 멀티 프로그래밍을 통해 컴퓨터 자원을 시간적으로 분할하여&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;, 각 사용자들이 동시에 모두&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;사용할 수 있게 해주는 시스템이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;엄밀히 말하자면 시스템의 자원을 정해진 시간(Time slice, Quantum) 만큼 번갈아가면서 사용하는 것이지만, 워낙 짧은 시간 안에 빠르게 작업이 처리되기 때문에, 사용자들은 동시에 사용하는 것처럼 느낄 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;이 시스템을 통해 모든 작업이 동시에 진행되는 것처럼 대화식 처리가 가능하다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;실시간 시스템 (Real-time System)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;자원이 한정돼있는 상황에서 작업 수행이 요청되었을 때, 정해진&amp;nbsp;시간 내에 결과를 처리해주는 시스템이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;시간의 정확성과 예측성을 보장해야 하는 환경에서 사용된다. 예) 공장 제어, 인공위성 제어, 무기 제어 등&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;✓ &lt;b&gt;분산 처리 시스템 (Distributed Processing System)&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;다수의 컴퓨터(프로세서)를&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;네트워크로 연결하여 &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;작업을 처리하는 시스템이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;다수의 프로세서를 이용하기 때문에, 무겁고 복잡한 계산 문제를 보다 빠르게 계산할 수 있다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/운영체제</category>
      <category>Operating System</category>
      <category>OS</category>
      <category>운영체제</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/861</guid>
      <comments>https://rebas.tistory.com/861#entry861comment</comments>
      <pubDate>Sun, 28 Apr 2019 20:02:22 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17144 &amp;middot; 미세먼지 안녕!</title>
      <link>https://rebas.tistory.com/848</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/992C9E435CB6EB6510&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F992C9E435CB6EB6510&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 시뮬레이션&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;문제에 주어진 조건을 그대로 구현하는 문제다. 크게 두 단계로 나눠서 설계해야 한다. 첫 번째, 미세먼지가 확산되는 과정, 두 번째 공기청정기의 바람이 순환하는 과정이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;1. 모든 미세먼지는 5 이상의 양이 남아있으면, 상하좌우로 확산할 수 있다.&lt;/li&gt;&lt;li&gt;확산할 때&amp;nbsp;상하좌우에 이미 먼지가 있는 경우가 있으므로, 별도의 배열을 만들어서 확산되는 먼지의 양을 저장해야 한다. 값을 바로 업데이트하면 다음 먼지 확산에 영향을 주기 때문에, 이 과정이 필요하다.&lt;/li&gt;&lt;li&gt;현재 먼지의 양을 5로 나눈 후, 그 양을 상하좌우 칸에 더한다. 위에서 만든 별도의 배열 B에 더한다.&lt;/li&gt;&lt;li&gt;모든 과정이 끝나면, 원래 먼지 배열 A에 확산 배열 B 값을 업데이트한다.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;2. 미세먼지가 바람 방향에 따라 순환한다.&lt;/li&gt;&lt;li&gt;입력받을 때 공기청정기의 위치를 S1, S2에 저장한다.&amp;nbsp;&lt;/li&gt;&lt;li&gt;위쪽 공기청정기는 S1을 기준으로 반시계 방향으로 회전한다.&lt;/li&gt;&lt;li&gt;아래쪽 공기청정기는 S2를 기준으로 시계 방향으로 회전한다.&lt;/li&gt;&lt;li&gt;역순으로 회전하면서, 현재 값에 다음&amp;nbsp;값을 저장하면 된다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; air {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; r, c, t, s1&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=-1&lt;/span&gt;, s2, ans;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};


&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;diffuse&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;};
    memcpy(b, a, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(a));
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;r; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;) {
                &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;/5&lt;/span&gt;;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[k], nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[k];
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; r &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; c &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; a[ni][nj] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                        b[ni][nj] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d;
                        b[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; d;
                    }
                }
            }
        }
    }
    memcpy(a, b, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(b));
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;purify&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Top purifier&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;s1&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;];  &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// ↓&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;];    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// ←&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;s1; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) a[i][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]; &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// ↑&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) a[s1][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[s1][i]; &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// →&lt;/span&gt;
    a[s1][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Bottom purifier&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;s2&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;];   &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// ↑&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) a[r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;];  &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// ←&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt;s2; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;];&lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// ↓&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) a[s2][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[s2][i];   &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// →&lt;/span&gt;
    a[s2][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
        diffuse();
        purify();
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;r; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) {
                ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; a[i][j];
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;r, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;c, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;r; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// s1 (top), s2 (bottom)&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (s1 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) s1 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; s2 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i;
            }
        }
    }
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;r, c, t &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r)]
s1, s2 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;diffuse&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;global&lt;/span&gt; a
    b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;c &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r)]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;:
                d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;//5&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; dx, dy &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
                    ni, nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx, j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; r &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; c &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; a[ni][nj] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                        b[ni][nj] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d
                        a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; d
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c):
            a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; b[i][j]

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;purify&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(s1&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(s1):
        a[i][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[s1][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[s1][i]
    a[s1][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(s2&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, s2&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        a[s2][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[s2][i]
    a[s2][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(t):
        diffuse()
        purify()
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;sum&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;sum&lt;/span&gt;, a))&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+2&lt;/span&gt;)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; s1 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                s1 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                s2 &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i
solve()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17144&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17144&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/848</guid>
      <comments>https://rebas.tistory.com/848#entry848comment</comments>
      <pubDate>Wed, 17 Apr 2019 18:54:26 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17143 &amp;middot; 낚시왕</title>
      <link>https://rebas.tistory.com/847</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/994E233E5CB6C2AC12&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F994E233E5CB6C2AC12&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 시뮬레이션&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;문제에 주어진 조건을 그대로 구현해야 하는 문제다.&amp;nbsp;크게 두 단계로 나눠서 설계해야 한다. 첫 번째, 낚시왕이 움직인 후 상어를 낚는 과정, 두 번째, 모든 상어가 제각기 움직이면서 서로를 잡아먹는 과정이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;1. 낚시왕은 매 턴마다 한 칸 움직인 후, 상어 한 마리를 낚는다.&lt;/li&gt;&lt;li&gt;먼저 낚시왕이 한 칸 앞으로 이동한다. 낚시왕은 최대 R(열의 개수)번 움직일 수 있다.&lt;/li&gt;&lt;li&gt;도착한 열에서 땅에 가장 가까운 상어 한 마리를 낚는다. 땅에 가장 가까운 상어는 0에 가까운 행(C)에 위치한다.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;2. 상어가 제각기 주어진 속도와 방향에 따라 움직이고, 자신보다 작은 상어를 잡아먹는다.&lt;/li&gt;&lt;li&gt;상어의 속도는 움직일 수 있는 칸의 개수를 의미한다. 가장자리에 도달하면 방향을 바꿔서 남은 횟수만큼 움직여야 한다.&lt;/li&gt;&lt;li&gt;매번 한 칸씩 움직이는 것은&amp;nbsp;비효율적이므로, 규칙을 찾는 것이 좋다. X에서 출발했다고 가정하면, 움직이다가 결국은 X로 돌아오게 되어있다. 즉, 같은 움직임을 주기적으로 반복한다.&lt;/li&gt;&lt;li&gt;속도를 S, 맵의 크기를 R * C 라고 하자. 단, 맵의 인덱스 범위는 [0, 0] ~ [R-1, C-1] 이다.&lt;/li&gt;&lt;li&gt;위/아래로 움직일 때는 S % ((R-1)*2) 만큼 반복된다. 왼/오른쪽으로 움직일 때는 S % ((C-1)*2) 만큼 반복된다.&lt;/li&gt;&lt;li&gt;구한 주기만큼 움직이면서, 가장자리에 부딪히면 방향을 바꾼다. 위(0) ↔ 아래(1), 오른쪽(2) ↔ 왼쪽(3)&lt;/li&gt;&lt;li&gt;움직임을 마친 후, 도착한 위치에 자신보다 큰 상어가 있는지 확인한다. 이때, 이미 이동을 마친 상어와 크기를 비교해야 한다. 이를 위해 별도의 맵을 만들어 둘 필요가 있다. A는 원래 상어가 위치한 맵이며, B는 상어의 이동을 저장할 맵이다.&lt;/li&gt;&lt;li&gt;자신보다 큰 상어가 없다면, 이동한 상어의 값을&amp;nbsp;B에 저장한다. 원래 상어가 위치한 A의 값은 0으로 초기화한다.&lt;/li&gt;&lt;li&gt;모든 상어의 이동을 마친 후, B에 들어있는 모든 값을&amp;nbsp;A에 복사한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; shark {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; s, d, z;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; r, c, m, ans;
shark a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c; t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        shark b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;};
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Fishing a shark&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;r; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][t].z) {
                ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; a[i][t].z;
                a[i][t] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;};
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;;
            }
        }
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Sharks move&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;r; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;c; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j].z) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j].d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;) { &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Up, Down&lt;/span&gt;
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j].s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; ((r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;);
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j].d;
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i;
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (s&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
                            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;) d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
                            ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; dx[d];
                        }
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j].z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; b[ni][j].z) b[ni][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {a[i][j].s, d, a[i][j].z};
                    } &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; { &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Right, Left&lt;/span&gt;
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j].s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; ((c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;);
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j].d;
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; j;
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (s&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
                            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;) d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;;
                            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;) d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;;
                            nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; dy[d];
                        }
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j].z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; b[i][nj].z) b[i][nj] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {a[i][j].s, d, a[i][j].z};
                    }
                    a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;};
                }
            }
        }
        memcpy(a, b, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(b));
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;r, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;c, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y, s, d, z;
        scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;x, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;y, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;s, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;d, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;z);
        a[x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {s, d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, z};
    }
    solve();
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;r, c, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r)]
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
    x, y, s, d, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
    a[x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [s, d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, z]
dx, dy, ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;), &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; t &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c):
    b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r)]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r):
        _, _, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][t]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; z:
            ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; z
            a[i][t] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(r):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(c):
            s, d, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; z:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;:
                    ns, nd, ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; ((r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;), d, i
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(ns):
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
                            nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; r&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;:
                            nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
                        ni &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; dx[nd]
                    _, _, bz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[ni][j]
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; bz:
                        b[ni][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [s, nd, z]
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                    ns, nd, nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; ((c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;), d, j
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(ns):
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;:
                            nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; c&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;:
                            nd &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;
                        nj &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; dy[nd]
                    _, _, bz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[i][nj]
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; bz:
                        b[i][nj] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [s, nd, z]
                a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]
    a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[:]

&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17143&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17143&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/847</guid>
      <comments>https://rebas.tistory.com/847#entry847comment</comments>
      <pubDate>Wed, 17 Apr 2019 15:41:27 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17142 &amp;middot; 연구소 3</title>
      <link>https://rebas.tistory.com/846</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99512C335CB347BF1E&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99512C335CB347BF1E&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS, 브루트 포스&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;연구소에 바이러스를 퍼트리는 데 걸리는 최소 시간을 구하는 문제다. 바이러스를 놓을 수 있는 경우를 모두 만든 후, 각 케이스에 대해 BFS로 바이러스를 퍼트려서 최솟값을 구하면 된다. BOJ 17141번 '&lt;a href=&quot;https://rebas.kr/845&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;연구소 2&lt;/a&gt;'와 거의 동일한 문제이다. 일부 값만 수정하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;연구소의 사이즈는 N이며, 바이러스의 개수는 M이다.&lt;/li&gt;&lt;li&gt;바이러스를 놓을 수 있는 칸(2)의 좌표를 별도의 리스트 V에 저장한다.&lt;/li&gt;&lt;li&gt;빈칸(0)의 개수를 모두 세고, 이를 K라 하자. 이 값은 바이러스를 퍼트려야 하는 칸의 총개수가 된다.&lt;/li&gt;&lt;li&gt;조합(combination) 방법을 통해 M개의 바이러스를 바이러스 칸(2)에 둔다. 이때 리스트 V를 활용하여, 바이러스를 놓은 칸의 좌표를 큐에 모두 집어넣는다.&lt;/li&gt;&lt;li&gt;BFS를 통해 바이러스를 감염시킨다. 감염시킬 때마다, 감염시킨 칸의 개수를 세고, 감염 시간을 계속 업데이트한다.&lt;/li&gt;&lt;li&gt;감염시킬 수 있는 곳은 빈칸(0)만 가능하지만, 바이러스를 놓을 수 있는 칸(2)으로도 이동할 수 있다.&lt;/li&gt;&lt;li&gt;BFS가 종료된 후, 감염시킨 칸의 개수와 K가 같은지 비교한다. 만약 비교한 값이 같고, 걸린 시간이 최솟값이라면, 정답으로 업데이트한다.&lt;/li&gt;&lt;li&gt;모든 경우의 수에 대해서 BFS를 수행한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;vector&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; virus {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;], dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m, k, ans&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1e9&lt;/span&gt;;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; select[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;];
vector&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;virus&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; v;
queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;virus&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; , &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; infect&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;, times&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y; q.pop(); 
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) {
                    infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                    times &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[nx][ny];
                }
                q.push({nx, ny});
            }
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; times) ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; times;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; idx, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; cnt, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d) {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; m) {
        memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;d; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (select[i]) {
                q.push({v[i].x, v[i].y});
                dist[v[i].x][v[i].y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
            }
        }
        bfs();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;idx; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;d; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;select[i]) {
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
            solve(i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, d);
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;) v.push_back({i, j});
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
        }
    }
    solve(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, v.size());
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e9&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;?&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;:&lt;/span&gt; ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque

n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
select &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;
q, v, k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque(), [], &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;(dist):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;global&lt;/span&gt; ans
    infect, times &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; dx, dy &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
                    infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
                    times &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[nx][ny]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k:
        ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(ans, times)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(idx, cnt, d):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; m:
        dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;n &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(d):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; select[i]:
                x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; v[i]
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((x, y))
                dist[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
        bfs(dist)
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(idx, d):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; select[i]:
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
            solve(i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, d)
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;:
            v&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((i, j))
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
            k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt;
solve(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;len&lt;/span&gt;(v))
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17142&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17142&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/845&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;연구소 2&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/846</guid>
      <comments>https://rebas.tistory.com/846#entry846comment</comments>
      <pubDate>Sun, 14 Apr 2019 23:52:16 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17141 &amp;middot; 연구소 2</title>
      <link>https://rebas.tistory.com/845</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9920BE445CB3442534&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9920BE445CB3442534&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS, 브루트 포스&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;연구소에 바이러스를 퍼트리는 데 걸리는 최소 시간을 구하는 문제다. 바이러스를 놓을 수 있는 경우를 모두 만든 후, 각 케이스에 대해 BFS로&amp;nbsp;바이러스를 퍼트려서 최솟값을 구하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;연구소의 사이즈는 N이며, 바이러스의 개수는 M이다.&lt;/li&gt;&lt;li&gt;바이러스를 놓을 수 있는 칸(2)의 좌표를 별도의 리스트 V에 저장한다.&lt;/li&gt;&lt;li&gt;빈칸(0)의 개수를 모두 세고, 여기에 바이러스를&amp;nbsp;놓을 수 있는 칸(2)의 개수를 더한 후, M을 뺀다. 이 값은 바이러스를 퍼트려야 하는 칸의 총개수가 된다. 이를 K라 하자.&lt;/li&gt;&lt;li&gt;조합(combination) 방법을 통해 M개의 바이러스를 바이러스 칸(2)에 둔다. 이때 리스트 V를 활용하여, 바이러스를 놓은&amp;nbsp;칸의&amp;nbsp;좌표를 큐에 모두 집어넣는다.&lt;/li&gt;&lt;li&gt;BFS를 통해 바이러스를 감염시킨다. 감염시킬 때마다, 감염시킨 칸의 개수를 세고, 감염 시간을 계속 업데이트한다.&lt;/li&gt;&lt;li&gt;감염시킬 수 있는 곳은 벽(1)을 제외한 모든 곳이다.&lt;/li&gt;&lt;li&gt;BFS가 종료된 후, 감염시킨 칸의 개수와 K가 같은지 비교한다.&amp;nbsp;만약 비교한 값이 같고,&amp;nbsp;걸린 시간이 최솟값이라면,&amp;nbsp;정답으로&amp;nbsp;업데이트한다.&lt;/li&gt;&lt;li&gt;모든 경우의 수에 대해서 BFS를 수행한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;vector&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; virus {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;], dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;50&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m, k, ans&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1e9&lt;/span&gt;;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; select[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;];
vector&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;virus&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; v;
queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;virus&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; , &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; infect&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;, times&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y; q.pop();
        times &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y];
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
                q.push({nx, ny});
                infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
            }
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; times) ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; times;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; idx, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; cnt, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d) {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; m) {
        memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;d; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (select[i]) {
                q.push({v[i].x, v[i].y});
                dist[v[i].x][v[i].y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
            }
        }
        bfs();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;idx; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;d; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;select[i]) {
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
            solve(i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, d);
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;) v.push_back({i, j});
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
        }
    }
    k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;v.size()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;m;
    solve(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, v.size());
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e9&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;?&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;:&lt;/span&gt; ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque

n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
select &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;
q, v, k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque(), [], &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;(dist):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;global&lt;/span&gt; ans
    infect, times &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        times &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; dx, dy &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; infect &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k:
        ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(ans, times)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(idx, cnt, d):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; m:
        dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;n &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(d):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; select[i]:
                x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; v[i]
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((x, y))
                dist[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
        bfs(dist)
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(idx, d):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; select[i]:
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
            solve(i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, d)
            select[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;:
            v&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((i, j))
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
            k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt;
k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;len&lt;/span&gt;(v)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;m
solve(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;len&lt;/span&gt;(v))
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17141&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17141&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/846&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;연구소 3&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/845</guid>
      <comments>https://rebas.tistory.com/845#entry845comment</comments>
      <pubDate>Sun, 14 Apr 2019 23:46:15 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17140 &amp;middot; 이차원 배열과 연산</title>
      <link>https://rebas.tistory.com/844</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/999F34445CB3401F35&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F999F34445CB3401F35&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 시뮬레이션&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;문제에 주어진 조건을 그대로 구현하는 문제다. 여러 개의 변수를&amp;nbsp;가진 구조체(또는 벡터)를&amp;nbsp;정렬해야&amp;nbsp;한다. 연산자 오버로딩을 통해 우선순위를 구현하거나, STL pair를 사용하면 된다.&amp;nbsp;정렬 대신, 우선순위 큐를 사용할 수 있다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;행의 최대 사이즈를 N, 열의 최대 사이즈를 M이라고 하자.&lt;/li&gt;&lt;li&gt;이 문제는 N &amp;gt;= M일 때 각 행을&amp;nbsp;정렬하고, N &amp;lt; M일 때 각 열을&amp;nbsp;정렬한다.&lt;/li&gt;&lt;li&gt;정렬의 우선순위는 1) 수의 등장 횟수, 2) 해당 숫자이며, 오름차순으로 정렬한다.&lt;/li&gt;&lt;li&gt;수의 등장 횟수는 별도의 카운터 배열을 만들어서 해당 숫자가 얼마나 등장하는지 센다.&lt;/li&gt;&lt;li&gt;배열에 값이 들어갈 때는 숫자가 먼저 들어가며, 그다음으로 수의 등장 횟수가 들어간다.&lt;/li&gt;&lt;li&gt;정렬 연산이 수행되면, 행 또는 열이 커질 수 있다. 커질 때마다 N 또는 M 값을 최대치로 업데이트한다.&lt;/li&gt;&lt;li&gt;행 연산과 열 연산은 동작이 거의 비슷하다.&amp;nbsp;그대로 복사 붙여넣기 한 후, 범위와 인덱스만 적절히 수정하면 된다.&lt;/li&gt;&lt;li&gt;정렬을 수행하다가 A[R][C] == K를 발견하면 현재 시간을 출력하고 종료한다.&lt;/li&gt;&lt;li&gt;수행 시간이 100을 초과하면,&amp;nbsp;-1을 출력하고 종료한다.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;utility&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; r, c, k, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=3&lt;/span&gt;, m&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=3&lt;/span&gt;;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; cnt[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;], a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;101&lt;/span&gt;; t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[r][c] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k) {
            printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, t);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                priority_queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;pair&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; pq;
                memset(cnt, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(cnt));
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j]) {
                        cnt[a[i][j]] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                        a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
                    }
                }
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=100&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (cnt[j]) pq.push(make_pair(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;cnt[j], &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j));
                }
                &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; len &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; pq.size()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;;
                m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(m, len);
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;len; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=2&lt;/span&gt;) {
                    a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;pq.top().second;
                    a[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;pq.top().first;
                    pq.pop();
                }
            }
        } &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;m; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                priority_queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;pair&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; pq;
                memset(cnt, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(cnt));
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[j][i]) {
                        cnt[a[j][i]] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                        a[j][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
                    }
                }
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=100&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (cnt[j]) pq.push(make_pair(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;cnt[j], &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j));
                }
                &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; len &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; pq.size()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;;
                n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(n, len);
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;len; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=2&lt;/span&gt;) {
                    a[j][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;pq.top().second;
                    a[j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;pq.top().first;
                    pq.pop();
                }
            }
        }
        
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;-1&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;r, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;c, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;k);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=3&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
        }
    }
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ C++ 코드에서는 우선순위 큐를 사용하여 정렬된 상태를 만들었다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;r, c, k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*101&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;)]
n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;global&lt;/span&gt; n, m
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; t &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[r][c] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k:
            &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(t)
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
                cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*101&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, m&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j]:
                        cnt[a[i][j]] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
                        a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
                b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; []
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;):
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; cnt[j]:
                        b&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((cnt[j], j))
                b&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;sort()
                m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(m, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;len&lt;/span&gt;(b)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;)
                j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; x &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; b:
                    a[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;], a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x
                    j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, m&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
                cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*101&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[j][i]:
                        cnt[a[j][i]] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
                        a[j][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
                b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; []
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;):
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; cnt[j]:
                        b&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((cnt[j], j))
                b&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;sort()
                n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(n, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;len&lt;/span&gt;(b)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;)
                j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; x &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; b:
                    a[j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][i], a[j][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x
                    j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
    a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;], a[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
solve()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ Python 코드에서는 일반적인 정렬을 사용하였다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17140&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17140&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/844</guid>
      <comments>https://rebas.tistory.com/844#entry844comment</comments>
      <pubDate>Sun, 14 Apr 2019 23:29:07 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17069 &amp;middot; 파이프 옮기기 2</title>
      <link>https://rebas.tistory.com/838</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9963BA3A5CAB163831&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9963BA3A5CAB163831&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;다운로드.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;파이프를 옮겨서 한쪽 끝을 (N, N)으로 이동시키는 방법의 개수를 구해야 한다. BOJ 17070번 '&lt;a href=&quot;https://rebas.kr/837&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;파이프 옮기기 1&lt;/a&gt;'이&amp;nbsp;업그레이드된 문제이다. N의 범위가 최대 32로 증가했다. 하지만 시간제한은 0.5초로 줄어들었으므로, 백트래킹으로 구현할 수 없다. 때문에 이 문제는 DP를 이용하여 구현해야 한다. DP로 구현하면 1번 문제도 해결할 수 있다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;파이프의 상태는 3가지로 나눌 수 있다.&amp;nbsp;&lt;/li&gt;&lt;li&gt;1. 가로로 놓여있는 상태 (0) : 가로(0) → 가로(0) 또는 대각선(2)&lt;/li&gt;&lt;li&gt;2. 세로로 놓여있는 상태 (1) : 세로(1) → 세로(1) 또는 대각선(2)&lt;/li&gt;&lt;li&gt;3. 대각선으로 놓여있는 상태 (2) : 대각선(2) → 가로(0) 또는 세로(1) 또는 대각선(2)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 816px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/996021345CAB113729&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F996021345CAB113729&quot; width=&quot;816&quot; height=&quot;220&quot; filename=&quot;캡처2.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[M][X][Y] : M번 파이프가 (X, Y)에 도착하는 방법의 수 (M은 파이프의 상태 0~2)&lt;/li&gt;&lt;li&gt;D[0][X][Y] : 가로 파이프가 (X, Y)에 도착하는 방법의 수&lt;/li&gt;&lt;li&gt;D[1][X][Y] : 세로 파이프가 (X, Y)에 도착하는 방법의 수&lt;/li&gt;&lt;li&gt;D[2][X][Y] : 대각선 파이프가 (X, Y)에 도착하는 방법의 수&lt;/li&gt;&lt;li&gt;D[0][1][2] = 1 : 가로 파이프 시작 위치 (1, 2)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 661px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99783B335CAB114E14&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99783B335CAB114E14&quot; width=&quot;661&quot; height=&quot;460&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;34&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;34&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;34&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;34&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;p[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]) d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;][i][j];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j]) d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;][i][j];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;p[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]) {
                d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;][i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;][i][j];
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%lld&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][n][n]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][n][n]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;][n][n]);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;p[i][j]);
        }
    }
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
p &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*3&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)]

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; p[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]:
                d[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j]:
                d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; (p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; p[i][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]):
                d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;sum&lt;/span&gt;(d[n][n]))&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ 파이썬 소스코드는 D[X][Y][M] 으로 구현했다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17069&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17069&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/837&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;파이프 옮기기 1&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/838</guid>
      <comments>https://rebas.tistory.com/838#entry838comment</comments>
      <pubDate>Wed, 10 Apr 2019 12:52:24 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16985 &amp;middot; Maaaaaaaaaze</title>
      <link>https://rebas.tistory.com/842</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9928E43E5CAC0C9D31&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9928E43E5CAC0C9D31&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS, 브루트 포스&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;3차원 미로를 탈출하는 것을 구현해야 한다. 단순한 3차원 BFS는, BOJ 7569번 '&lt;a href=&quot;https://rebas.kr/651&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;토마토&lt;/a&gt;'처럼 구현하면 된다. 하지만, 이 문제는 각 층의 미로가 회전하고, 각 층마다 섞여서&amp;nbsp;까다롭다. 미로를 제대로 만드는 것이 중요하다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;미로 맵을 나타낼 3차원 배열의 인덱스는 [층 번호] [X좌표] [Y좌표] 이다.&lt;/li&gt;&lt;li&gt;각 층을 섞는 것을 순열을 통해 만든다. 별도의 인덱스[0~4]를 만들어서 층을 나타내고, 이를 순열로 섞으면 된다.&lt;/li&gt;&lt;li&gt;층을 섞은 후, 각 층을 회전해야 한다. 별도의 회전 함수를 만들고, 5중 for문 또는 재귀를 통해 각 층을 순차적으로 회전시킨다.&lt;/li&gt;&lt;li&gt;첫 층의 출발 칸(0, 0, 0)이&amp;nbsp;이동할 수 있는 칸[1]인 경우에만 다음 층을 섞는다. 이동할 수 없는 칸[0]이라면 다음 층으로 이동할 수 없기 때문이다.&lt;/li&gt;&lt;li&gt;층을 순서대로 회전시키면서&amp;nbsp;마지막 층까지 회전시킨 후, 도착 칸(4, 4, 4)이 이동할 수 있는 칸[1]인지 확인한다. 이동할 수 있는 칸이라면, BFS 탐색을 시작한다.&lt;/li&gt;&lt;li&gt;BFS 탐색을 할 때마다, 최단 거리 정보를 업데이트한다.&lt;/li&gt;&lt;li&gt;만약 최단 거리가 12로&amp;nbsp;나왔다면, 이보다 더 짧을 수 없으므로, exit 함수를 통해 프로그램을 바로 종료한다. 이 부분을 처리하면 수행 시간을 대폭 줄일 수 있다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdlib&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; maze {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y, z;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;], b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;], ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e9&lt;/span&gt;;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dz[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; ,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;maze&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
    q.push({&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;});
    memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
    dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().z; q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;) {
            ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; min(ans, dist[x][y][z]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;12&lt;/span&gt;) {
                printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;12&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
                exit(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;);
            }
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;6&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i], nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; z&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dz[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (b[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                q.push({nx, ny, nz});
                dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y][z]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            }
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;rotate&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; s) {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; temp[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;];
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            temp[j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4-&lt;/span&gt;i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[s][i][j];
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            b[s][i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; temp[i][j];
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;do&lt;/span&gt; {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            memcpy(b[d[i]], a[i], &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(a[i]));
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            rotate(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                rotate(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;);
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    rotate(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;);
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; m&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; m&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; m&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                        rotate(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;);
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                            rotate(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;);
                            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;]) bfs();
                        }
                    }
                }
            }
        }
    } &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (next_permutation(d, d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+5&lt;/span&gt;));
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j][k]);
            }
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;5&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i;
    solve();
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e9&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;?&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;:&lt;/span&gt; ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;itertools&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; permutations

ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt;
dx, dy, dz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)]
b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*5&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)]

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;global&lt;/span&gt; ans
    q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()
    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;))
    dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*5&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)]
    dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x, y, z) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt; ,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
            ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(ans ,dist[x][y][z])
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;12&lt;/span&gt;:
                &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;12&lt;/span&gt;)
                exit(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;)
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;6&lt;/span&gt;):
            nx, ny, nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i], z&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dz[i]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; b[nx][ny][nz] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny, nz))
                dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y][z]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;rotate&lt;/span&gt;(k):
    temp &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*5&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;)]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;):
            temp[j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4-&lt;/span&gt;i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[k][i][j]
    b[k] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; temp

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;maze&lt;/span&gt;(cnt):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;:
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;]:
            bfs()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]:
            maze(cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)
        rotate(cnt)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; d &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; permutations([&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;]):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;5&lt;/span&gt;):
            b[d[i]] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i]
        maze(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;)

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16985&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16985&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/842</guid>
      <comments>https://rebas.tistory.com/842#entry842comment</comments>
      <pubDate>Tue, 9 Apr 2019 12:21:38 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 1149 &amp;middot; RGB거리</title>
      <link>https://rebas.tistory.com/840</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99C7DA4F5CAB336228&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99C7DA4F5CAB336228&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;RGB 거리의 각&amp;nbsp;집에&amp;nbsp;색칠하는 비용을 구해야 한다. DP로 구현하여&amp;nbsp;색칠할 때 드는 최소 비용을 구하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N][M] : N번째 집을 M으로 칠할 때 드는 최소 비용 (M은 색깔 0~2)&lt;/li&gt;&lt;li&gt;D[N][0] : N번째 집을 빨간색(0)으로 칠할 때 드는 최소 비용&lt;/li&gt;&lt;li&gt;D[N][1] : N번째 집을 초록색(1)으로 칠할 때 드는 최소 비용&lt;/li&gt;&lt;li&gt;D[N][2] : N번째 집을 파란색(2)으로 칠할 때 드는 최소 비용&lt;/li&gt;&lt;li&gt;D[1][0] = P[1][0], D[1][1] = P[1][1], D[1][2] = P[1][2]&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 569px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99D488375CAB38333C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99D488375CAB38333C&quot; width=&quot;569&quot; height=&quot;349&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i];
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; min(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;];
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; min(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;];
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; min(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;];
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, min(min(d[n][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[n][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]), d[n][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]));
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;3&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;p[i][j]);
        }
    }
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;):
        d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[n]))

n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*3&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)]
p &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*3&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;[&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
solve()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/1149&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 1149&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/840</guid>
      <comments>https://rebas.tistory.com/840#entry840comment</comments>
      <pubDate>Mon, 8 Apr 2019 21:01:39 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 1012 &amp;middot; 유기농 배추</title>
      <link>https://rebas.tistory.com/839</link>
      <description>&lt;p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/998A03445CAB211309&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F998A03445CAB211309&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : DFS&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;배추 밭에 놓아야 하는 배추흰지렁이의 마릿수를 구해야 한다. 배추흰지렁이는 하나의 배추 그룹에 한 마리만 놓으면, 최소 마릿수가 된다. 즉, 이 문제는 배추 그룹의 수를 세는 플러드 필 문제이다.&amp;nbsp;배추가&amp;nbsp;인접한 상하좌우로 연결되면, 이것은 하나의 배추 그룹이 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;우선 N*M 사이즈의 배열 A를 만들고 모든 좌표를 0으로 둔다.&lt;/li&gt;&lt;li&gt;입력으로 주어지는 배추의 좌표 (X, Y)를&amp;nbsp;배열 A에 1로 둔다.&lt;/li&gt;&lt;li&gt;N*M 사이즈의 맵을 DFS를 통해 모든 곳을 방문할 때까지 완전 탐색한다.&lt;/li&gt;&lt;li&gt;DFS로 다음 좌표를 이동할 때, 배추(1)로만 이동한다.&lt;/li&gt;&lt;li&gt;테스트케이스가 여러 개 주어지므로, 초기화가 매번 필요하다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; m, n, k, ans;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;51&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;51&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; check[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;51&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;51&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;dfs&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; y) {
    check[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[nx][ny]) {
            dfs(nx, ny);
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[i][j]) {
                dfs(i, j);
                ans&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;;
            }
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; t;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
        ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
        memset(check, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(check));
        memset(a, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(a));
        scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;k);
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;k; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;x, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;y);
            a[y][x] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
        }
        solve();
        printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans);
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;sys&lt;/span&gt;
sys&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;setrecursionlimit(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10000&lt;/span&gt;)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;dfs&lt;/span&gt;(x, y):
    check[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; dx, dy &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
        nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[nx][ny]:
            dfs(nx, ny)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[i][j]:
                dfs(i, j)
                ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())):
    m, n, k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
    a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;m &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
    check &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;m &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(k):
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
        a[y][x] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    solve()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/1012&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 1012&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/839</guid>
      <comments>https://rebas.tistory.com/839#entry839comment</comments>
      <pubDate>Mon, 8 Apr 2019 19:36:08 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17070 &amp;middot; 파이프 옮기기 1</title>
      <link>https://rebas.tistory.com/837</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9956D6465CAB01FA3A&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9956D6465CAB01FA3A&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 백트래킹&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;파이프를 옮겨서 한쪽 끝을 (N, N)으로 이동시키는 방법의 개수를 구해야 한다. 파이프는 2칸을 차지하지만, 파이프의 끝만 고려하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;파이프의 상태는 3가지로 나눌 수 있다.&amp;nbsp;&lt;/li&gt;&lt;li&gt;1. 가로로 놓여있는 상태 (0) : 가로(0) → 가로(0) 또는&amp;nbsp;대각선(2)&lt;/li&gt;&lt;li&gt;2. 세로로 놓여있는 상태 (1) : 세로(1) → 세로(1) 또는&amp;nbsp;대각선(2)&lt;/li&gt;&lt;li&gt;3. 대각선으로 놓여있는 상태 (2) : 대각선(2)&amp;nbsp;→ 가로(0) 또는 세로(1) 또는&amp;nbsp;대각선(2)&lt;/li&gt;&lt;li&gt;가로(0)에서 세로(1)로 곧바로 이동할 수 없고, 세로(1)에서 가로(0)로 곧바로 이동할 수 없다.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;재귀 함수의 인자는 (X 좌표, Y 좌표, 파이프 상태) 이다.&lt;/li&gt;&lt;li&gt;현재 파이프 상태에 따라서 다음 이동의 파이프 상태를 위의 3가지 조건에 따라 구현한다.&lt;/li&gt;&lt;li&gt;또한, 다음 이동 좌표가 벽(1)인지 확인하고, 벽이라면 이동하지 않도록 한다. 빈칸(0)으로만 이동할 수 있다.&lt;/li&gt;&lt;li&gt;마지막 좌표에 도착하면 정답에 +1 카운트한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, ans;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;16&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;16&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; y, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; z) {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
        ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; a[nx][ny]) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (i &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; (a[x][y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; a[x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][y])) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
        solve(nx, ny, i);
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
        }
    }
    solve(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;);
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;PyPy3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;dx, dy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;)
n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(x, y, z):
    res &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
        nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; a[nx][ny]:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; (a[x][y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; a[x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][y]):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
        res &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; solve(nx, ny, i)
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; res

&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(solve(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;))&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;✓ Python 3로 제출하면 시간 초과가 나오므로, PyPy3로 제출해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17070&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17070&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/837</guid>
      <comments>https://rebas.tistory.com/837#entry837comment</comments>
      <pubDate>Mon, 8 Apr 2019 17:26:34 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 2156 &amp;middot; 포도주 시식</title>
      <link>https://rebas.tistory.com/836</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99046E4C5CA9FE4717&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99046E4C5CA9FE4717&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;N개의 포도주 잔이 있을 때, 최대로 마실 수 있는 포도주의 양을 구해야 한다. DP를 이용하여 포도주 마시는 규칙에 따라 구현하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;포도주는 연속으로 3잔 이상 마실 수 없다. 즉, 연속하여 마실 수 있는 포도주는 최대 2잔이다.&lt;/li&gt;&lt;li&gt;D[N] : N 번째 잔의 포도주를 마셨을 때, 최대로 마실 수 있는 포도주의 양&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N]은&amp;nbsp;3가지 경우의 수로&amp;nbsp;나눌 수 있으며, 3가지 값 중 가장 큰 값이 D[N]이 된다.&lt;/li&gt;&lt;li&gt;1. N 번째 잔을&amp;nbsp;마시지 않은 경우 : D[N-1]&lt;/li&gt;&lt;li&gt;2. N 번째 잔이&amp;nbsp;연속 1잔째 마신 경우 : D[N-2] + P[N] (N-1&amp;nbsp;번째 잔은 마시지 않아야 한다.)&lt;/li&gt;&lt;li&gt;3. N 번째 잔이 연속 2잔째 마신 경우 : D[N-3] + P[N-1] + P[N-1] (N-2 번째 잔은 마시지 않아야 한다.)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 540px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/999BB2385CAA011B0D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F999BB2385CAA011B0D&quot; width=&quot;540&quot; height=&quot;143&quot; filename=&quot;캡처2.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10001&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;];
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;];
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[i]);
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[i]);
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[n]);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;p[i]);
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[i])
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[i])

n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+2&lt;/span&gt;)
p &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;[&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]
solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/2156&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 2156&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/836</guid>
      <comments>https://rebas.tistory.com/836#entry836comment</comments>
      <pubDate>Sun, 7 Apr 2019 22:54:22 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 17135 &amp;middot; 캐슬 디펜스</title>
      <link>https://rebas.tistory.com/835</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/995D53455CA9E8BD16&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F995D53455CA9E8BD16&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 브루트 포스, 시뮬레이션&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;궁수를&amp;nbsp;성에 적절히 배치하여 적을 최대한 많이 죽이도록&amp;nbsp;구현하는 문제다. 가능한 궁수의 배치를 모두 만들고, 각 배치에서 적을 얼마나 쏴 죽일 수 있는지 카운트하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;N*M 사이즈의 맵에서 궁수는 N+1번째 줄에 3명이 배치된다.&lt;/li&gt;&lt;li&gt;궁수가 서로 겹치지 않도록,&amp;nbsp;3중 for문 또는 재귀를 통해 각각 배치한다. 조합 방식으로 구현하면 된다.&lt;/li&gt;&lt;li&gt;그리고 배치한 위치의 인덱스를 별도로 저장한다. 아래의 코드에서는 archer 배열에 Y좌표(열 인덱스)가 저장되어 있다.&lt;/li&gt;&lt;li&gt;각 배치에서 궁수로부터 적이 얼마나 떨어져 있는지 계산하고, 각 적을 죽여야 한다.&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;1. 궁수의 위치를 (N, K)라고 하고, 적의 위치를 (i, j)라고 하자. 단, (0≤i＜N, 0≤j＜M) 이다.&lt;/li&gt;&lt;li&gt;2. 궁수와 적 사이의 거리는 dist&amp;nbsp;=&amp;nbsp;|N-i|&amp;nbsp;+ |K-j| 가 된다.&lt;/li&gt;&lt;li&gt;3. dist가&amp;nbsp;입력으로 주어진 D 이하라면, 죽일 수 있는 적의 후보가 된다. 이 적의 위치를 우선순위 큐(Min Heap)에 집어넣는다. 우선순위 큐는 거리(dist), Y좌표(j), X좌표(i) 순으로 우선순위를 가진다.&lt;/li&gt;&lt;li&gt;4. 적을 모두 찾은 후, 우선순위 큐에 있는 첫 번째 값을 꺼낸다. 이 값은 가장 먼저 죽여야 하는 적의 위치이다. 이 위치를 별도의 리스트 V에 저장한다.&lt;/li&gt;&lt;li&gt;세 명의 궁수에 대해서 1~4과정을 진행하고, 리스트 V에서 값을 꺼낸다. 이 리스트에는 중복된 좌표가&amp;nbsp;존재할 수 있다. 해당 좌표에 있는 적을 모두 죽인다.&lt;/li&gt;&lt;li&gt;한 턴이 종료되었으므로, 적을 한 칸씩 모두 내린다. 적은&amp;nbsp;최대 N개의 줄에 존재하므로, N번만 내리면 모두 사라지게 된다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cmath&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;vector&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; castle {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y, z;
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; castle &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; t.z) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; t.y;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; t.z;
    }
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m, d, ans;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;15&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;15&lt;/span&gt;], archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;kill&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;15&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;15&lt;/span&gt;], cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;, t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;n;
    memcpy(b, a, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(a));
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Find enemies.&lt;/span&gt;
        vector&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;castle&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; v;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;3&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            priority_queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;castle&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (b[i][j]) {
                        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; abs(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;i)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;abs(archer[k]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j);
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; d) q.push({i, j, dist});
                    }
                }
            }
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (q.size()) {
                &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.top().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.top().y;
                v.push_back({x, y});
            }
        }
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Kill enemies.&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;)v.size(); i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; v[i].x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; v[i].y;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (b[x][y]) {
                b[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
                cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
            }
        }
        &lt;span style=&quot;color: rgb(64, 128, 128); font-style: italic;&quot;&gt;// Enemies move to down.&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                b[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[i][j];
            }
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; cnt) ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; cnt;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i, archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; j, archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; k;
                kill();
            }
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;d);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
        }
    }
    solve();
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;heapq&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; heappush, heappop
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;copy&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deepcopy

n, m, d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
archer &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*3&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;kill&lt;/span&gt;(b):
    cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
        v &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; []
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; k &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;):
            q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; []
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; b[i][j]:
                        dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;abs&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;i)&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;abs&lt;/span&gt;(archer[k]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j)
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; d:
                            heappush(q, (dist, j, i))
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; q:
                _, y, x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; heappop(q)
                v&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((x, y))
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; x, y &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; v:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; b[x][y]:
                b[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
                cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
                b[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; b[i][j]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
            b[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; cnt

ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, m):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; k &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, m):
            archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], archer[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i, j, k
            b &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deepcopy(a)
            ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(ans, kill(b))
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/17135&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 17135&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/835</guid>
      <comments>https://rebas.tistory.com/835#entry835comment</comments>
      <pubDate>Sun, 7 Apr 2019 21:36:01 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 9465 &amp;middot; 스티커</title>
      <link>https://rebas.tistory.com/834</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9926C0435CA9BD1E19&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9926C0435CA9BD1E19&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;스티커를 떼어내어 얻을 수 있는 최대 점수를 DP를 이용하여 구해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N][M] : N번째 열에서 스티커를 M했을 때, 2xN 개의 스티커에서 얻을 수 있는 최대 점수&lt;/li&gt;&lt;li&gt;M = 0 (위쪽 스티커를 뜯음), M = 1 (아래쪽 스티커를 뜯음), M = 2 (둘 다 뜯지 않음)&lt;/li&gt;&lt;li&gt;D[1][0] = P[1][0], D[1][1] = P[1][1], D[1][2] = 0&lt;/li&gt;&lt;li&gt;D[N][0] : N번째 열에서 위쪽 스티커를 뜯어서 얻은&amp;nbsp;최대 점수&lt;/li&gt;&lt;li&gt;D[N][1] : N번째 열에서 아래쪽 스티커를 뜯어서 얻은 최대 점수&lt;/li&gt;&lt;li&gt;D[N][2] : N번째 열에서 둘 다 뜯지 않고 얻은 최대 점수&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 613px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99B0313B5CA9FAA231&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99B0313B5CA9FAA231&quot; width=&quot;613&quot; height=&quot;325&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100001&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100001&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;];
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;];
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(max(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]), d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]);
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, max(max(d[n][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[n][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]), d[n][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]));
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; t;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
        scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;p[j][i]);
            }
        }
        solve();
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][i]
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i]
        d[i][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;])

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())):
    n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
    p &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;)]
    d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*3&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)]
    solve()
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[n]))&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/9465&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 9465&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/834</guid>
      <comments>https://rebas.tistory.com/834#entry834comment</comments>
      <pubDate>Sun, 7 Apr 2019 18:09:08 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 2193 &amp;middot; 이친수</title>
      <link>https://rebas.tistory.com/833</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/998DCC495CA9ADB109&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F998DCC495CA9ADB109&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;N자리 이친수를 DP를 통해 만들어야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : N자리 이친수의 개수&lt;/li&gt;&lt;li&gt;D[1] = 1, D[2] = 1&lt;/li&gt;&lt;li&gt;D[N] = 0으로 끝나는 N-1자리 이친수 + 01로 끝나는 N-2자리 이친수&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 345px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9962FF3A5CA9AFAB13&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9962FF3A5CA9AFAB13&quot; width=&quot;345&quot; height=&quot;80&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;91&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;];
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%lld&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[n]);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/2193&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 2193&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/833</guid>
      <comments>https://rebas.tistory.com/833#entry833comment</comments>
      <pubDate>Sun, 7 Apr 2019 17:06:43 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 11057 &amp;middot; 오르막 수</title>
      <link>https://rebas.tistory.com/832</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99141A505CA8785110&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99141A505CA8785110&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;오르막 수를 DP로 구현하는 문제다. 오르막 수란 각 자리의 수가&amp;nbsp;오름차순을 이루는 숫자를 말한다. 0으로 시작할 수 있으며, 인접한 자리의 숫자가 같아도 오르막 수이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N][M] : M으로 끝나는 N자리 오르막 수&lt;/li&gt;&lt;li&gt;D[1][0] = 1, D[1][1] = 1,&amp;nbsp;‥‥ , D[1][9] = 1&lt;/li&gt;&lt;li&gt;D[N][0] = D[N-1][0] : 0으로 끝나는 N자리 오르막 수&lt;/li&gt;&lt;li&gt;D[N][1] = D[N-1][0] + D[N-1][1]&lt;/li&gt;&lt;li&gt;D[N][2] = D[N-1][0] + D[N-1][1] + D[N-1][2]&lt;/li&gt;&lt;li&gt;‥‥&lt;/li&gt;&lt;li&gt;D[N][9] = D[N-1][0] + D[N-1][1] + ‥‥ + D[N-1][9]&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 313px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9972F8335CA87AE11F&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9972F8335CA87AE11F&quot; width=&quot;313&quot; height=&quot;170&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, ans;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; mod &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10007&lt;/span&gt;;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=9&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=9&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;j; k&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
                d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][k];
            }
            d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; mod;
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=9&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[n][i];
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;mod);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*10&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)]
mod &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10007&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;):
        d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; k &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
                d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][k]
            d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; mod

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;sum&lt;/span&gt;(d[n])&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;mod)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/11057&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 11057&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/832</guid>
      <comments>https://rebas.tistory.com/832#entry832comment</comments>
      <pubDate>Sat, 6 Apr 2019 19:09:20 +0900</pubDate>
    </item>
    <item>
      <title>삼성 SW 역량 테스트 문제 리스트</title>
      <link>https://rebas.tistory.com/789</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9939A0485C823B1430&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9939A0485C823B1430&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;다운로드.jpg&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;+ 삼성 소프트웨어&amp;nbsp;역량 테스트란?&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;&lt;a href=&quot;https://www.samsungcareers.com/main.html&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;삼성&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;(삼성전자, 삼성 SDS 등)&amp;nbsp;채용 단계에서 S직군이 보는 오프라인 코딩 테스트(직무적성검사)이다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;SW 개발 직무에 지원한다면, GSAT 대신 소프트웨어&amp;nbsp;역량테스트를 봐야 한다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;+ SW 역량테스트 준비&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;프로그래밍 언어를 익힌다. C/C++, Java, Python3 중 (C++ 추천)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;자료구조(큐, 스택, 덱, 그래프 등)와 알고리즘 이론(DFS, BFS, 힙, 백트래킹 등)을 배운다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;알고리즘 스킬(재귀 구현, 비트 연산, STL 사용법 등)을 익힌다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;백준 온라인 저지&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;와 &lt;/span&gt;&lt;a href=&quot;https://swexpertacademy.com/&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;SW Expert Academy&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;에서 알고리즘 문제를 풀면서 연습한다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;역대 기출은 시뮬레이션, BFS, DFS, 브루트 포스 등의 문제로 출제되었으므로, 이러한 유형을 집중적으로 푼다.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;SWEA에서 &lt;a href=&quot;https://www.swexpertacademy.com/main/userpage/code/userProblemBoxDetail.do?probBoxId=AV5Po0AqAPwDFAUq&amp;amp;leftPage=1&amp;amp;curPage=userpage&amp;amp;userId=SWEAC&amp;amp;&amp;amp;&amp;amp;&amp;amp;&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;모의 역량 SW 테스트 문제&lt;/a&gt;를 풀어본다.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;+ 필수 문제&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ol style=&quot;list-style-type: decimal;&quot;&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/13460&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;구슬 탈출 2&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/725&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/12100&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;2048 (Easy)&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/763&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 &lt;/span&gt;&lt;/a&gt;&lt;a href=&quot;https://rebas.kr/763&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/3190&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;뱀&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/785&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/13458&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;시험 감독&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/783&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14499&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;주사위 굴리기&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/784&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14500&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;테트로미노&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/792&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14501&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;퇴사&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/692&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14502&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;연구소&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/782&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14503&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;로봇 청소기&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/743&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14888&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;연산자 끼워넣기&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/693&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14889&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;스타트와 링크&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/754&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14890&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;경사로&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/788&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/14891&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;톱니바퀴&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/786&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15683&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;감시&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/732&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15684&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;사다리 조작&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/790&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15685&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;드래곤 커브&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/793&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/15686&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;치킨 배달&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/791&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/5373&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;큐빙&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/794&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16234&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;인구 이동&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/715&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16235&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;나무 재테크&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://rebas.kr/713&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16236&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;아기 상어&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;&lt;a href=&quot;https://rebas.kr/714&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17144&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;미세먼지 안녕!&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;a href=&quot;https://rebas.kr/848&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;정답 및 해설&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17143&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;낚시왕&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;a href=&quot;https://rebas.kr/847&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;정답 및 해설&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17140&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;이차원 배열과 연산&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/844&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17142&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;연구소 3&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;a href=&quot;https://rebas.kr/846&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-size: 14pt;&quot;&gt;+ 추천 문제&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ol style=&quot;list-style-type: decimal;&quot;&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17070&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;파이프 옮기기 1&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/837&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17135&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;캐슬 디펜스&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/835&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16985&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;Maaaaaaaaaze&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/842&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/11559&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;Puyo Puyo&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/731&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16197&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;두 동전&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/709&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/3055&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;탈출&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/661&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2529&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;부등호&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/755&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/3987&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;보이저 1호&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/734&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2174&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;로봇 시뮬레이션&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/739&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16946&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;벽 부수고 이동하기 4&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/800&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2151&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;거울 설치&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/781&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/1938&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;통나무 옮기기&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/774&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/6087&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;레이저 통신&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/758&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/9019&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;DSLR&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/764&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2468&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;안전 영역&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/773&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/1726&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;로봇&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/742&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/9328&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;열쇠&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/660&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/1525&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;퍼즐&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/768&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2638&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;/a&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/2638&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;치즈&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/771&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/1194&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;달이 차오른다, 가자.&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;&lt;a href=&quot;https://rebas.kr/738&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/1600&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;말이 되고픈 원숭이&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;span style=&quot;font-size:12pt;&quot;&gt;&lt;a href=&quot;https://rebas.kr/741&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16509&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;장군&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / &lt;/span&gt;&lt;a href=&quot;https://rebas.kr/823&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt;정답 및 해설&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17136&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;색종이 붙이기&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / 업데이트 예정&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16637&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;괄호 추가하기&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / 업데이트 예정&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/16918&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;&lt;span style=&quot;color: rgb(0, 85, 255); font-size: 12pt;&quot;&gt;봄버맨&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 12pt;&quot;&gt; / 업데이트 예정&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>삼성</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/789</guid>
      <comments>https://rebas.tistory.com/789#entry789comment</comments>
      <pubDate>Sat, 6 Apr 2019 17:39:46 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16194 &amp;middot; 카드 구매하기 2</title>
      <link>https://rebas.tistory.com/831</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9978D9355CA852B317&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9978D9355CA852B317&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;DP를 통해 카드를 구매하기 위해 지불하는 금액의 최솟값을 구해야 한다. BOJ 11052번 '&lt;a href=&quot;https://rebas.kr/830&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;카드 구매하기&lt;/a&gt;'에서 최댓값 대신 최솟값을 구하면 된다.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : 카드 N장을 구입하기 위한, 지불 금액의 최솟값&lt;/li&gt;&lt;li&gt;D[0] = 0, D[1] = P[1]&lt;/li&gt;&lt;li&gt;D[N-1] + P[1] : 카드 1개 들어있는 카드팩을 구매하고, 카드 N-1장을 구매한 지불 금액의 최솟값&lt;/li&gt;&lt;li&gt;D[N-2] + P[2]&lt;/li&gt;&lt;li&gt;‥‥&lt;/li&gt;&lt;li&gt;D[0] + P[N] : 카드 N개 들어있는 카드팩을 구매하고, 카드 0장을 구매한 지불 금액의 최솟값&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 375px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99E8F74D5CA853921C&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99E8F74D5CA853921C&quot; width=&quot;375&quot; height=&quot;119&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;];
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;i; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; min(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[j]);
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;p[i]);
    fill(d, d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1e9&lt;/span&gt;);
    solve();
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[n]);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**9&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)
p &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split()))

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
            d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[j])

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16194&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16194&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/830&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;카드 구매하기&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/831</guid>
      <comments>https://rebas.tistory.com/831#entry831comment</comments>
      <pubDate>Sat, 6 Apr 2019 16:21:24 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 11052 &amp;middot; 카드 구매하기</title>
      <link>https://rebas.tistory.com/830</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9977FB425CA84E6F14&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9977FB425CA84E6F14&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;DP를 통해 카드를 구매하기 위해 지불하는 금액의 최댓값을 구해야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : 카드 N장을 구입하기 위한, 지불 금액의 최댓값&lt;/li&gt;&lt;li&gt;D[0] = 0, D[1] = P[1]&lt;/li&gt;&lt;li&gt;D[N-1] + P[1] : 카드 1개 들어있는 카드팩을 구매하고, 카드 N-1장을 구매한 지불 금액의 최댓값&lt;/li&gt;&lt;li&gt;D[N-2] + P[2]&lt;/li&gt;&lt;li&gt;‥‥&lt;/li&gt;&lt;li&gt;D[0] + P[N] : 카드 N개 들어있는 카드팩을 구매하고, 카드 0장을 구매한 지불 금액의 최댓값&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 368px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9962C14E5CA8523D1D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9962C14E5CA8523D1D&quot; width=&quot;368&quot; height=&quot;106&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;], p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;];
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;i; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[j]);
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;p[i]);
    solve();
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[n]);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)
p &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split()))

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, p[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;]
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
            d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(d[i], d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;j]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;p[j])

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/11052&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 11052&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/830</guid>
      <comments>https://rebas.tistory.com/830#entry830comment</comments>
      <pubDate>Sat, 6 Apr 2019 16:15:44 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 10844 &amp;middot; 쉬운 계단 수</title>
      <link>https://rebas.tistory.com/829</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/999BB9455CA844F90D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F999BB9455CA844F90D&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;DP를 통해 계단 수의 총개수를 구하는 문제다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N][M] : M으로 끝나는 N자리 계단 수의 총개수&lt;/li&gt;&lt;li&gt;D[1][1] = 1, D[1][2] = 1, ‥‥ , D[1][9] = 1 (한 자릿수)&lt;/li&gt;&lt;li&gt;D[N][0] = D[N-1][1] : 0으로 끝나는 N자리 계단 수의 총개수&lt;/li&gt;&lt;li&gt;D[N][1] = D[N-1][0] + D[N-1][2] : 1로 끝나는 N자리 계단 수의 총개수&lt;/li&gt;&lt;li&gt;‥‥&lt;/li&gt;&lt;li&gt;D[N][8] = D[N-1][7] + D[N-1][9]&lt;/li&gt;&lt;li&gt;D[N][9] = D[N-1][8]&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 469px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/997E623B5CA8491118&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F997E623B5CA8491118&quot; width=&quot;469&quot; height=&quot;126&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; mod &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000000000&lt;/span&gt;;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; ans, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;10&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;10&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;9&lt;/span&gt;) d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;];
            d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; mod;
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;10&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[n][i];
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%lld&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;mod);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*10&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)]
ans, mod &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000000000&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;):
        d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;):
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
                d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;9&lt;/span&gt;:
                d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;]
            d[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; mod

solve()
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;sum&lt;/span&gt;(d[n])&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt;mod)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/10844&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 10844&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/829</guid>
      <comments>https://rebas.tistory.com/829#entry829comment</comments>
      <pubDate>Sat, 6 Apr 2019 15:36:29 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 15988 &amp;middot; 1, 2, 3 더하기 3</title>
      <link>https://rebas.tistory.com/828</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/993B7F355CA70A9C06&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F993B7F355CA70A9C06&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;숫자 N을 1, 2, 3의 합으로 나타내는 방법의 수를 구해야 한다. BOJ 9095번 '&lt;a href=&quot;https://rebas.kr/827&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;1, 2, 3 더하기&lt;/a&gt;'에서 범위가 확장된 문제다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : 숫자 N을 1, 2, 3의 합으로 나타내는 방법의 수&lt;/li&gt;&lt;li&gt;D[1] = 1&lt;/li&gt;&lt;li&gt;D[2] = 2 (1+1, 2)&lt;/li&gt;&lt;li&gt;D[3] = 4 (1+1+1, 1+2, 2+1, 3)&lt;/li&gt;&lt;li&gt;D[N-1] : 마지막에 1을 더한 방법의 수&lt;/li&gt;&lt;li&gt;D[N-2] : 마지막에 2를 더한 방법의 수&lt;/li&gt;&lt;li&gt;D[N-3] : 마지막에 3을 더한 방법의 수&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 471px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99AE87345CA70C410E&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99AE87345CA70C410E&quot; width=&quot;471&quot; height=&quot;90&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;long&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;1000001&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;];
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000000009&lt;/span&gt;;
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; t;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t);
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
        scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
        printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%lld&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[n]);
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;]
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000001&lt;/span&gt;):
    d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;])&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%1000000009&lt;/span&gt;)
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())):
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())])&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/15988&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 15988&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/827&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;1, 2, 3 더하기&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/828</guid>
      <comments>https://rebas.tistory.com/828#entry828comment</comments>
      <pubDate>Fri, 5 Apr 2019 17:05:04 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 9095 &amp;middot; 1, 2, 3 더하기</title>
      <link>https://rebas.tistory.com/827</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99EDFB395CA6F68324&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99EDFB395CA6F68324&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;기초적인 DP 문제이다. 숫자 N을 1, 2, 3의 합으로 나타내는 방법의 수를 구해야 한다.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : 숫자 N을 1, 2, 3의 합으로 나타내는 방법의 수&lt;/li&gt;&lt;li&gt;D[1] = 1&lt;/li&gt;&lt;li&gt;D[2] = 2 (1+1, 2)&lt;/li&gt;&lt;li&gt;D[3] = 4 (1+1+1, 1+2, 2+1, 3)&lt;/li&gt;&lt;li&gt;D[N-1] : 마지막에 1을 더한 방법의 수&lt;/li&gt;&lt;li&gt;D[N-2] : 마지막에 2를 더한 방법의 수&lt;/li&gt;&lt;li&gt;D[N-3] : 마지막에 3을 더한 방법의 수&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 471px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/998BF74F5CA6F7CA20&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F998BF74F5CA6F7CA20&quot; width=&quot;471&quot; height=&quot;90&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;11&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;() {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;11&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;];
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; t;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t);
    solve();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (t&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;--&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
        scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
        printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, d[n]);
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;]
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;11&lt;/span&gt;):
    d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append(d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;])
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())):
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())])&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/9095&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 9095&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/827</guid>
      <comments>https://rebas.tistory.com/827#entry827comment</comments>
      <pubDate>Fri, 5 Apr 2019 15:37:18 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 11727 &amp;middot; 2&amp;times;n 타일링 2</title>
      <link>https://rebas.tistory.com/826</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99B2404E5CA6F19212&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99B2404E5CA6F19212&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;기초적인 DP 문제이다. BOJ 11726번 '&lt;a href=&quot;https://rebas.kr/825&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;2×n 타일링&lt;/a&gt;'에서 살짝 변경해주면 된다.&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 853px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99D1294D5CA6F3020D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99D1294D5CA6F3020D&quot; width=&quot;853&quot; height=&quot;159&quot; filename=&quot;캡처2.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : 2xN 사이즈의 직사각형을 2x2, 2x1 사각형으로 채우는 방법의 수&lt;/li&gt;&lt;li&gt;D[0] = D[1] = 1&lt;/li&gt;&lt;li&gt;D[N-1] : 마지막 조각을 2x1 사각형으로 채운 방법의 수&lt;/li&gt;&lt;li&gt;D[N-2] : 마지막 조각을 2x2 사각형 또는 2x1 사각형 2개로 채운 방법의 수&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 391px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/996D03495CA6F3100D&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F996D03495CA6F3100D&quot; width=&quot;391&quot; height=&quot;95&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n) {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+2*&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;];
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10007&lt;/span&gt;;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; d[n];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, solve(n));
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(n):
    d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+2*&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10007&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])

solve(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()))&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/11727&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 11727&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/825&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;2×n 타일링&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/826</guid>
      <comments>https://rebas.tistory.com/826#entry826comment</comments>
      <pubDate>Fri, 5 Apr 2019 15:14:23 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 11726 &amp;middot; 2&amp;times;n 타일링</title>
      <link>https://rebas.tistory.com/825</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/991A11495CA6E80832&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F991A11495CA6E80832&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;기초적인 DP 문제이다. 그림 이해를 통해 점화식을 만들고 구현하면 된다.&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99F905425CA6EF0436&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99F905425CA6EF0436&quot; width=&quot;860&quot; height=&quot;146&quot; filename=&quot;캡처2.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : 2xN 사이즈의 직사각형을 1x2, 2x1 사각형으로 채우는 방법의 수&lt;/li&gt;&lt;li&gt;D[0] = D[1] = 1&lt;/li&gt;&lt;li&gt;D[N-1] : 마지막 조각을 2x1 사각형으로 채운 방법의 수&lt;/li&gt;&lt;li&gt;D[N-2] : 마지막 조각을 1x2 사각형 2개로 채운 방법의 수&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 342px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/991638435CA6F2A517&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F991638435CA6F2A517&quot; width=&quot;342&quot; height=&quot;72&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n) {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;];
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10007&lt;/span&gt;;
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; d[n];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, solve(n));
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(n):
    d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;]
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10007&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])

solve(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()))&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/11726&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 11726&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/825</guid>
      <comments>https://rebas.tistory.com/825#entry825comment</comments>
      <pubDate>Fri, 5 Apr 2019 14:59:46 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 1463 &amp;middot; 1로 만들기</title>
      <link>https://rebas.tistory.com/824</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/990A6D3C5CA6DACF18&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F990A6D3C5CA6DACF18&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : 다이나믹 프로그래밍&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;기초적인 DP문제이다. 3가지 조건을 따져서 점화식을 만들고, 이를 구현하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;D[N] : N을 1로 만드는 최소 연산 횟수&lt;/li&gt;&lt;li&gt;D[1] = 0&lt;/li&gt;&lt;li&gt;D[N-1] + 1 : N에서 1을 뺀 경우&lt;/li&gt;&lt;li&gt;D[N/3] + 1 : N을 3으로 나눈 경우&lt;/li&gt;&lt;li&gt;D[N/2] + 1 : N을 2로 나눈 경우&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 492px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99E1783A5CA6DE3016&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99E1783A5CA6DE3016&quot; width=&quot;492&quot; height=&quot;151&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000001&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n) {
    d[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%3&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; temp &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;/3&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; temp) d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; temp;
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%2&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==0&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; temp &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;/2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; temp) d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; temp;
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; d[n];
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n;
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, solve(n));
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;solve&lt;/span&gt;(n):
    d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;(n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;)
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;):
        d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%3&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
            temp &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;//3&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
            d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[i], temp)
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%2&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;:
            temp &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; d[i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;//2&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
            d[i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(d[i], temp)
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(d[n])

solve(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()))&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/1463&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 1463&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/824</guid>
      <comments>https://rebas.tistory.com/824#entry824comment</comments>
      <pubDate>Fri, 5 Apr 2019 13:48:37 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16509 &amp;middot; 장군</title>
      <link>https://rebas.tistory.com/823</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9932864F5CA5B2851E&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9932864F5CA5B2851E&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;장기 말을 옮기는 최소 횟수를 구하는 문제다. BFS를 통해 상(象)을 옮겨서 왕을 먹어야 한다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;장기판에서 상을 옮겨서 왕의 위치로 도달하는 최소 횟수를 구해야 한다.&lt;/li&gt;&lt;li&gt;상은 그림처럼 움직이며, 움직이는 경로에 다른 말이 있으면 해당 위치로 옮길 수 없다. 다른 말은 존재하지 않기 때문에, 왕의 위치가 상의 이동 경로에 있는지만 고려하면 된다.&lt;/li&gt;&lt;li&gt;별도로 이동 좌표를 만들고, 움직이는 경로에 왕이 있다면 상을 움직이지 않는다.&lt;/li&gt;&lt;li&gt;이동이 가능하다면 옮긴다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/996260425CA5B62428&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F996260425CA5B62428&quot; width=&quot;860&quot; height=&quot;664&quot; filename=&quot;다운로드 (1).png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; grid {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; sx, sy, ex, ey;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;9&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;8&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {
    {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;},
    {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;} };
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dy[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;8&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {
    {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;},
    {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;}, {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;} };

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;move&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;x, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;y) {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;3&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i][j], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i][j];
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;9&lt;/span&gt;) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ex &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ey) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;false&lt;/span&gt;;
    }
    x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; nx, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; ny;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;grid&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
    q.push({sx, sy});
    dist[sx][sy] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y; q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ex &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ey) {
            printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, dist[x][y]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;8&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (move(i, nx, ny) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                q.push({nx, ny});
                dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;-1&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;sx, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;sy);
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;ex, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;ey);
    bfs();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque
sx, sy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
ex, ey &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*9&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt;)]
dx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;),
      (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;)]
dy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;),
      (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-3&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;)]

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;move&lt;/span&gt;(i, x, y):
    nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x, y
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;):
        nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i][j], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i][j]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;9&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; j &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ex &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ey:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; nx, ny

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()
    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((sx, sy))
    dist[sx][sy] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ex &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ey:
            &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(dist[x][y])
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;8&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; move(i, x, y)
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                dist[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)

bfs()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16509&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16509&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/823</guid>
      <comments>https://rebas.tistory.com/823#entry823comment</comments>
      <pubDate>Thu, 4 Apr 2019 16:45:23 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16469 &amp;middot; 소년 점프</title>
      <link>https://rebas.tistory.com/822</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/9905703A5CA5926519&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F9905703A5CA5926519&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;세 명의 사람이 한곳에 모이는 최단 거리와, 그 최단 거리의 수를 구해야 하는 문제다. 주어진 3명의 좌표를 큐에 집어넣고, 각각 BFS를 돌리면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;3명의 사람을 0번, 1번, 2번이라고 하자.&lt;/li&gt;&lt;li&gt;dist 배열은 이동 거리를 저장하고, 방문 여부를 체크한다.&lt;/li&gt;&lt;li&gt;dist의 인덱스는 [X 좌표] [Y 좌표] [사람 번호] 이다. dist의 모든 값은 -1로 초기화해둔다.&lt;/li&gt;&lt;li&gt;큐에 0~2번 사람의 좌표를 모두 넣고, dist[X][Y][번호]를 0으로 초기화한다.&lt;/li&gt;&lt;li&gt;BFS를 통해 완전 탐색을 하면, dist 배열에 각 사람의 이동 거리가 업데이트되어있다.&lt;/li&gt;&lt;li&gt;dist배열을 N*M 순서로 탐색하면서 dist[X][Y][0], dist[X][Y][1], dist[X][Y][2]의 값이 모두 -1이 아닌 위치를 찾는다.&lt;/li&gt;&lt;li&gt;모두 -1이 아니라면, 3명이 만날 수 있는&amp;nbsp;장소이다.&lt;/li&gt;&lt;li&gt;이 장소 중에서 가장 최단 거리로 만날 수 있는 곳을 찾고, 같은 최단 거리가 존재한다면 수를 센다.&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 860px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99AE6F3B5CA599AC01&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99AE6F3B5CA599AC01&quot; width=&quot;860&quot; height=&quot;580&quot; filename=&quot;캡처.PNG&quot; filemime=&quot;image/jpeg&quot; style=&quot;&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; grid {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y, z;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;char&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;101&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;];
queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;grid&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().z; q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'0'&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; dist[nx][ny][z] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                q.push({nx, ny, z});
                dist[nx][ny][z] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y][z]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            }
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%s&quot;&lt;/span&gt;, a[i]);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;3&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
        scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;x, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;y);
        q.push({x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, i});
        dist[x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    }
    bfs();
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; ans&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=1e9&lt;/span&gt;, cnt&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;], y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;], z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[i][j][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;];
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; max(max(x,y),z);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (min(min(x,y),z) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; k) {
                    ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; k;
                    cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                } &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k) {
                    cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                }
            }
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (cnt) printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans, cnt);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;-1&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque
n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;strip()) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*3&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
dx, dy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)
q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'0'&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; dist[nx][ny][z] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny, z))
                dist[nx][ny][z] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y][z]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;3&lt;/span&gt;):
    x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, i))
    dist[x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][i] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
bfs()
ans, cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;e9, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
        k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;max&lt;/span&gt;(dist[i][j])
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;min&lt;/span&gt;(dist[i][j]) &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; k:
                ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; k
                cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;elif&lt;/span&gt; ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; k:
                cnt &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; cnt:
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;%d&quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; (ans, cnt))
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16469&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16469&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/822</guid>
      <comments>https://rebas.tistory.com/822#entry822comment</comments>
      <pubDate>Thu, 4 Apr 2019 14:43:39 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16441 &amp;middot; 아기돼지와 늑대</title>
      <link>https://rebas.tistory.com/821</link>
      <description>&lt;p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99A21C3B5CA56F5122&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99A21C3B5CA56F5122&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;늑대가 가지 못하는 위치를 찾는 문제다. 갈 수 있는 곳을 모두 탐색해야 하므로, BFS를 사용하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;늑대(W)는 여러 마리가 주어질 수 있고, 각 늑대는 맵에 갈 수 있는 모든 곳을 가야 한다.&amp;nbsp;&lt;/li&gt;&lt;li&gt;늑대의 이동을 크게 2가지로 구분해서 처리해야 한다.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;1. 다음 이동 위치가 초원('.')인 경우&lt;/li&gt;&lt;li&gt;초원에서는 인접한 상하좌우로 한 칸씩 움직인다.&lt;/li&gt;&lt;li&gt;움직인 후&amp;nbsp;방문 체크를 하고, 큐에 좌표를 집어넣는다.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;2. 다음 이동 위치가 초원('.')이 아닌 경우 : 산('#'), 빙판('+')&lt;/li&gt;&lt;li&gt;while 반복문을 통해 늑대를 이동시켜야 한다.&lt;/li&gt;&lt;li&gt;1) 이동한 곳이 산이라면, 위치를 되돌리고 반복문을 빠져나온다.&lt;/li&gt;&lt;li&gt;2) 산이 아니라면 빙판이므로, 다음 좌표로 한 칸 앞으로 간다.&lt;/li&gt;&lt;li&gt;3) 다음 이동 좌표가 초원이라면, 반복문을 빠져나온다. 아니라면 1) ~ 2) 과정을 산에 부딪히거나, 초원에 도착할 때까지 반복한다.&lt;/li&gt;&lt;li&gt;반복문을 빠져나온 후, 현재 좌표를 방문 체크하고 큐에 집어넣는다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; wolf {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;char&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; check[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100&lt;/span&gt;];
queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;wolf&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y; q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'.'&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[nx][ny]) {
                q.push({nx, ny});
                check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
            } &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; {
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'.'&lt;/span&gt;) {
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'#'&lt;/span&gt;) {
                        nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; dy[i];
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;;
                    }
                    nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; dy[i];
                }
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[nx][ny]) {
                    q.push({nx, ny});
                    check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
                }
            }
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot; %1c&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'W'&lt;/span&gt;) {
                q.push({i, j});
                check[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
            }
        }
    }
    bfs();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'.'&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[i][j]) printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;P&quot;&lt;/span&gt;);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%c&quot;&lt;/span&gt;, a[i][j]);
        }
        printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque
n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;strip()) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
check &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;m &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
dx, dy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)
q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'.'&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[nx][ny]:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'.'&lt;/span&gt;:
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'#'&lt;/span&gt;:
                        nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; nx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;dx[i], ny&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-&lt;/span&gt;dy[i]
                        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;
                    nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; nx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i]
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[nx][ny]:
                    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                    check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'W'&lt;/span&gt;:
            q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((i, j))
            check[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
bfs()
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;'.'&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[i][j]:
            &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;P&quot;&lt;/span&gt;, end&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;''&lt;/span&gt;)
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(a[i][j], end&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;''&lt;/span&gt;)
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16441&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16441&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/821</guid>
      <comments>https://rebas.tistory.com/821#entry821comment</comments>
      <pubDate>Thu, 4 Apr 2019 12:34:19 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16397 &amp;middot; 탈출</title>
      <link>https://rebas.tistory.com/820</link>
      <description>&lt;p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99A32A495CA4D7CC07&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99A32A495CA4D7CC07&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;숫자 N을 숫자 G로&amp;nbsp;만드는 최소 버튼 횟수를 구하는 문제다. BFS를 통해 최소 횟수를 구하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;숫자 N을 큐에 집어넣는다.&lt;/li&gt;&lt;li&gt;현재 숫자를 X라고 할 때, 다음 숫자는 X+1 또는 X*2 - 10^(X*2의 자릿수-1)가 된다.&lt;/li&gt;&lt;li&gt;최대 T번까지 BFS로 숫자를 탐색한다.&lt;/li&gt;&lt;li&gt;숫자 G에 도달하면 값을 출력한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;string&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, t, g;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100000&lt;/span&gt;];

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
    q.push({n});
    dist[n] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front(); q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (dist[x] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; t) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; g) {
            printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, dist[x]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
        }
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;, x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;};
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dx[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;99999&lt;/span&gt;) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (i &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; nx) {
                string s &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; to_string(nx);
                s[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
                nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; stoi(s);
            }
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (dist[nx] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                q.push(nx);
                dist[nx] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;ANG&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;t, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;g);
    bfs();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()
    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append(n)
    dist[n] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; dist[x] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; t:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; g:
            &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(dist[x])
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
        dx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [(x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;), (x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*2&lt;/span&gt;)]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;):
            nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dx[i]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;99999&lt;/span&gt;:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; x:
                nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;10**&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;len&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;str&lt;/span&gt;(nx))&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; dist[nx] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append(nx)
                dist[nx] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;ANG&quot;&lt;/span&gt;)

n, t, g &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*100000&lt;/span&gt;
bfs()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16397&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16397&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/820</guid>
      <comments>https://rebas.tistory.com/820#entry820comment</comments>
      <pubDate>Thu, 4 Apr 2019 01:04:57 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 16174 &amp;middot; 점프왕 쩰리 (Large)</title>
      <link>https://rebas.tistory.com/819</link>
      <description>&lt;p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/998D7E4C5CA4BE7E13&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F998D7E4C5CA4BE7E13&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;(0, 0)부터 출발하여 점프를 하면서 (N-1, N-1) 도착하는 것을 구현하는 문제다. BFS를 통해 맵을 탐색하면서, 밟은 위치에 쓰여있는 숫자만큼&amp;nbsp;이동하면 된다. 유사한 문제로 BOJ 16173번 '&lt;a href=&quot;https://www.acmicpc.net/problem/16173&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;점프왕 쩰리 (Small)&lt;/a&gt;'이 있다. N 범위만 더 적은 문제이므로, 같은 코드로 해결할 수 있다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;(0, 0)를 큐에 넣고 BFS를 시작한다.&lt;/li&gt;&lt;li&gt;아래 방향(↓)과 오른쪽 방향(→)으로만 이동할 수 있으므로 구현이 간단하다.&lt;/li&gt;&lt;li&gt;현재 위치에 쓰여있는 숫자만큼 이동한다.&lt;/li&gt;&lt;li&gt;방문 여부를 체크하면서 마지막 좌표(N-1, N-1)까지 이동할 수 있는지 확인한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; jump {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;64&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;64&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; check[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;64&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;64&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;jump&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
    q.push({&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;});
    check[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y; q.pop();
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[x][y];
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
            printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;HaruHaru&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;2&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;k, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;k;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[nx][ny]) {
                q.push({nx, ny});
                check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;Hing&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
        }
    }
    bfs();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
} &lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()
    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;))
    check[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        k &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[x][y]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; n&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;HaruHaru&quot;&lt;/span&gt;)
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; dx, dy &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;k, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;k
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[nx][ny]:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;Hing&quot;&lt;/span&gt;)

n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
check &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;n &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
bfs()&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16174&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16174&lt;/a&gt;&lt;/li&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/16173&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 16173&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/819</guid>
      <comments>https://rebas.tistory.com/819#entry819comment</comments>
      <pubDate>Wed, 3 Apr 2019 23:16:15 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 14940 &amp;middot; 쉬운 최단거리</title>
      <link>https://rebas.tistory.com/818</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/997F004D5CA4152815&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F997F004D5CA4152815&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;맵에 주어진 목표지점에서 각 지점에 대한 거리를 구하는 문제다. BFS를 통해 각 점에 대한 최단 거리를 모두 출력하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;N*M 사이즈의 맵에서 목표지점(2)로부터 각 지점까지의 최단 거리를 구한다.&lt;/li&gt;&lt;li&gt;벽(0)으로는 이동할 수 없다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; grid {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m, sx, sy;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;grid&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
    q.push({sx, sy});
    a[sx][sy] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y; q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;) {
                q.push({nx, ny});
                a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            }
        }
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;) sx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; i, sy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; j;
        }
    }
    bfs();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j];
            printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d &quot;&lt;/span&gt;, d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;?&lt;/span&gt; d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;);
        }
        printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;sys&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; stdin, stdout
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; stdin&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;readline
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; stdout&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;write

n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
dx, dy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)
q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
            nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i]
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;=&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; m &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;:
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny))
                a[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[x][y]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;:
            q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((i, j))
            a[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;break&lt;/span&gt;
bfs()
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
        d &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; a[i][j]
        &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d &quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;%&lt;/span&gt; (d&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-2&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; d &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;))
    &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/14940&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 14940&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/818</guid>
      <comments>https://rebas.tistory.com/818#entry818comment</comments>
      <pubDate>Wed, 3 Apr 2019 11:09:17 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 14923 &amp;middot; 미로 탈출</title>
      <link>https://rebas.tistory.com/817</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99E6F4355CA40A6003&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99E6F4355CA40A6003&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : BFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;벽을 하나 뚫고 미로를 탈출하는 것을 구현해야 한다. BOJ 2206번 '&lt;a href=&quot;https://rebas.kr/658&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;벽 부수고 이동하기&lt;/a&gt;'와 유사한 문제이다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;입력으로 주어진 출발점(HX, HY)부터 탈출 위치(EX, EY)까지 BFS 탐색을 통해 이동한다.&lt;/li&gt;&lt;li&gt;이동 거리 저장과 방문 여부를 체크할 배열 dist를 3차원 배열로 선언한다.&lt;/li&gt;&lt;li&gt;dist의 인덱스는 [X 좌표] [Y좌표] [벽 부순 횟수] 이다.&lt;/li&gt;&lt;li&gt;벽(1)을 만나면, 벽을 아직 부수지 않은 경우 벽을 부수고 이동한다. 벽을 이미 부순 경우 이동하지 않는다.&lt;/li&gt;&lt;li&gt;빈칸(0)은 그대로 이동한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;queue&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;struct&lt;/span&gt; maze {
    &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, y, z;
};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; n, m, hx, hy, ex, ey;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dist[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1000&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;2&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;}, dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;() {
    queue&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;maze&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;&lt;/span&gt; q;
    q.push({hx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, hy&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;});
    dist[hx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][hy&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;q.empty()) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().x, y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().y, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q.front().z; q.pop();
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ex&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ey&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
            printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, dist[x][y][z]);
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt;;
        }
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;4&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i], nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; z;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[nx][ny]) {
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nz) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt; nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
            }
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;) {
                q.push({nx, ny, nz});
                dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y][z]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;;
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;-1&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;);
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    memset(dist, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(dist));
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m);
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;hx, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;hy);
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;ex, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;ey);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
        }
    }
    bfs();
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;sys&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; stdin
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; deque
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; stdin&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;readline

n, m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
hx, hy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
ex, ey &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
dist &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m)] &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n)]
dx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;)
dy &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;bfs&lt;/span&gt;():
    q &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; deque()
    q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((hx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, hy&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;))
    dist[hx&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][hy&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;while&lt;/span&gt; q:
        x, y, z &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;popleft()
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; x &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ex&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; y &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; ey&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; dist[x][y][z]
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;4&lt;/span&gt;):
            nx, ny, nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i], z
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; a[nx][ny]:
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nz:
                    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
                &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                    nz &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;:
                dist[nx][ny][nz] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; dist[x][y][z]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+1&lt;/span&gt;
                q&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;append((nx, ny, nz))
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(bfs())&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/14923&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 14923&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://rebas.kr/658&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;벽 부수고 이동하기&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/817</guid>
      <comments>https://rebas.tistory.com/817#entry817comment</comments>
      <pubDate>Wed, 3 Apr 2019 10:36:52 +0900</pubDate>
    </item>
    <item>
      <title>BOJ 14716 &amp;middot; 현수막</title>
      <link>https://rebas.tistory.com/816</link>
      <description>&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center; clear: none; float: none;&quot;&gt;&lt;span class=&quot;imageblock&quot; style=&quot;display: inline-block; width: 600px;  height: auto; max-width: 100%;&quot;&gt;&lt;img src=&quot;https://t1.daumcdn.net/cfile/tistory/99DB55355CA400FE2F&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Ft1.daumcdn.net%2Fcfile%2Ftistory%2F99DB55355CA400FE2F&quot; width=&quot;600&quot; height=&quot;334&quot; filename=&quot;ps.png&quot; filemime=&quot;image/jpeg&quot;/&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;알고리즘 분류 : DFS&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;현수막에 쓰여있는 문자의 개수를 출력하는 문제다. 전형적인 플러드 필 문제이며, 인접한 8방향을 처리하면 된다.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: square;&quot;&gt;&lt;li&gt;M*N 사이즈의 맵을 DFS로 탐색하면서 연결된 글자(1)를 따라 이동하면 된다.&lt;/li&gt;&lt;li&gt;인접한 8방향(상, 하, 좌, 우, 대각선)으로 이동한다.&lt;/li&gt;&lt;li&gt;글자의 개수를 카운트한다.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;C++ 소스코드&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(188, 122, 0);&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; m, n, ans;
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;250&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;250&lt;/span&gt;];
&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;bool&lt;/span&gt; check[&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;250&lt;/span&gt;][&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;250&lt;/span&gt;];
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dx[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;};
&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; dy[] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;};

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;dfs&lt;/span&gt;(&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; x, &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; y) {
    check[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;true&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;8&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx[i], ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy[i];
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;||&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[nx][ny] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; a[nx][ny]) dfs(nx, ny);
    }
}

&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;main&lt;/span&gt;() {
    scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d %d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;m, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            scanf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
        }
    }
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;m; i&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(176, 0, 64);&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=0&lt;/span&gt;; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt;n; j&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;++&lt;/span&gt;) {
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;!&lt;/span&gt;check[i][j] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; a[i][j]) {
                dfs(i, j);
                ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;;
            }
        }
    }
    printf(&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;color: rgb(187, 102, 34); font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(186, 33, 33);&quot;&gt;&quot;&lt;/span&gt;, ans);
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;;
}&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Python 3 소스코드&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre style=&quot;color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 0px; line-height: 18.75px;&quot;&gt;&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255); font-weight: bold;&quot;&gt;sys&lt;/span&gt;
sys&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;setrecursionlimit(&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;100000&lt;/span&gt;)

m, n &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())
a &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;input&lt;/span&gt;()&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;.&lt;/span&gt;split())) &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m)]
check &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; [[&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;False&lt;/span&gt;]&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;*&lt;/span&gt;n &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; _ &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m)]
ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;dfs&lt;/span&gt;(x, y):
    check[x][y] &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;True&lt;/span&gt;
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; dx, dy &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;-1&lt;/span&gt;), (&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;):
        nx, ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;=&lt;/span&gt; x&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dx, y&lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+&lt;/span&gt;dy
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; nx &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; m &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;or&lt;/span&gt; ny &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;&amp;gt;=&lt;/span&gt; n:
            &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;continue&lt;/span&gt;
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[nx][ny] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; a[nx][ny]:
            dfs(nx, ny)

&lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(m):
    &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;range&lt;/span&gt;(n):
        &lt;span style=&quot;color: rgb(0, 128, 0); font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;not&lt;/span&gt; check[i][j] &lt;span style=&quot;color: rgb(170, 34, 255); font-weight: bold;&quot;&gt;and&lt;/span&gt; a[i][j]:
            dfs(i, j)
            ans &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(102, 102, 102);&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;print&lt;/span&gt;(ans)&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;참고&lt;/b&gt;&lt;/p&gt;&lt;ul style=&quot;list-style-type: disc;&quot;&gt;&lt;li&gt;백준 온라인 저지 : &lt;a href=&quot;https://www.acmicpc.net/problem/14716&quot; target=&quot;_blank&quot; class=&quot;tx-link&quot;&gt;BOJ 14716&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;</description>
      <category>프로그래밍/알고리즘</category>
      <category>BOJ</category>
      <category>알고리즘</category>
      <author>레바스</author>
      <guid isPermaLink="true">https://rebas.tistory.com/816</guid>
      <comments>https://rebas.tistory.com/816#entry816comment</comments>
      <pubDate>Wed, 3 Apr 2019 09:44:03 +0900</pubDate>
    </item>
  </channel>
</rss>