企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
深度优先搜索的用法——lake counting <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="882" valign="top" style="width:661.75pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:14pt; font-family:宋体"><strong>问题主题:</strong></span><span style="font-size:14pt; font-family:'Times New Roman'">Lake Counting</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p></td></tr><tr><td width="882" valign="top" style="width:661.75pt; padding:0pt 5.4pt; border-left-width:0.5pt; border-style:none solid solid; border-left-color:rgb(0,0,0); border-right-width:0.5pt; border-right-color:rgb(0,0,0); border-bottom-width:0.5pt; border-bottom-color:rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:14pt; font-family:宋体"><strong>问题描述:</strong></span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">有一个大小为</span><span style="font-size:14pt; font-family:'Times New Roman'">N*M</span><span style="font-size:14pt; font-family:宋体">的园子,雨后积了很多水。八连通的积水被认为是在一起的。请求出园子里共有多少个水洼?</span><span style="font-size:14pt; font-family:'Times New Roman'">(</span><span style="font-size:14pt; font-family:宋体">八连通是指下图中相对</span><span style="font-size:14pt; font-family:'Times New Roman'">+</span><span style="font-size:14pt; font-family:宋体">的</span><span style="font-size:14pt; font-family:'Times New Roman'">*</span><span style="font-size:14pt; font-family:宋体">部分</span><span style="font-size:14pt; font-family:'Times New Roman'">)</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:28pt"><span style="font-size:14pt; font-family:'Times New Roman'">+++</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:28pt"><span style="font-size:14pt; font-family:'Times New Roman'">+*+</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:28pt"><span style="font-size:14pt; font-family:'Times New Roman'">+++</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:14pt; font-family:宋体"><strong>限制条件:</strong></span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:28pt"><span style="font-size:14pt; font-family:'Times New Roman'">N,M &lt;= 100</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p></td></tr><tr><td width="882" valign="top" style="width:661.75pt; padding:0pt 5.4pt; border-left-width:0.5pt; border-style:none solid solid; border-left-color:rgb(0,0,0); border-right-width:0.5pt; border-right-color:rgb(0,0,0); border-bottom-width:0.5pt; border-bottom-color:rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:14pt; font-family:宋体"><strong>样例:</strong></span><span style="font-size:14pt; font-family:'Times New Roman'"><strong/></span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:14pt; font-family:宋体">输入</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">N=10, M=12</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:宋体">园子如下图</span><span style="font-size:14pt; font-family:'Times New Roman'">(‘+’</span><span style="font-size:14pt; font-family:宋体">表示积水,</span><span style="font-size:14pt; font-family:'Times New Roman'">’*’</span><span style="font-size:14pt; font-family:宋体">表示没有积水</span><span style="font-size:14pt; font-family:'Times New Roman'">)</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">+****++*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">*+++***+++</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">**++***++*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">*****++*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">*****+**</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">**+****+**</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">*+*+***++*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">+*+*+***+*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">*+*+****+*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">**+*****+*</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:14pt; font-family:宋体">输出</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'; text-indent:27pt"><span style="font-size:14pt; font-family:'Times New Roman'">3</span><span style="font-size:14pt; font-family:'Times New Roman'"/></p></td></tr></tbody></table> ### 【解法一】 ### 解题分析:    从任意的’+’开始,不停地把邻接的部分用’*’代替,一次dfs(深度优先遍历)遍历后,与初始的这个+所连接的所有+都会被替换成*,因此直到图中没有+为止,总共进行dfs的次数即为积水的次数。 ### 程序实现: **C++** <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="882" valign="top" style="width:661.8pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">#include</span><span style="font-size:9.5pt; font-family:新宋体"> </span><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"iostream"</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">using</span><span style="font-size:9.5pt; font-family:新宋体"> </span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">namespace</span><span style="font-size:9.5pt; font-family:新宋体"> std;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">const</span><span style="font-size:9.5pt; font-family:新宋体"> </span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> N = 10;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">const</span><span style="font-size:9.5pt; font-family:新宋体"> </span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> M = 12;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">char</span><span style="font-size:9.5pt; font-family:新宋体"> garden[N][M+1] = {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"+****++*"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"*+++***+++"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"**++***++*"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"*****++*"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"*****+**"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"**+****+**"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"*+*+***++*"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"+*+*+***+*"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"*+*+****+*"</span><span style="font-size:9.5pt; font-family:新宋体">,</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">"**+*****+*"</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体">;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">void</span><span style="font-size:9.5pt; font-family:新宋体"> dfs(</span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> x, </span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> y);</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">void</span><span style="font-size:9.5pt; font-family:新宋体"> solve() {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> count = 0;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">for</span><span style="font-size:9.5pt; font-family:新宋体">(</span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> j=0; j&lt;N; j++) {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">for</span><span style="font-size:9.5pt; font-family:新宋体">(</span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> i=0; i&lt;M; i++){</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">if</span><span style="font-size:9.5pt; font-family:新宋体">(garden[j][i] == </span><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">'+'</span><span style="font-size:9.5pt; font-family:新宋体">) {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">dfs(j, i);</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">count ++;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">cout &lt;&lt; count &lt;&lt; endl;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">void</span><span style="font-size:9.5pt; font-family:新宋体"> dfs(</span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> y, </span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> x) {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">garden[y][x] = </span><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">'*'</span><span style="font-size:9.5pt; font-family:新宋体">;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">for</span><span style="font-size:9.5pt; font-family:新宋体">(</span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> dy=y-1; dy&lt;=y+1; dy++) {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">for</span><span style="font-size:9.5pt; font-family:新宋体">(</span><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> dx=x-1; dx&lt;=x+1; dx++) {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">if</span><span style="font-size:9.5pt; font-family:新宋体">(dx &gt;=0 &amp;&amp; dy &gt;= 0 &amp;&amp; dx &lt; M &amp;&amp; dy &lt; N &amp;&amp; garden[dy][dx] == </span><span style="color:rgb(163,21,21); font-size:9.5pt; font-family:新宋体">'+'</span><span style="font-size:9.5pt; font-family:新宋体">) {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">dfs(dy, dx);</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"> </span></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">int</span><span style="font-size:9.5pt; font-family:新宋体"> main() {</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="font-size:9.5pt; font-family:新宋体">solve();</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体"/><span style="color:rgb(0,0,255); font-size:9.5pt; font-family:新宋体">return</span><span style="font-size:9.5pt; font-family:新宋体"> 0;</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:9.5pt; font-family:新宋体">}</span><span style="font-size:9.5pt; font-family:新宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"><span style="font-size:10.5pt; font-family:'Times New Roman'"> </span></p></td></tr></tbody></table> **Java** <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="882" valign="top" style="width:661.8pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:10.5pt; font-family:'&amp;#23435;&amp;#20307;'"/><pre code_snippet_id="143244" snippet_file_name="blog_20140105_1_3080505" name="code" class="java">/** * User: luoweifu * Date: 14-1-7 * Time: 下午4:53 */ public class LakeCounting { public static final int N = 10; public static final int M = 12; private String garden[] = { "+****++*", "*+++***+++", "**++***++*", "*****++*", "*****+**", "**+****+**", "*+*+***++*", "+*+*+***+*", "*+*+****+*", "**+*****+*" }; public void solve() { int count = 0; for(int j=0; j&lt;N; j++) { for(int i=0; i&lt;M; i++){ if(garden[j].charAt(i) == '+') { dfs(j, i); count ++; } } } System.out.println(count); } public void dfs(int y, int x) { StringBuilder stringY = new StringBuilder(garden[y]); stringY.setCharAt(x, '*'); garden[y] = stringY.toString(); for(int dy=y-1; dy&lt;=y+1; dy++) { for(int dx=x-1; dx&lt;=x+1; dx++) { if(dx &gt;=0 &amp;&amp; dy &gt;= 0 &amp;&amp; dx &lt; M &amp;&amp; dy &lt; N &amp;&amp; garden[dy].charAt(dx) == '+') { dfs(dy, dx); } } } } public static void main(String args[]) { LakeCounting lakeCounting = new LakeCounting(); lakeCounting.solve(); } }</pre><br/><p/></td></tr></tbody></table>