Problem 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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/a48dc2d6f714c4a3475f6f8f0748478d.jpeg)
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
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
题意:找出十字架的个数,要注意十字架旁边不能有#
思路:直接模拟
#include <stdio.h>
#include <string.h>
char map[55][55];
int main()
{
int n,i,j,k,ans,flag;
while(~scanf("%d",&n),n)
{
ans = 0;
for(i = 0; i<n; i++)
scanf("%s",map[i]);
int cnt1,cnt2,cnt3,cnt4;
for(i=1; i<n-1; i++)
{
for(j=1; j<n-1; j++)
{
flag=1;
if(map[i][j]=='#')//这个#是中间的
{
cnt1=cnt2=cnt3=cnt4=0;
for(k=i-1; k>=0; k--)//向上找
{
if(map[k][j]=='#')
{
if(map[k][j-1]=='#'||map[k][j+1]=='#')//每找一个看这个的上下是否有#,有则不是要求的十字架
{
flag=0;
break;
}
cnt1++;//统计向上伸长的长度
}
else break;
}
if(cnt1<1) flag=0;//由于十字架长度要最少为3,所以小于1肯定不行
for(k=i+1; k<n&&flag; k++)//下向
{
if(map[k][j]=='#')
{
if(map[k][j-1]=='#'||map[k][j+1]=='#')
{
flag=0;
break;
}
cnt2++;
}
else break;
}
if(cnt1!=cnt2) flag=0;//上下不等,肯定不是十字架
for(k=j-1; k>=0&&flag; k--)//左向
{
if(map[i][k]=='#')
{
if(map[i-1][k]=='#'||map[i+1][k]=='#')
{
flag=0;
break;
}
cnt3++;
}
else break;
}
if(cnt3<1) flag=0;
for(k=j+1; k<n&&flag; k++)//右向
{
if(map[i][k]=='#')
{
if(map[i-1][k]=='#'||map[i+1][k]=='#')
{
flag=0;
break;
}
cnt4++;
}
else break;
}
if(cnt3!=cnt4) flag=0;
if(flag) ans++;//所有条件符合,个数加1
}
}
}
printf("%d\n",ans);
}
return 0;
}