The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

Pku 3620 Avoid the lake -dfs基本问题

Pku 3620解题报告

一、原题

Description

Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest "lake" on his farm.

The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ KN × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected cell and is part of the lake.

Input

* Line 1: Three space-separated integers: N, M, and K
* Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C

Output

* Line 1: The number of cells that the largest lake contains. 

Sample Input

3 4 5 
3 2 
2 2 
3 1 
2 3 
1 1 

Sample Output

4 
二.题意阐述 
1.其实本题可以转化成如下的纯数学模型。 
2.给出n*m大小的矩阵,初始值全为0 
3.在特定的位置将a[i][j]置成1 
4.求取相连的1个数的最大值。 
  
.算法 
  此题看似简单,但实际上蕴藏玄机。懂得此法的同学将无疑大大提升自己的能力,再将其拓展,则能够解决一大类问题,此题包含着一个及其重要的数学模型----递归搜索(名字是自己取的,不专业请原谅)。 
  只要设计一个函数,给一个入口参数(I,j),使得运行该函数后,得到与(I,j)方块相连的方块数。 
然后在用循环,将每一个方块都找一遍,求出最大值即可。 
  那个函数,这是本题的精髓,用递归的方法,可以让其自动朝着四周的方向搜索满足条件的方块,直到求出与某一格相连的小方块数目的总数。 
  其实,我一直想把上回的超级玛丽做出来,这个题给了我基本的搜索本领。我想,只要我继续研究下去就一定能解决超级玛丽的问题了 o(_)o… 

 

四、解题过程

本来想用循环的方法来做的,可是发现用循环貌似无法结束查找。于是我请教了陈泽怡学长,他提示我用递归的方法来解此题,这才使我恍然大悟。用递归可以不断地调用函数体本身,直到结束查找!~

.程序代码

 

#include<iostream>

#include
<math.h>

#include
<algorithm>

using namespace std;

int a[101][101]={0};

int num;

void search(int i,int j)

{

      

       
if(a[i][j]==1)

       {

              a[i][j]
=-1;

              num
++;

              search(i,j
+1);

              search(i
+1,j);

              search(i,j
-1);

              search(i
-1,j);

       }

      

}
//定义递归搜索函数,入口参数为所要调查的小方块位置

      

      

      

int main()

{

             

       
int n,m,k,i,j,x,y,max=0;

       cin
>>n>>m>>k;

       
for(i=1;i<=k;i++)

       {

              cin
>>x>>y;

              a[x][y]
=1;//把要调查的位置置为1;

       }

       
for(i=1;i<=n;i++)

              
for(j=1;j<=m;j++)

              {

                     num
=0;

                     search(i,j);

                     
if(num>=max)

                            max
=num;

      

              }
//用循环的方法将整个矩阵都查找一遍,并求出与某小方块相连方块数的最大值;

              cout
<<max<<endl;//输出该最大值;

              
return 0;

                    

}

 

六.小结 
这道题的代码看上去很短,但是确非常非常的重要,当然 ,这并不代表我完全掌握了这类方法,如果说不是深度或者广度优先怎么办,如何用队列?这是接下来我需要面对和解决的问题另外,我写报告的水平有待提高,如果不是知道我的意图,会有人看得懂这份报告么?ACM讲究的是团队作战,即使我很优秀 ,也不过是个独断独行的人,这是不能成功的,要想取得成绩,先得学会与同学交流。 
呵呵 这是我的第一篇结题报告 值得纪念啊 当然还要特别感谢贾琼学姐哦 O(∩_∩)O~ 

posted on 2009-02-19 13:07 abilitytao 阅读(1022) 评论(1)  编辑 收藏 引用

评论

# re: Pku 3620 Avoid the lake -dfs基本问题 2009-06-19 19:41 fs

扫雷的算法  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理