题目: http://blog.csdn.net/qq_35786326/article/details/78820692
题意:
求矩阵中的连续(上下左右都可)板块数
分析:
90%都是水分,只需要中等水平的广搜+双重循环即可AC
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#define LL long long//C++的福音!!!400ms——cin,120ms——超快输入流
using namespace std;
inline LL read(){
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
int t,n,m,head,tail,state[10001][2],x[70][70],i,j,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
void bfs()
{
int i,j,f=0,x1,y1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(x[i][j]>0) f=1;
if(f==1) break;
}
if(f==1) break;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(x[i][j]>0)
{
x[i][j]=-1;//0为不可走,-1为已走过,>0为没走过 t++;//找到一个>0则+1,与其在一起的其他细胞,用广搜筛掉
state[1][0]=i;state[1][1]=j;head=0;tail=1;//广搜初始化
do//广搜开始
{
head++;
for(int k=0;k<4;k++)
{
x1=state[head][0]+dx[k];y1=state[head][1]+dy[k];
if(x[x1][y1]>0&&x1>0&&x1<=n&&y1>0&&y1<=m)
{
tail++,x[x1][y1]=-1;
state[tail][0]=x1;state[tail][1]=y1;
}
}
}
while(head<tail);
}
}
}
printf("%d",t);
return;
}
int main()
{
n=read();m=read();
int i,j;char s;
for(i=1;i<=n;i++)
{
j=0;
while(s=getchar(),s!='\n')
{
j++;
x[i][j]=s-48;
}
}
bfs();
return 0;
}