首页 > 代码库 > HDU 4414 枚举

HDU 4414 枚举

Finding crosses

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1501    Accepted Submission(s): 802


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
 
 
 
题目意思:
找出由‘#‘构成的十字架数目,题目要求十字架尽可能的大即一个十字架就是算一个而不算其包含的子十字架,十字架周围不能有其他的‘#’。
 
思路:
先把‘#‘的位置存下来,然后枚举每个‘#‘,看以这个‘#‘为中心能否构成满足条件的十字架,n最大为50,再加上剪枝,水水就过了。
 
 
代码:
 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8  9 #define N 5510 11 char map[N][N];12 int n;13 14 struct node{15     int x, y;16 }a[N*N];17 18 19 main()20 {21     int i, j, k;22     while(scanf("%d",&n)==1&&n){23         for(i=0;i<n;i++) scanf("%s",map[i]);24         k=0;25         for(i=0;i<n;i++){26             for(j=0;j<n;j++){27                 if(map[i][j]==#){28                     a[k].x=i;a[k].y=j;k++;29                 }30             }31         }32         int ans=0, f;33         for(i=0;i<k;i++){34             if(a[i].x==0||a[i].x==n-1||a[i].y==0||a[i].y==n-1) continue;35             if(map[a[i].x][a[i].y+1]!=#||map[a[i].x][a[i].y-1]!=#||map[a[i].x+1][a[i].y]!=#||map[a[i].x-1][a[i].y]!=#) continue;36             int len1, len2;37             int xx=a[i].x-1, yy=a[i].y;38             len1=len2=0;f=1;39             while(map[xx][yy]==#){//up40                 len1++;41                 if(map[xx][yy-1]==#||map[xx][yy+1]==#){42                     f=0;break;43                 }44                 xx--;45                 if(xx<0) break;46             }47             if(!f) continue;48             xx=a[i].x+1;49             while(map[xx][yy]==#){//down50                 len2++;51                 if(map[xx][yy-1]==#||map[xx][yy+1]==#){52                     f=0;break;53                 }54                 xx++;55                 if(xx>=n) break;56             }57             if(!f) continue;58             if(len1!=len2) continue;59             len2=0;60             xx=a[i].x;yy=a[i].y-1;61              while(map[xx][yy]==#){//left62                 len2++;63                 if(map[xx-1][yy]==#||map[xx+1][yy]==#){64                     f=0;break;65                 }66                 yy--;67                 if(yy<0) break;68             }69             if(!f) continue;70             if(len1!=len2) continue;71             len2=0;72             yy=a[i].y+1;73             while(map[xx][yy]==#){//right74                 len2++;75                 if(map[xx-1][yy]==#||map[xx+1][yy]==#){76                     f=0;break;77                 }78                 yy++;79                 if(yy>=n) break;80             }81             if(!f) continue;82             if(len1!=len2) continue;83             ans++;84         }85         printf("%d\n",ans);86     }87 }

 

HDU 4414 枚举