地址: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; }
深搜啊,递归啊!!悲剧啊
551

被折叠的 条评论
为什么被折叠?



