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
#include<stdio.h>
int n,sk,hk,s,h,stakH[1000],stakS[1000],right,lift,up,dow,zxloct;
char map[105][105];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; void DFS(int i,int j)
{
int e;
map[i][j]='o';
if(stakS[s]==j)
dow++;
for(e=0;e<4;e++)
if(e<2)
{
if(i+dir[e][0]>=0&&i+dir[e][0]<n&&j>=0&&j<n)
if(map[i+dir[e][0]][j]=='#')
{
sk++;
if(j!=stakS[s])
stakS[++s]=j;
DFS(i+dir[e][0],j+dir[e][1]);
}
}
else
{
if(i>=0&&i<n&&j+dir[e][1]>=0&&j+dir[e][1]<n)
if(map[i][j+dir[e][1]]=='#')
{
hk++;
if(e==2)
right++;
else
lift++;
if(i!=stakH[h])
stakH[++h]=i;
if(zxloct==-1)
{
zxloct=i; dow--;
} DFS(i,j+dir[e][1]);
}
}
if(zxloct==-1)
{
dow--;up++;
}
}
int main()
{
int i,j,k;
while(scanf("%d",&n)==1&&n)
{
k=0;getchar();
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
getchar();
}
stakH[0]=-1;
stakS[0]=-1;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(map[i][j]=='#')
{
hk=s=h=0;
right=lift=0;
up=dow=0;
zxloct=-1;
sk=1;stakS[++s]=j;
DFS(i,j);
if(s==1&&h==1&&sk%2==1&&hk%2==0&&hk>0&&sk>1&&right==lift&&up==dow&&lift!=0&&up!=0)
k++;
} printf("%d\n",k);
}
}

hdu4414(DFS 找十字架数量)的更多相关文章

  1. CodeForces - 103B(思维+dfs找环)

    题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...

  2. # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)

    「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...

  3. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  4. dfs找負環

    某些無良出題人可能會卡bfs找負環,所以要用dfs 核心代碼(以jzoj5173為例): #include<bits/stdc++.h> using namespace std; #def ...

  5. leetcode-200-岛屿的个数(dfs找所有的连通分量)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  6. Codeforces Beta Round #87 (Div. 2 Only)-Party(DFS找树的深度)

    A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exa ...

  7. HDU 4665 Unshuffle DFS找一个可行解

    每层找一对相等的整数,分别放在两个不同的串中. 参考了学弟的解法,果断觉得自己老了…… #include <cstdio> #include <cstring> #includ ...

  8. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  9. HDU 1242 dFS 找目标最短路

    //多个起点,要最短得目标,不妨倒过来从目标出发,去找最近的点更新!!!!!!递归时思路要清楚 #include<iostream> #include<cstring> usi ...

随机推荐

  1. Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading

    Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...

  2. GitBook配置

    GitBook 是一个通过 Git 和 Markdown 来撰写书籍的工具.生成格式有:JSON.ePub.PDF.Website ! ================================ ...

  3. 安裝 Rails 開發環境

    安裝 Rails 開發環境 Give someone a program, you frustrate them for a day; teach them how to program, you f ...

  4. 砸金蛋:jQuery+PHP实现的砸金蛋中奖程序

    原文 砸金蛋:jQuery+PHP实现的砸金蛋中奖程序 砸金蛋被广泛应用于庆典活动.商家促销.电视娱乐等场合,它的趣味.悬念能迅速活跃现场气氛.同样,我们也可以将砸金蛋应用到WEB网站上,用于开展线上 ...

  5. java中string和int互相转化

    1 怎样将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  6. Python超级明星WEB框架Flask

    Flask简介 Flask是一个相对于Django而言轻量级的Web框架. 和Django大包大揽不同,Flask建立于一系列的开源软件包之上,这其中 最主要的是WSGI应用开发库Werkzeug和模 ...

  7. 吞吐量(Throughput)、QPS、并发数、响应时间(RT)对系统性能的影响

    首先对吞吐量().QPS.并发数.响应时间(RT)几个概念一直比较模糊,也不知道哪些指标可以较好的衡量系统的性能.今天特意查了些资料做一些记录:首先看一些概念(来自百度百科) 1. 响应时间(RT) ...

  8. Linux开源模块迁移概述暨交叉编译跨平台移植总结--从《嵌入式Linux驱动模板简洁和工程实践》

    本文摘录<嵌入式Linux驱动模板简洁和工程实践>一本书"开发和调试技术". Linux强大的是,有那么多的开源项目可以使用.通常非常需要可以通过寻找相关的源模块被定义 ...

  9. Android单元测试Junit (一)

    1.在eclips中建立一个Android工程,具体信息如下: 2.配置单元测试环境,打开AndroidManifest.xml,具体代码如下所示: <?xml version="1. ...

  10. QML Image得到的图片资源路径的详细信息

    最近又开始了Qt5.在学习QML当地的资源总是越来越留念类似 " QML Image: Cannot open: qrc:///images/Blue hills.jpg "的错误 ...