题意:在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. Problem 1010 - 素数环问题

    #include<iostream> #include<string> #include<algorithm> #include<vector> #in ...

  2. 网络基础---OSI 模型与TCP/IP

    一.网络的演进: 1.简单的联接:1960's ------------ 1970's    Host Network 六十至七十年代,网络的概念主要是主机架构的低速串行联接,提供应用程序执行.远程打 ...

  3. Swift 中的getting和setter的使用

    以下简单的介绍Swift中的getting和setting的使用方法: Xcode version: 6.1 先附代码: class Test { var num1: Double = 0.0 ini ...

  4. MYSQL常用命令集合(转载)

    文章出处:http://www.cnblogs.com/q1ng/p/4474501.html 1.导出整个数据库mysqldump -u 用户名 -p --default-character-set ...

  5. JavaScript 中常用的 正则表达式

    这编文章我来整理了一些在 javascript 中常用的正则式希望能给大家带来一些开发的灵感 //校验是否全由数字组成 function isDigit(s) { var patrn=/^[0-9]{ ...

  6. jquery自动切换tabs选项卡

    <%@ page language="java" contentType="text/html; charset=utf-8"    pageEncodi ...

  7. js获取时间天数

    date2必须大于date1 function getDays(date1,date2){ /*获取之间的天数*/ /*date1,date2都是date格式*/ var getd=(date2.ge ...

  8. TCP/IP笔记 应用层(1)——DNS

    1. DNS DNS(Domain Name System ):域名系统,是因特网的一项核心服务,它作为可以将域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网,而不用去记住能够被机 ...

  9. iOS: XCode6 beta 6 错误

    在使用XCode6 Beta6时, 遇到"__TFSs15_arrayForceCastU___FGSaQ__GSaQ0__"错误: 在http://stackoverflow.c ...

  10. 《Braid》碎片式台词

    谁见到过风? 你没有,我也没有. 但当树儿低下头, 便是风儿经过时. 便是风儿穿过的时候. 但当树叶微微摇首, 你没有,我也没有. 谁见到过风? 二.时间与宽恕 1.提姆要出发了!他要去寻找并救出公主 ...