<div class="colorscripter-code" style="color:rgb(1,1,1);overflow:auto;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> <table class="colorscripter-code-table" style="margin:0px;padding:0px;border:;background-color:rgb(250,250,250);" cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:6px;border-right-color:rgb(229,229,229);border-right-width:2px;border-right-style:solid;"> <div style="margin:0px;padding:0px;text-align:right;color:rgb(102,102,102);line-height:130%;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> <div style="line-height:130%;">1</div> <div style="line-height:130%;">2</div> <div style="line-height:130%;">3</div> <div style="line-height:130%;">4</div> <div style="line-height:130%;">5</div> <div style="line-height:130%;">6</div> <div style="line-height:130%;">7</div> <div style="line-height:130%;">8</div> <div style="line-height:130%;">9</div> <div style="line-height:130%;">10</div> <div style="line-height:130%;">11</div> <div style="line-height:130%;">12</div> <div style="line-height:130%;">13</div> <div style="line-height:130%;">14</div> <div style="line-height:130%;">15</div> <div style="line-height:130%;">16</div> <div style="line-height:130%;">17</div> <div style="line-height:130%;">18</div> <div style="line-height:130%;">19</div> <div style="line-height:130%;">20</div> <div style="line-height:130%;">21</div> <div style="line-height:130%;">22</div> <div style="line-height:130%;">23</div> <div style="line-height:130%;">24</div> <div style="line-height:130%;">25</div> <div style="line-height:130%;">26</div> <div style="line-height:130%;">27</div> <div style="line-height:130%;">28</div> <div style="line-height:130%;">29</div> <div style="line-height:130%;">30</div> <div style="line-height:130%;">31</div> <div style="line-height:130%;">32</div> <div style="line-height:130%;">33</div> <div style="line-height:130%;">34</div> <div style="line-height:130%;">35</div> <div style="line-height:130%;">36</div> <div style="line-height:130%;">37</div> <div style="line-height:130%;">38</div> <div style="line-height:130%;">39</div> <div style="line-height:130%;">40</div></div></td> <td style="padding:6px 0px;"> <div style="margin:0px;padding:0px;color:rgb(1,1,1);line-height:130%;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;"> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(0,134,179);">#include</span> <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span>vector<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">></span></div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(0,134,179);">#include</span> <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span>algorithm<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">></span></div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(0,134,179);">#include</span> <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span>functional<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">></span></div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(0,134,179);">#include</span> <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span>iostream<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">></span></div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(0,134,179);">#include</span> <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span>map<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">></span></div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(167,29,93);">using</span> <span style="color:rgb(167,29,93);">namespace</span> <span style="color:rgb(6,109,226);">std</span>;</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(167,29,93);">class</span> FilipTheFrog{</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(167,29,93);">public</span>:</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(6,109,226);">int</span> countReachableIslands(vector<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span><span style="color:rgb(6,109,226);">int</span><span style="color:rgb(167,29,93);">></span> positions, <span style="color:rgb(6,109,226);">int</span> L){</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> map<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span><span style="color:rgb(6,109,226);">int</span>, <span style="color:rgb(6,109,226);">int</span><span style="color:rgb(167,29,93);">></span> reachable;</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> reachable[<span style="color:rgb(0,153,204);">0</span>] <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> positions[<span style="color:rgb(0,153,204);">0</span>];</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(167,29,93);">for</span> (map<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span><span style="color:rgb(6,109,226);">int</span>, <span style="color:rgb(6,109,226);">int</span><span style="color:rgb(167,29,93);">></span>::iterator map_iter <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> reachable.begin(); map_iter <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">!</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> reachable.end(); map_iter<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">+</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">+</span>)</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> {</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(167,29,93);">for</span> (<span style="color:rgb(167,29,93);">unsigned</span> <span style="color:rgb(6,109,226);">int</span> i <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> <span style="color:rgb(0,153,204);">0</span>; i <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span> positions.size(); i<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">+</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">+</span>)</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> {</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(167,29,93);">if</span> (abs(map_iter<span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">-</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">></span>second <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">-</span> positions[i]) <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> L)</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> {</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(167,29,93);">if</span> (reachable.find(positions[i]) <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> reachable.end())</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> {</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> reachable[reachable.size()] <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> positions[i];</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> }</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> }</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> }</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> }</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(167,29,93);">return</span> reachable.size();</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> }</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;">};</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"><span style="color:rgb(6,109,226);">int</span> main(){</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> FilipTheFrog result_class;</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(6,109,226);">int</span> result <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);">=</span> result_class.countReachableIslands({ <span style="color:rgb(0,153,204);">4</span>, <span style="color:rgb(0,153,204);">7</span>, <span style="color:rgb(0,153,204);">1</span>, <span style="color:rgb(0,153,204);">3</span>, <span style="color:rgb(0,153,204);">5</span> }, <span style="color:rgb(0,153,204);">1</span>);</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> </div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(6,109,226);">cout</span> <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span> result <span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span><span style="color:rgb(1,1,1);"></span><span style="color:rgb(167,29,93);"><</span> endl;</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;"> <span style="color:rgb(167,29,93);">return</span> <span style="color:rgb(0,153,204);">0</span>;</div> <div style="padding:0px 6px;line-height:130%;white-space:pre;">}</div></div> <div style="text-align:right;font-size:9px;font-style:italic;margin-top:-13px;margin-right:5px;"><a style="color:rgb(229,229,229);text-decoration:none;" href="http://colorscripter.com/info#e" target="_blank">Colored by Color Scripter</a></div></td> <td style="padding:0px 2px 4px 0px;vertical-align:bottom;"><a style="color:#FFFFFF;text-decoration:none;" href="http://colorscripter.com/info#e" target="_blank"><span style="padding:1px;color:#FFFFFF;font-size:9px;background-color:rgb(229,229,229);">cs</span></a></td></tr></tbody></table></div> <div> 20번째 줄의 if조건문에서 find 멤버 함수를 이용해서 reachable 안에 positions[i]와 동일한 value가 없으면 reachable에 그 값을 삽입하고 아니면 그냥 넘어가게 하려고 하는데 </div> <div>reachable.find(k) 일 때 k 값이 없다면 end() 반복자를 가리키니까 중복된 값은 저장되지 않도록 될줄 알았는데 2번이나 더 4가 저장되더라구요.</div> <div>디버그했을 때 최종적으로 reachable에 저장된 값은 (key, value)가 순서대로 (0, 4) (1, 4) (2, 3) (3, 5) (4, 4) (5, 5)가 됩니다.</div> <div>원하는 값은 (0, 4) (1, 3) (2, 5)이고 따라서 리턴 값이 3이 되었으면 좋겠는데 중복된 값이 저장되서 리턴 값이 6이 되네요.. </div> <div>어떻게 수정해야 중복된 값이 저장되지 않을지 모르겠습니다. ㅠ</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.