题目链接:https://qduoj.com/problem/152/点击打开链接
ycb惹女朋友生气了
发布时间: 2017年6月3日 18:46 最后更新: 2017年6月3日 18:49 时间限制: 1000ms 内存限制: 128M
话说上题中,ycb刚在博知楼饮水机排完队打水,女朋友来电话说也想喝水,让ycb去接水,ycb为了方便就要把自己的水端去给女朋友,可谁知刚走到女朋友跟前,不小心将水全部洒在了地上(如果瓷砖上有水,则水都正好覆盖整块瓷砖,不考虑瓷砖之间的缝隙),ycb的女朋友很生气,对ycb说:”你要是数不清现在教室里有多少个水湾咱俩就分手好了!”。ycb数学不太好,他比较头疼这件事,现在需要你的帮助.
输入第一行包括两个整数n,m.表示教室的长,宽. (1<=n,m<=1000);
第二行到n+1行每行m个字符表示有m块瓷砖,’*’表示瓷砖上有水,‘.’表示没水.
只有一个整数,输出水湾的个数.(若两块瓷砖都有水并且瓷砖之间有一条公共边,则算一个水湾)。
2 2 .* *.
2
3 3 .*. *** .*.
1
简单搜索题 暂时使用了广搜 有空再试试深搜吧
印象中《啊哈算法》里有这个例题
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include<algorithm>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <queue>
#include <stack>
using namespace std;
char a[1000][1000];
int n=0;
int m=0;
int num=0;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct po
{
int x;int y;
};
void bfs(int x,int y)
{
if(a[x][y]=='*')
{
queue<struct po> q;
struct po p;
p.x=x;p.y=y;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
struct po p1;
for(int i=0;i<4;i++)
{
p1.x=p.x+dir[i][0];
p1.y=p.y+dir[i][1];
if(p1.x<0||p1.x>=n||p1.y<0||p1.y>=m||a[p1.x][p1.y]=='.')
continue;
else
{
q.push(p1);
a[p1.x][p1.y]='.';
}
}
}
num++;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",&a[i]);
getchar();
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
bfs(i,j);
}
printf("%d",num);
}