您的位置:首页 > 其它

HDU 4414: Finding crosses

2016-08-10 16:36 309 查看


Description

The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in southern Peru. They were designated as a UNESCO World Heritage Site in 1994. The high, arid plateau stretches more than 80 kilometres (50 mi) between the towns
of Nazca and Palpa on the Pampas de Jumana about 400 km south of Lima. Although some local geoglyphs resemble Paracas motifs, scholars believe the Nazca Lines were created by the Nazca culture between 400 and 650 AD.[1] The hundreds of individual figures range
in complexity from simple lines to stylized hummingbirds, spiders, monkeys, fish, sharks, orcas, llamas, and lizards.

Above is the description of Nazca Lines from Wikipedia. Recently scientists found out that those lines form many crosses. Do those crosses have something to do with the Christian religion? Scientists are curious about this. But at first, they want to figure
out how many crosses are there. So they took a huge picture of Nazca area from the satellite, and they need you to write a program to count the crosses in the picture.

To simplify the problem, we assume that the picture is an N*N matrix made up of 'o' and '#', and some '#' can form a cross. Here we call three or more consecutive '#' (horizontal or vertical) as a "segment".

The definition of a cross of width M is like this:

1) It's made up of a horizontal segment of length M and a vertical segment of length M.

2) The horizontal segment and the vertical segment overlap at their centers.

3) A cross must not have any adjacent '#'.

4) A cross's width is definitely odd and at least 3, so the above mentioned "centers" can't be ambiguous.

For example, there is a cross of width 3 in figure 1 and there are no cross in figure 2 ,3 and 4.



You may think you find a cross in the top 3 lines in figure 2.But it's not true because the cross you find has a adjacent '#' in the 4th line, so it can't be called a "cross". There is no cross in figure 3 and figure 4 because of the same reason.

Input

There are several test cases.

In each test case:

The First line is a integer N, meaning that the picture is a N * N matrix ( 3<=N<=50) .

Next N line is the matrix.

The input end with N = 0

Output

For each test case, output the number of crosses you find in a line.

Sample Input

4
oo#o
o###
oo#o
ooo#
4
oo#o
o###
oo#o
oo#o
5
oo#oo
oo#oo
#####
oo#oo
oo##o
6
ooo#oo
ooo##o
o#####
ooo#oo
ooo#oo
oooooo
0


Sample Output

1
0
0
0

以每个点为十字架中心,搜索一次,就可以判断十字架的个数,搜索时记录四个方向,搜索结果各边#数相等就说明这个中心符合要求,

在搜索过程中,如果路径旁边有#破坏十字架形状就返回-1;

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,ans;
char mp[59][59];
int dx[]= {0,0,1,-1},dy[]= {1,-1,0,0};
int judge(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<n&&mp[x][y]=='#';
}
int dfs(int x,int y,int dir)
{
int ret=0;
while(judge(x,y))
{
if(dir==0||dir==1)
{
for(int i=2; i<=3; i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(judge(tx,ty))
{
return -1;
}
}
}
else
{
for(int i=0; i<=1; i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(judge(tx,ty))
{
return -1;
}
}
}
x+=dx[dir],y+=dy[dir];
ret++;
}
return ret;
}
int main()
{
while(scanf("%d",&n)&&n)
{
ans=0;
for(int i=0; i<n; i++)
scanf("%s",mp[i]);
for(int i=1; i<n-1; i++)
for(int j=1; j<n-1; j++)
if(mp[i][j]=='#')
{
int a=dfs(i+dx[0],j+dy[0],0);
int b=dfs(i+dx[1],j+dy[1],1);
int c=dfs(i+dx[2],j+dy[2],2);
int d=dfs(i+dx[3],j+dy[3],3);
if(a==b&&b==c&&c==d&&a>0)
ans++;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息