hdu 4414 Finding crosses 简单搜索

Finding crosses

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


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
 
这个题目是37届ACM网络预选赛杭州赛区的第5个题目,简单的搜索.思想就是找到十字架的中心,然后向两边扩展.
以前的版本是有边界判断函数的,但是由于已经用memset来把地图初始化为o,所以不需要这个函数了,所以在下面的代码中直接用不为字符#来判断所有情况.另外由于cin这个输入字符流消耗时间较大,所以改为scanf.最终0MS,再说一点.这个地图读的很郁闷呀.为什么scanf读单个字符用了很多方法都不行,后来果断%s.下面上代码.
 
#include
using namespace std;
char map[60][60];
int n;
int pdh(int x,int y)
{
    if (map[x-1][y]=='o'&&map[x+1][y]=='o')
        return 1;
    return 0;
}
int pds(int x,int y)
{
    if (map[x][y-1]=='o'&&map[x][y+1]=='o')
        return 1;
    return 0;
}
int pd2(int x,int y)
{
    if (map[x][y]!='#')
        return 1;
    return 0;
}
int pd(int x,int y)
{
    if (map[x][y]=='#')
        return 1;
    return 0;
}
int main()
{
    int i,j;
    int ans=0;
    int flag2;
    int flag;
    while(scanf("%d",&n),n)
    {
        memset(map,'o',sizeof(map));
        for (i=1;i<=n;i++)
        {
            scanf("%s",map[i]+1);
        }
        ans=0;
        for (i=2;i<=n-1;i++)
        {
            for (j=2;j<=n-1;j++)
            {
                if (map[i][j]=='#')
                {
                    flag=0;
                    flag2=0;
                    int x1,y1,x2,y2,x3,y3,x4,y4;
                    while(1)
                    {
                        flag++;
                        x1=i;y1=j-flag;
                        x2=i;y2=j+flag;
                        x3=i-flag;y3=j;
                        x4=i+flag;y4=j;
                        if (pd(x1,y1)&&pd(x2,y2)&&pd(x3,y3)&&pd(x4,y4))
                        {
                            if (pdh(x1,y1)&&pdh(x2,y2)&&pds(x3,y3)&&pds(x4,y4))
                                continue;
                            else
                            {
                                flag2=1;
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (flag2==0&&flag>=2&&pd2(x1,y1)&&pd2(x2,y2)&&pd2(x3,y3)&&pd2(x4,y4))
                        ans++;
                }
            }

        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(编程算法)