水池数目(搜索第二题)

地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=27

冷啊……

心寒……

搞了半天,原来是广搜,终于明白了两者的区别了,看了这两个题还要再用深搜写一遍了…………

#include<stdio.h> #include<string.h> #include<queue> using namespace std; int m[100][100],sx[4]={0,0,1,-1},zy[4]={1,-1,0,0}; typedef struct {int x,y;}WA; int search(int x1,int y1) { int count=0,i,j; for(int k=0;k<x1;k++) for(int l=0;l<y1;l++) { if(m[k][l]==1) { m[k][l]=0; queue <WA> Q; WA a={k,l}; Q.push(a); ++count; while(!Q.empty()) { for(int p=0;p<4;p++) { i=Q.front().x+zy[p]; j=Q.front().y+sx[p]; if(i>=0&&i<=x1-1&&j>=0&&j<=y1-1&&m[i][j]==1) { WA b={i,j}; Q.push(b); m[i][j]=0; } } Q.pop(); } } } return count; } int main() { int a,b,c,d; scanf("%d",&a); while(a--) { memset(m,0,sizeof(m)); scanf("%d%d",&b,&c); for(int i=0;i<b;i++) for(int j=0;j<c;j++) scanf("%d",&m[i][j]); printf("%d\n",search(b,c)); } return 0; }


深搜啊,递归啊!!悲剧啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值