题意:在N*N的图中,找出孤立存在的十字架的个数。十字架要求为正十字,孤立表示组成十字架的‘#的周围的一格再无’#‘。

dfs找出在中心的‘#’(周围四格也为‘#'),则缩小了搜索范围,再bfs找出是否是符合要求。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
char map[55][55];
int n,cnt,head,tail,vis[55][55],center[55][55];
int dirx[4] = {1,-1,0,0};
int diry[4] = {0,0,1,-1}; struct Queue {
int x,y;
} q[11111]; bool go(int x,int y) {
if(x < 0 || x >=n || y < 0 || y >= n) return false;
if(vis[x][y]) return false;
if(map[x][y] != '#') return false;
return true;
}
void dfs(int x,int y) {
int xx,yy;
int i,p = 0;
for(i=0; i<4; i++) {
xx = x + dirx[i];
yy = y + diry[i];
if(go(xx,yy)) {
p ++ ;
vis[xx][yy] = 1;
dfs(xx,yy);
}
}
if(p == 3) {
center[x][y] = 1;
}
} void bfs(int x,int y) {
head = 0;
tail = 0;
q[head].x = x;
q[head++].y = y;
vis[x][y] = 1;
int cut = 4; //dfs计算后,确保至少长度为三的十字架
while(head != tail) {
Queue t = q[tail++];
Queue tt;
int num = 0;
for(int i=0; i<4; i++) { //下 上 右 左
tt.x = t.x + dirx[i];
tt.y = t.y + diry[i];
if(go(tt.x,tt.y)) {
num++;
vis[tt.x][tt.y] = 1;
q[head++] = tt;
}
}
if(num == 1) cut ++;
}
cut = cut / 4; // 层数
if(cut * 4 + 1 == head) cnt ++;
} int main() {
while(cin >> n && n) {
cnt = 0;
memset(center,0,sizeof(center));
for(int i=0; i<n; i++) {
scanf("%s",map[i]);
}
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j ++) {
if(map[i][j] == '#' && vis[i][j] == 0) {
vis[i][j] = 1;
dfs(i,j);
}
}
}
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j ++) {
if(center[i][j] == 1 && vis[i][j] == 0) {
memset(q,0,sizeof(q));
bfs(i,j);
}
}
}
printf("%d\n",cnt);
}
return 0;
}

HDU 4414 Finding crosses (DFS + BFS)的更多相关文章

  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. HDU 4414 Finding crosses(dfs)

    Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...

  4. HDOJ 4414 Finding crosses 暴力!

    Finding crosses Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  7. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  8. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  9. HDU 1401 Solitaire 双向DFS

    HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...

随机推荐

  1. iOS:iOS开发中用户密码保存位置

    原文来自简书:http://www.jianshu.com/p/4af3b8179136/comments/1294203 如果要实现自动登录,不必每次打开应用都去登录,我们势必要把密码保存到本地.一 ...

  2. 设置lable内容不上下居中

    转载自:http://dong-zsh.github.io/2015/10/14/%E8%AE%BE%E7%BD%AElable%E5%86%85%E5%AE%B9%E4%B8%8D%E4%B8%8A ...

  3. Eclipse Git和sourceTree用法

    Eclipse Git和sourceTree用法 Eclipse Git: 提交代码到git: 1.team->Repository->pull 若没有冲突: 2.team->com ...

  4. 百度地图Api 根据两个坐标点计算距离

    百度地图Android Sdk的Api里面,没有现成的直接获取两个坐标点之间距离的方法,但是,在jsapi里面,有直接计算距离的方法. class Point: pass def max(a,b): ...

  5. jQuery开发技术笔记

    HTML DOM 加载步骤    1. 解析 HTML 结构     2.加载外部脚本和样式表文件     3.解析并执行脚本代码     4.构造 HTML DOM 模型     5.加载图片等外部 ...

  6. Android Camera拍照 压缩

    http://www.linuxidc.com/Linux/2014-12/110924.htm package com.klp.demo_025; import java.io.ByteArrayI ...

  7. PYTHON开发--面向对象基础入门

    面向对象 一:面向对象初级 1.思考:首先在python中,以前我们以前用到的几乎都是函数式编程,但是有时候函数式编程其中代码重复利用率太高,我们往往会把这些重复代码写进一个函数日后去调用,所以呢,今 ...

  8. 在Swift中使用libxml2

    // // main.swift // C150805_libxml2r2 // http://git.oschina.net/yao_yu/Swift2015/tree/master/C150805 ...

  9. WPF 分页控件 WPF 多线程 BackgroundWorker

    WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...

  10. 学习Swift -- 数组(Array) - 持续更新

    集合类型--数组 Array是Swift中的一种集合类型:数组,数组是使用有序列表储存同一类型的多个值,与OC的NSArray的最大不同是,Swift的数组是值类型,OC的数组是引用类型 声明数组的方 ...