您的位置:首页 > 其它

sjtu oj 1003.二哥养细菌

2014-02-21 22:21 337 查看
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
struct point
{
int x,y;
point(int a,int b){x=a;y=b;}
point(){};
};
int L,res=0,n0=0,n1=0,temp,c[100][100];
queue<point> q;

bool check(int x,int y)
{
if(x<0||x>=L||y<0||y>=L)
return false;
if(c[x][y]!=0)
return false;
return true;
}
void f()
{
temp=0;
int x,y;
while(n1--)
{
x=q.front().x;
y=q.front().y;
q.pop();
if(check(x-1,y))
{
temp++;
q.push(point(x-1,y));
c[x-1][y]=1;
}
if(check(x,y-1))
{
temp++;
q.push(point(x,y-1));
c[x][y-1]=1;
}
if(check(x,y+1))
{
temp++;
q.push(point(x,y+1));
c[x][y+1]=1;
}
if(check(x+1,y))
{
temp++;
q.push(point(x+1,y));
c[x+1][y]=1;
}

}
n1=temp;
n0-=temp;

}
int main()
{
int i,j;
scanf("%d",&L);
for(i=0;i<L;i++)
{
for(j=0;j<L;j++)
{
scanf("%d",&c[i][j]);
if(c[i][j]==1)
{
q.push(point(i,j));
n1++;
}
if(c[i][j]==0)
n0++;
}
}
while(n0)
{
f();
res+=1;
}
printf("%d\n",res);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: