您的位置:首页 > 其它

HDU-1045 Fire Net

2013-04-11 08:55 267 查看
#include<stdio.h>
#include<string.h>

char a[6][6];
int v[6][6],hash[6][6];//V标记是否已经放过,hash标记是否此位置是否进入过队列
int n,sum,max;

struct N
{
int x,y;
};

int judge(int x,int y)
{
int i,j;
for(i = x+1; i <= n; i++)
if(v[i][y]) return 0;
else if(a[i][y] == 'X') break;
for(i = x-1; i >= 1; i--)
if(v[i][y]) return 0;
else if(a[i][y] == 'X') break;
for(i = y+1; i <= n; i++)
if(v[x][i]) return 0;
else if(a[y][i] == 'X') break;
for(i = y-1; i >= n; i--)
if(v[x][i]) return 0;
else if(a[x][i] == 'X') break;
return 1;
}

void bfs(int x,int y)
{
int i,j;
sum = 0;
struct N q[5000],t1,t;
q[0].x = x;
q[0].y = y;
int s = 0,e = 1;
int fx[] = {-1,-1, 1, 1};
int fy[] = { 1,-1,-1, 1};
while(s != e)
{
t.x = q[s].x;
t.y = q[s].y;
s++;
if(a[t.x][t.y] == '.' && !v[t.x][t.y])
{
if(judge(t.x,t.y))
{
v[t.x][t.y] = 1;
sum++;
}
}
for(i = 0;i < 4; i++)
{
t1.x = t.x + fx[i];
t1.y = t.y + fy[i];
if(t1.x >= 1 && t1.x <= n && t1.y >= 1 && t1.y <= n && !hash[t1.x][t1.y])
{
hash[t1.x][t1.y] = 1;
q[e].x = t1.x;
q[e].y = t1.y;
e++;
}
}
e %= 5000;
s %= 5000;
}
if(max < sum)
{

max = sum;
}
}

int main()
{
int i,j;
while(scanf("%d%*c",&n) != EOF && n)
{
max = 0;
for(i = 1;i <= n; i++)
scanf("%s",a[i]+1);
for(i = 1;i <= n; i++)
for(j = 1;j <= n; j++)
{
memset(v,0,sizeof(v));
memset(hash,0,sizeof(hash));
if(a[i][j] == '.')
{
bfs(i,j);
}
}
printf("%d\n",max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: