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 ...
随机推荐
- crawler_网络爬虫中编码的正确处理与乱码的解决策略
转载: http://hi.baidu.com/erliang20088/item/9156132bdaeae8949c63d134 最近一个月一直在对nutch1.6版进行中等层次的二次开发,本来是 ...
- python_在windows下安装配置python开发环境及Ulipad开发工具
最近开始学习Python,在网上寻找一下比较好的IDE.因为以前用C#做开发的,用Visual Studio作为IDE,鉴于用惯了VS这么强大的IDE,所以对IDE有一定的依赖性. Python的ID ...
- 《剑指offer》 相应 在线测试地址
<剑指Offer>面试题集收录汇总 面试题1 赋值运算符函数 不适合在线模式 面试题2 实现Singleton模式 不适合在线模式 面试题3 二维数组中的查找 已收录 面试题4 替换空格 ...
- ACM经典算法之字符串处理:字符串替换
语法:replace(char str[],char key[],char swap[]); 參数: str[]:在此源字符串进行替换操作 key[]:被替换的字符串,不能为空串 swap[]:替换的 ...
- C#中WebBrowser的使用
原文:C#中WebBrowser的使用 其实网络上这类文章很多,我大致从头说说我自己的经验. 1.加入引用 在控件栏按右键,选择“添加/移除项”,选COM组件,选中“Microsoft Web Bro ...
- struts2的Action该方法不能去
最近做的一个特征,使用struts2,input标签内容,并与相应的内容背景的实体类,当提交方法,无法进入action该方法和程序没有报错被,检验N通方法还没有找到,查找终于找到了,它是input标签 ...
- bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (三) 图片裁剪
官方的例子 是 长方形的. 我这里 用于 正方形的头像 所以 做如下 修改 #preview-pane .preview-container { width: 73px; height: 73px ...
- 用Iconv应对NodeJs对称加密技术在汉字编码与NoSQL的一些坑洞
·起因 汉字编码技术在实际应用中总是会存在这样或者那样的问题,尤其是在一些热门NoSQL方面多少会遇到挑战.比方说Cassandra字符集还不直接支持GB2312,要想存储写汉字那可真是麻烦.当然这还 ...
- UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)
在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位And ...
- oracle 存储过程的基本语法
原文:oracle 存储过程的基本语法 1.基本结构 CREATE OR REPLACE PROCEDURE 存储过程名字( 参数1 IN NUMBER, 参数2 IN NUMBER) I ...