题意:在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. 转: angularjs 指令中动态编译的方法(适用于有异步请求的情况) 内嵌指令无效

    angular的坑很多 例子: 在directive的link中有一个$http请求,当请求完成后根据返回的值动态做element.append('......');这个操作, 能显示没问题,可问题是 ...

  2. 中文翻译:pjsip教程(三)之ICE stream transport的使用

    1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...

  3. jQuery慢慢啃之ajax(九)

    1.jQuery.ajax(url,[settings])//通过 HTTP 请求加载远程数据 如果要处理$.ajax()得到的数据,则需要使用回调函数.beforeSend.error.dataFi ...

  4. 要想重启后也生效LINUX防火墙配置

    新配置的一台服务器,安装的是CentOS6.3系统,在安装完LNMP之后,发现nginx进程存在,且php解析正常,但是用分配的独立IP去访问的时候发现无法访问. 查了下网上的资料,发现可能是Linu ...

  5. JS字符串操作大全

    String对象属性 (1) length属性 length算是字符串中非常常用的一个属性了,它的功能是获取字符串的长度.当然需要注意的是js中的中文每个汉字也只代表一个字符,这里可能跟其他语言有些不 ...

  6. 经典SQL练习题

    题目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711 1. 查询Student表中的所有记录的Sname.Ssex和Class列 ...

  7. WPF RadioButton & CheckBox Style

    <Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...

  8. JQuery 字符串截取

    //字符串截取,全小写 strObj.substring(startIndex,endIndex); //需要注意大小写 strObj.lastIndexOf(String splitObj); // ...

  9. #Leet Code# Binary Tree Max[待精简]

    描述:递归调用,getMax返回 [节点值,经过节点左子节点的最大值,经过节点右节点的最大值],每次递归同时查看是否存在不经过节点的值大于max. 代码:待优化 def getLargeNode(se ...

  10. 【读书笔记】【CLR via C#】【第一章】The CLR’s Execution Model

    内容提要 本章的目的是对.Net 框架的设计做一个总体的介绍,包括介绍框架中使用的一些技术.定义一些术语.同时会展示从源代码生成应用程序(或者一些包含了一些自定义类型的可以发布的组件),并且会解释程序 ...