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
  1. 4
  2. oo#o
  3. o###
  4. oo#o
  5. ooo#
  6. 4
  7. oo#o
  8. o###
  9. oo#o
  10. oo#o
  11. 5
  12. oo#oo
  13. oo#oo
  14. #####
  15. oo#oo
  16. oo##o
  17. 6
  18. ooo#oo
  19. ooo##o
  20. o#####
  21. ooo#oo
  22. ooo#oo
  23. oooooo
  24. 0
 
Sample Output
  1. 1
  2. 0
  3. 0
  4. 0
 
Source
 

题意:找出十字架个数

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. #include<vector>
  8.  
  9. #define L(x) (x<<1)
  10. #define R(x) (x<<1|1)
  11. #define MID(x,y) ((x+y)>>1)
  12. using namespace std;
  13. #define N 55
  14.  
  15. int ans,le,ri,up,down,temp;
  16. int n,flag;
  17. int step[4][2]={-1,0,1,0,0,-1,0,1};//上,下,左,右
  18.  
  19. char a[N][N];
  20.  
  21. int judge(int x,int y)
  22. {
  23. if(x>=0&&x<n&&y>=0&&y<n)
  24. return 1;
  25. return 0;
  26. }
  27.  
  28. void dfs(int x,int y,int i)
  29. {
  30. temp++;
  31. int j;
  32. if(i<2) j=2;
  33. else
  34. j=0;
  35.  
  36. int xx=x+step[i][0];
  37. int yy=y+step[i][1];
  38.  
  39. if(!judge(xx,yy)) return ;
  40.  
  41. if(a[xx][yy]=='o') return ;
  42.  
  43. int xup=xx+step[j][0];
  44. int ydn=yy+step[j][1];
  45.  
  46. if(judge(xup,ydn)&&a[xup][ydn]=='#')
  47. {
  48. flag=1;
  49. return ;
  50. }
  51. xup=xx+step[j+1][0];
  52. ydn=yy+step[j+1][1];
  53. if(judge(xup,ydn)&&a[xup][ydn]=='#')
  54. {
  55. flag=1;
  56. return ;
  57. }
  58. dfs(xx,yy,i);
  59. }
  60.  
  61. int main()
  62. {
  63. int i,j;
  64. while(scanf("%d",&n),n)
  65. {
  66. for(i=0;i<n;i++)
  67. scanf("%s",a[i]);
  68.  
  69. ans=0;
  70. for(i=0;i<n;i++)
  71. for(j=0;j<n;j++)
  72. if(a[i][j]=='#')
  73. {
  74. temp=0;
  75. flag=0;
  76. dfs(i,j,0);
  77. up=temp;
  78. temp=0;
  79. if(flag) continue;
  80. dfs(i,j,1);
  81. down=temp;
  82. temp=0;
  83.  
  84. if(flag) continue;
  85. dfs(i,j,2);
  86. le=temp;
  87. temp=0;
  88.  
  89. if(flag) continue;
  90. dfs(i,j,3);
  91.  
  92. ri=temp;
  93. temp=0;
  94.  
  95. if(flag) continue;
  96.  
  97. if(le==ri&&down==up&&le!=1&&up!=1) //左右相等。上下相等,不能是一条线
  98. ans++;
  99. }
  100.  
  101. printf("%d\n",ans);
  102. }
  103. return 0;
  104. }

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

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. GDB(十)--调试正在运行的进程

    我编写了一个循环: long i;    for (i = 0; i < 999999; i++) {        mt.a += 1;        sleep(1);    }把它编译成a ...

  2. GitHub的repository的相关操作

    原文地址 https://www.jianshu.com/p/038e8ba10e45 1.准备工作 a.有自己的GitHub账号(https://github.com/)b.在自己本地有安装git软 ...

  3. java开发中序列化与反序列化起到的作用

    基本概念: 序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象. 这两个过程结合起来,能够轻松地存储和数据传输. 特别在网络传输中,它的作用显得尤为重要.我们 ...

  4. 开发自己的PHP MVC框架(一)

    这个教程能够使大家掌握用mvc模式开发php应用的基本概念.此教程分为三个部分.如今这篇是第一部分. 如今市面上有非常多流行的框架供大家使用.可是我们也能够自己动手开发一个mvc框架.採用mvc模式能 ...

  5. [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function

    In this lesson, we'll filter a list of objects based on multiple conditions and we'll use Ramda's al ...

  6. Android开发:使用ViewDragHelper实现抽屉拉伸效果

    事实上,有非常多方法能够实现一个Layout的抽屉拉伸效果,最常常的方法就是自己定义一个ViewGroup,然后控制点击事件.控制移动之类的,这样的方法的代码量多,并且实现起来复杂,后期维护添加其它效 ...

  7. 5.7-GTID复制搭建

    基本环境   Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.56.157 ...

  8. MySql批量drop table

    原文:MySql批量drop table 今天发现数据库中很多没用的表,想清理掉. 发现mysql好像不支持类似这样的写法:drop table like "%r" 在oracle ...

  9. 手机端自适应布局demo

    原型如下: 要求如下:适应各种机型 源码如下: <!DOCTYPE html > <html>     <head>         <meta http-e ...

  10. HDoj-1874-畅通project续-Dijkstra算法

    畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...