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): 727 Accepted Submission(s): 409
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
Recommend
liuyiding
解析:求得十字形的数量
对每一个可能存在十字形的点一一讨论。
1.讨论其上下左右是否对称分布
2,判断其周边是否有杂质
*/
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<algorithm> #include <iostream> using namespace std; const int maxn=55; char ch[maxn]; int map[maxn][maxn]; int n; int check(int i,int j,int d) { if(d==1)//判断左右方向上 {if(map[i][j-1]==0&&map[i][j+1]==0) return 1; return 0; } if(d==0)//上下方向 {if(map[i-1][j]==0&&map[i+1][j]==0) return 1; return 0; } } int find(int i,int j) { //printf("(I%d,J%d) ",i,j); int left=0,right=0,above=0,below=0; int k=j; while(k--,map[i][k]&&k>=1){left++;}//往左边历遍 if(left==0) return 0; k=j; // printf("left==%d ",j-left); while(k++,map[i][k]&&k<=n){right++;}//右边 if(right==0||right!=left)//要求边长大于3且左右对称 return 0; k=i; // printf("right==%d ",right+j); while(k--,map[k][j]&&k>=1){above++;}//上 if(above==0||above!=left) return 0; k=i; //printf("above==%d ",i-above); while(k++,map[k][j]&&k<=n){below++;}//下 if(below==0||below!=above) return 0; //printf("below==%d\n",below+i); for(int x=j-left;x<=j+right;x++)//横 { if(x==j) continue; if(!check(i,x,0)) return 0; } for(int y=i-above;y<=i+below;y++)//纵向 { if(y==i) continue; if(!check(y,j,1)) return 0; } return 1; } int main() { int i,j; while(scanf("%d",&n)!=EOF&&n!=0) { memset(map,0,sizeof(map)); for(i=1;i<=n;i++) {scanf("%s",ch); for(j=0;j<n;j++) {if(ch[j]=='#') map[i][j+1]=1; } } int ans=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(map[i][j]==1&&find(i,j))ans++;//此时以(i,j)为中心向各个方向历遍 printf("%d\n",ans); } return 0; }