hdu4414(DFS 找十字架数量)
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.
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
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
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 找十字架数量)的更多相关文章
- CodeForces - 103B(思维+dfs找环)
题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...
- # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)
「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- dfs找負環
某些無良出題人可能會卡bfs找負環,所以要用dfs 核心代碼(以jzoj5173為例): #include<bits/stdc++.h> using namespace std; #def ...
- leetcode-200-岛屿的个数(dfs找所有的连通分量)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- 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 ...
- HDU 4665 Unshuffle DFS找一个可行解
每层找一对相等的整数,分别放在两个不同的串中. 参考了学弟的解法,果断觉得自己老了…… #include <cstdio> #include <cstring> #includ ...
- 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 ...
- HDU 1242 dFS 找目标最短路
//多个起点,要最短得目标,不妨倒过来从目标出发,去找最近的点更新!!!!!!递归时思路要清楚 #include<iostream> #include<cstring> usi ...
随机推荐
- HDU 1042 N! 參考代码
HDU 1042 N! 题意:给定整数N(0 ≤ N ≤ 10000), 求 N! (题目链接) #include <iostream> using namespace std; //每一 ...
- 前端学习笔记(zepto或jquery)——对li标签的相关操作(三)
对li标签的相关操作——八种方式遍历li标签并获取其值 $("ul>li").forEach(function(item,index){ alert(index+" ...
- 教你发布Silverlight Bussiness Application(SQL Server 登录,局域网访问,以及使用ArcGIS Server服务需要注意的问题)
原文:教你发布Silverlight Bussiness Application(SQL Server 登录,局域网访问,以及使用ArcGIS Server服务需要注意的问题) 之前发布过Silver ...
- lua for通过循环table一些差异
有两个著名的是:ipairs和pairs,双方都认为,我们都非常熟悉的.其中ipairs刮(idx=1)从明年序遍历,经验nil那退出循环:和pairs遍历,仅仅要里面有值都能够遍历的到. 那假如我须 ...
- JavaScript 奇技淫巧
JavaScript 奇技淫巧 这里记录一下以前学习各种书籍和文章里边出现的JS的小技巧,分享给大家,也供自己查阅,同时感谢那些发现创造和分享这些技巧的前辈和大牛们. 1.遍历一个obj的属性到数组 ...
- Android L下载
Android L千呼万唤最终出来了,那么我们先下载下来一睹为快,那么怎么去拿到最新的L的分支 那依照傻瓜步骤总结下(Linux Ubuntu) 1.获取repo文件 (1).curl https:/ ...
- [CLR via C#]1.6 Framework类库~1.9与非托管代码的互操作性
原文:[CLR via C#]1.6 Framework类库~1.9与非托管代码的互操作性 1.6 Framework类库 1. .NET Framework中包含了Framework类库(Frame ...
- 基于 自己定义注解 和 aop 实现使用memcache 对数据库的缓存 演示样例
好久没更新blog了,在新公司打拼了两个月,每天都从早忙到晚,学到了非常多东西,可是没有时间来更新blog了.... 以下開始解说这次的主题 公司老大让我研究 ocs 就是阿里云的 开放缓存服务 点击 ...
- C语言库函数大全及应用实例五
原文:C语言库函数大全及应用实例五 [编程资料]C语言库函数大全及应用实例五 函数名: getcurdi ...
- Installshield脚本拷贝文件常见问题汇总
原文:Installshield脚本拷贝文件常见问题汇总 很多朋友经常来问:为什么我用CopyFile/XCopyFile函数拷贝文件无效?引起这种情况的原因有很多,今天略微总结了一下,欢迎各位朋友跟 ...