HDU-4414 Finding crosses

问题描述:

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.

输入说明:

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

输出说明:

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

SAMPLEOUTPUT:

1
0
0
0

思路:

题目就是让我们寻找输入的矩阵有多少由*组成的正十字架。想法的具体做法在代码注释中

扫描二维码关注公众号,回复: 13059091 查看本文章

AC代码:

#include <bits/stdc++.h>
using namespace std;
char a[110][110];
int dfs(int i,int j)
{
    
    
    int q=0,b=0,c=0,d=0,m;
    for(int m=1;a[i-m][j]=='#';m++)//从当前的#位置,开始向下寻找#,最后保存最下点到中间的距离
    {
    
    if(a[i-m][j+1]=='#'||a[i-m][j-1]=='#')//由题意,我们需要寻找的十字架周围不能有其他的#号,在这一向下寻找的过程中,如果出现左右有#出现的,那么直接返回0,表示不可能存在以a[i][j]处的#为中心的十字架。
            return 0;
        else q=m;
    }
    for(int m=1;a[i+m][j]=='#'; m++)//操作和思路和上面的一样,从当前位置开始,向上寻找#,最后保存最上点到中间的距离
    {
    
    
        if(a[i+m][j+1]=='#'||a[i+m][j-1]=='#')
            return 0;
        else b=m;
    }
    for(int m=1; a[i][j-m]=='#';m++)//一样的操作,但是这边是从当前位置向左,保存最左端到中间的距离
    {
    
    
        if(a[i-1][j-m]=='#'||a[i+1][j-m]=='#')
            return 0;
        else c=m;
    }
    for(int m=1;a[i][j+m]=='#';m++)//如法炮制,保存最右端到中间的距离
    {
    
    
        if(a[i-1][j+m]=='#'||a[i+1][j+m]=='#')
            return 0;
        else d=m;
    }
    if(q==b&&q==c&&q==d&&q)//因为题目是询问是否存在正十字,因此四段距离都应相等而且不为0,进行一个判断
        return 1;
    else return 0;
}
int main()
{
    
    
    int n;
    while(scanf("%d",&n)&&n)
    {
    
    
        int ans=0;
        memset(a,'\0',sizeof(a));//因为多组输入的缘故,要进行清零操作
        for(int i=1;i<=n;i++)
        {
    
    
            for(int j=1;j<=n;j++)
            {
    
    
                cin>>a[i][j];
            }
        }
        for(int i=0;i<=n+1;i++)//给输入的数据加上一个‘o’的框架,以方便在之后的判断中能终止寻找
        {
    
    
            a[i][0]=a[0][i]=a[n+1][i]=a[i][n+1]='o';
        }
        for(int i=1; i<=n; i++)
        {
    
    
            for(int j=1; j<=n; j++)
            {
    
    
                if(a[i][j]=='#')//对于每一个是#的位置都去搜索以这个点为中心是否存在十字架
                {
    
    
                    ans+=dfs(i,j);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/m0_51727949/article/details/115347957