题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4414
Finding crosses
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1282 Accepted Submission(s): 696
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.
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
Source
2012 ACM/ICPC Asia Regional Hangzhou Online
分析:
以每个位置作为中心点向四周进行查找,看四周是否存在相同长度且连续的#。
AC代码:
#include
#include
#include
#include
#include
using namespace std;
char mmap[55][55];
int n;
bool search_(int i,int j)
{
if(mmap[i][j]=='o') return false;
int len1,len2,len3,len4;
len1=len2=len3=len4=0;
bool flag = false;
for(int x=i-1;x>=0;x--)
{
if(mmap[x][j]=='#')
{
len1++;
if(mmap[x][j-1]=='#' || mmap[x][j+1]=='#')
{
flag = true;
break;
}
}
else break;
}
if(len1<1 || flag) return false;
for(int x=i+1;xif(mmap[x][j]=='#')
{
len2++;
if(mmap[x][j-1]=='#' || mmap[x][j+1]=='#')
{
flag = true;
break;
}
}
else break;
}
if(len1!=len2 || flag) return false;
for(int y=j-1;y>=0;y--)
{
if(mmap[i][y]=='#')
{
len3++;
if(mmap[i-1][y]=='#' || mmap[i+1][y]=='#')
{
flag = true;
break;
}
}
else break;
}
if(len1!=len3 || flag) return false;
for(int y=j+1;yif(mmap[i][y]=='#')
{
len4++;
if(mmap[i-1][y]=='#' || mmap[i+1][y]=='#')
{
flag = true;
break;
}
}
else break;
}
if(len1!=len4 || flag) return false;
return true;
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i=0;iscanf("%s",mmap[i]);
int sum = 0;
for(int i=0;ifor(int j=0;jif(search_(i,j)) sum++;
}
printf("%d\n",sum);
}
return 0;
}