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
 

题意:找出十字架个数

思路:列举十字架的中点。dfs时传进去方向,方便推断十字架周围是不是有#(这是不合格的)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 55 int ans,le,ri,up,down,temp;
int n,flag;
int step[4][2]={-1,0,1,0,0,-1,0,1};//上,下,左,右 char a[N][N]; int judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<n)
return 1;
return 0;
} void dfs(int x,int y,int i)
{
temp++;
int j;
if(i<2) j=2;
else
j=0; int xx=x+step[i][0];
int yy=y+step[i][1]; if(!judge(xx,yy)) return ; if(a[xx][yy]=='o') return ; int xup=xx+step[j][0];
int ydn=yy+step[j][1]; if(judge(xup,ydn)&&a[xup][ydn]=='#')
{
flag=1;
return ;
}
xup=xx+step[j+1][0];
ydn=yy+step[j+1][1];
if(judge(xup,ydn)&&a[xup][ydn]=='#')
{
flag=1;
return ;
}
dfs(xx,yy,i);
} int main()
{
int i,j;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%s",a[i]); ans=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]=='#')
{
temp=0;
flag=0;
dfs(i,j,0);
up=temp;
temp=0;
if(flag) continue;
dfs(i,j,1);
down=temp;
temp=0; if(flag) continue;
dfs(i,j,2);
le=temp;
temp=0; if(flag) continue;
dfs(i,j,3); ri=temp;
temp=0; if(flag) continue; if(le==ri&&down==up&&le!=1&&up!=1) //左右相等。上下相等,不能是一条线
ans++;
} printf("%d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU 4414 Finding crosses(dfs)的更多相关文章

  1. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  2. hdu 4414 Finding crosses

    题目链接:hdu 4414 其实是一道简单的字符型水题,不涉及任何算法,可比赛时却没能做出来,这几天的状态都差到家了... 题目大意是求有多少个满足条件的十字架,十字架的边不能有分叉路口,所以枚举每个 ...

  3. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  4. hdu 1716 排序2(dfs)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. HDU 4414 Finding crosses (DFS + BFS)

    题意:在N*N的图中,找出孤立存在的十字架的个数.十字架要求为正十字,孤立表示组成十字架的‘#的周围的一格再无’#‘. dfs找出在中心的‘#’(周围四格也为‘#'),则缩小了搜索范围,再bfs找出是 ...

  6. hdu 2660 Accepted Necklace(dfs)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  7. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  8. HDU 6351 Beautiful Now(DFS)多校题解

    思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...

  9. HDU 5952 Counting Cliques(dfs)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 可直接复制粘贴的boostrap图标库网址

    1:http://fontawesome.dashgame.com/ 2:http://www.kuiyu.net/art-34.html 3:http://www.bootcss.com/p/fon ...

  2. hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...

  3. 线程堆栈大小 pthread_attr_setstacksize 的使用

    pthread_create 创建线程时,若不指定分配堆栈大小,系统会分配默认值,查看默认值方法如下: # ulimit -s8192# 上述表示为8M:单位为KB. 也可以通过# ulimit -a ...

  4. Android系统开发(1)——GCC编译器的编译和安装过程

    GCC编译器介绍 GCC编译器(GNG C Compiler)是GNU项目中符合ANSI C标准的编译系统,能够编译C  C++  Object C等语言编写的程序,同时GCC也是一个交叉编译器,特别 ...

  5. 数据类型总结——null和undefined

    相关文章 简书原文:https://www.jianshu.com/p/c3e252efe848 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...

  6. 【45.65%】【codeforces 560B】Gerald is into Art

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. QT代理Delegates使用实例(三种代理控件)

    效果如下,在表格的单元格中插入控件,用Delegates方式实现 源代码如下: main.cpp文件 #include <QApplication>#include <QStanda ...

  8. Android 监听软键盘按键的三种方式

    前言: 我们在Android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“Go”按键加载url页面:在点击搜索框的时候,点击右下角的searc ...

  9. js对时间的操作相关

    摘自网络,我主要用了日期增加若干天之后获得的日期,就是现在是5月2号,我增加30天,应该返回6月几号来着,就是这个意思 用到了Date.prototype.DateAdd 方法,prototype的意 ...

  10. iOS云存储:CloudKit 基本使用教程 增删改查(Swift)

    一.从iOS8开始,苹果为开发者提供了ClouKit,可以把我们的应用程序和用户数据存储在iCloud上,用于代替后台服务器,开发移动代码即可. 二.设置 (1)需要一个开发者账号,并且设置一个bun ...