http://poj.org/problem?id=2386

题目大意:

有一个大小为N*M的园子,雨后积起了水。八连通的积水被认为是连接在一起的。请求出院子里共有多少水洼?

思路:

水题~直接DFS,DFS过程把途中表示水洼的W改为‘.',看DFS了几次即可。

  1. #include<cstdio>
  2. #include<cstring>
  3. const int MAXN=100+10;
  4. char map[MAXN][MAXN];
  5. int n,m;
  6. void dfs(int x,int y)
  7. {
  8. if(map[x][y]=='.')
  9. return ;
  10.  
  11. map[x][y]='.';
  12. dfs(x+1,y+1);
  13. dfs(x,y+1);
  14. dfs(x-1,y+1);
  15. dfs(x-1,y);
  16. dfs(x-1,y-1);
  17. dfs(x,y-1);
  18. dfs(x+1,y-1);
  19. dfs(x+1,y);
  20. }
  21.  
  22. int main()
  23. {
  24. while(~scanf("%d%d",&n,&m))
  25. {
  26. for(int i=1;i<=n;i++)
  27. scanf("%s",map[i]+1);
  28.  
  29. for(int i=0;i<=n+1;i++)
  30. map[i][0]=map[i][m+1]='.';
  31. for(int i=0;i<=m+1;i++)
  32. map[0][i]=map[n+1][i]='.';
  33.  
  34. int ans=0;
  35. for(int i=0;i<=n+1;i++)
  36. {
  37. for(int j=0;j<=m+1;j++)
  38. {
  39. if(map[i][j]=='W') {
  40. ans++;
  41. dfs(i,j);
  42. }
  43. }
  44. }
  45. printf("%d\n",ans);
  46. }
  47.  
  48. return 0;
  49. }

POJ 2386 Lake Counting DFS水水的更多相关文章

  1. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  2. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  3. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  4. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  5. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  6. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  7. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  8. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

  9. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

随机推荐

  1. Annotation中Result的params属性

    这个属性只有在重定向时有用,而转发时不会设置参数. 如: @Results({ @Result(name="success", location="page", ...

  2. OCP-1Z0-051-题目解析-第26题

    26. Which is the valid CREATE TABLE statement? A. CREATE TABLE  emp9$#  (emp_no NUMBER (4));  B. CRE ...

  3. [Java开发之路](9)对象序列化与反序列化

    1. 对象序列化 当你创建对象时.仅仅要你须要.它会一直存在,可是程序终止时,不管何时它都不会继续存在.虽然这样做是很有意义的,可是在某些情况下.假设程序不执行时扔能存在而且保存其信息,那将对我们很实 ...

  4. 归并排序_分治算法 (白书P226)

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a ...

  5. Nginx安装以及配置

    安装编译工具及库文件 1 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 安装 PCRE 下载 PC ...

  6. jq ---- 实现浏览器全屏

    // 点击进入全屏 方法. var fullscreen=function(){ elem=document.body; if(elem.webkitRequestFullScreen){ elem. ...

  7. 洛谷P2115 [USACO14MAR]破坏Sabotage

    题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...

  8. HDU 2633 Getting Driving License(模拟)

    Getting Driving License Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  9. Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer

    A computer system is described having one or more host processors, a host chipset and an input/outpu ...

  10. Centos6.4安装opennebula

    Centos6.4安装opennebula #安装163源 http://mirrors.163.com/.help/CentOS6-Base-163.repo #安装epel源 wget http: ...