池塘问题(深搜)
来源:互联网 发布:福建安溪如意茶厂淘宝 编辑:程序博客网 时间:2024/04/25 09:04
题目:
Description
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John's field, determine how many ponds he has.
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John's field.
Sample Input
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
Sample Output
3
Hint
下面是一个正确的代码。
#include<stdio.h>#define MAXN 110char ww[MAXN][MAXN];int vis[MAXN][MAXN];int f=0;int n,m;void search(int x,int y){ if(x<0||x>=n||y<0||y>=m||vis[x][y]||ww[x][y]=='.') return; vis[x][y]=1; search(x+1,y); search(x+1,y+1);search(x+1,y-1);search(x-1,y);search(x-1,y+1);search(x-1,y-1);search(x,y+1);search(x,y-1);}main(){char z;int i,j;int x,y;for(i=0;i<MAXN;i++){for(j=0;j<MAXN;j++)vis[i][j]=0;}scanf("%d%d",&n,&m); scanf("%s",ww[i]);}for(i=0;i<n;i++){for(j=0;j<m;j++){if(!vis[i][j]&&ww[i][j]=='W'){search(i,j);f++;}}}printf("%d\n",f);}
下面是一个错误的代码:
#include<stdio.h>#define MAXN 110char ww[MAXN][MAXN];int vis[MAXN][MAXN];int f=0;void search(int x,int y){vis[x][y]=1;if(!vis[x][y]&&ww[x][y]=='W'){search(x+1,y);search(x+1,y+1);search(x+1,y-1);search(x-1,y);search(x-1,y+1);search(x-1,y-1);search(x,y+1);search(x,y-1);}}main(){int i,j;int n,m;int x,y;for(i=0;i<MAXN;i++){for(j=0;j<MAXN;j++)vis[i][j]=0;}scanf("%d%d",&n,&m);getchar();for(i=0;i<MAXN;i++){for(j=0;j<MAXN;j++)ww[i][j]='.';}for(i=1;i<=n;i++){for(j=1;j<=m;j++)getchar(ww[i][j]);getchar();}for(i=1;i<=n;i++){for(j=1;j<=m;j++){printf("%c\n",ww[i][j]);if(!vis[i][j]&&ww[i][j]=='W'){search(i,j);f++;}}}printf("%d\n",f);}
上面的代码中有两处错误,但是我不知道具体哦原因。
一处是gerchar的使用会使编译器报错。
另一处是深搜函数有问题,可能不执行递归。
没有进行调试,具体原因不清楚。
0 0
- 池塘问题(深搜)
- 032_池塘问题(lake counting)
- 池塘
- 池塘
- bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘(BFS)
- 池塘取水
- BZOJ 1698 (USACO 2007 Feb)Liypad Pond (黄金)荷叶池塘
- 选对池塘钓大鱼
- 鱼与池塘
- 鲸鱼与池塘
- 青草池塘处处蛙
- [JZOJ 4855]荷花池塘
- 【jzoj4855】【荷花池塘】【最短路】
- 最小生成树——(性质)其最大边权为生成树中最大边权最小的——(例题)承包池塘的青蛙
- 选对池塘钓大鱼-做好生涯规划
- 模拟鸟和鱼的生态池塘
- 【五校联考2015 9.13】池塘
- 池塘夜降彩色雨部分总结
- 成功求职者的自述:如何向应聘企业展示自身价值?
- 转android的UI设计七
- [Mugeda HTML5技术教程之16]案例分析:制作跨屏互动游戏
- TR069协议向导——一个帮助你了解TR069协议的简明教程(一)
- 人生哲学 - 易经
- 池塘问题(深搜)
- 针对IE的CSS hack 全面 实用
- 灯火阑珊处等你回眸
- linux svn 命令详解
- 《计算机文化基础》
- DownloadProvider 源码详细分析
- TR069协议向导—— 一个帮助你了解TR069协议的简明教程(二)
- Ralph Lauren Rea A wee
- Flume-0.9.4和Hbase-0.96整合(1)