题意:在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. C# 推送信息到APNs

    iPhone消息推送机制实现与探讨 class Program     {           public static DateTime? Expiration { get; set; }     ...

  2. 求两个数的最大公约数(Euclid算法)

    求两个数 p 和 q 的最大公约数(greatest common divisor,gcd),利用性质 如果 p > q, p 和 q 的最大公约数 = q 和 (p % q)的最大公约数. 证 ...

  3. LATEX学习笔记1

    LATEX源文件的结构分三大部分,依次为:文档类声明.序言(可选).正文. 文档结构 \documentclass{article} \usepackage{amsmath} \usepackage{ ...

  4. boost::xml————又一次失败的尝试

    尝试使用wptree来进行xml解析,又一次失败了,可以正常读取正常输出,但是使用wptree进行节点读取失败(乱码) 请看源码: DealXml.h #pragma once #include &l ...

  5. windows phone 之手势识别(Manipulation)

    在Windows Phone 7的多触摸屏上可以检测到至少四根同时存在的手指,并且一起操作使触摸屏充分发挥效果. 在silverlight开发中通过事件来实现触屏事件的检测,包括低级别的和高级别的接口 ...

  6. CentOS 6.4 64位 安装 mysql 5.6.24

    下载安装包 由于官网访问及版本选择下载不太方便,使用 suho 的源进行下载 http://mirrors.sohu.com/mysql/MySQL-5.6/ 下载如下三个安装包: MySQL-ser ...

  7. DOM对象与Jquery对象区别

  8. Apache 支持.htaccess

    ******************************************************************************* Apache 服务器 ********* ...

  9. How to install Pygame for Python 3.4 on Ubuntu 14.04(转)

    First run this to install dependencies: sudo apt-get install mercurial python3-dev python3-numpy \ l ...

  10. ASP.NET MVC轻教程 Step By Step 6——改进表单

    上一节我们使用原始的HTML表单来完成留言功能,但是ASP.NET MVC提供了丰富的HTML辅助方法来帮助我们构建更简洁优雅的表单. Step 1. 修改Form标签 首先,我们可以使用Html.B ...