HDU 4414 Finding crosses (DFS + BFS)
题意:在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)的更多相关文章
- hdu 4414 Finding crosses【简单模拟】
		
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...
 - hdu 4414 Finding crosses
		
题目链接:hdu 4414 其实是一道简单的字符型水题,不涉及任何算法,可比赛时却没能做出来,这几天的状态都差到家了... 题目大意是求有多少个满足条件的十字架,十字架的边不能有分叉路口,所以枚举每个 ...
 - HDU 4414 Finding crosses(dfs)
		
Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...
 - HDOJ 4414 Finding crosses 暴力!
		
Finding crosses Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
 - HDU 1241 Oil Deposits (DFS/BFS)
		
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
 - HDU 1728 逃离迷宫(DFS||BFS)
		
逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
 - HDU 4771 (DFS+BFS)
		
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
 - ID(dfs+bfs)-hdu-4127-Flood-it!
		
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
 - HDU 1401 Solitaire 双向DFS
		
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
 
随机推荐
- BearSkill实用方法之UITextField限制输入的字符数量
			
原文:http://blog.csdn.net/xiongbaoxr/article/details/51525061
 - Windows Phone 之手势识别(Flick)
			
1. 引入dll (silverlight for wndows phone toolkit) 2.引入命名空间 01.xmlns:toolkit="clr-namespace:Micros ...
 - 【JQuery学习历程】1.初识JQuery
			
1.JQuery简介: JQuery是用js写的JavaScript库,是为了简化js对HTML元素的操作.实现动画效果并方便为网站提供ajax交互: 2.ready()方法: ready()方法和j ...
 - winfrom中按钮文本&的显示问题/按钮快捷键设置问题
			
其实这个问题是因为“&”有特殊的意义-就是可以作为快捷键 第一种:Alt + *(按钮快捷键) 在大家给button.label.menuStrip等控件设置Text属性时在名字后边加& ...
 - finalspeed服务器端和客户端安装
			
https://www.91yun.org/archives/2775 https://www.91yun.org/archives/615 1.首先安装服务器端:一键安装代码 wget -N --n ...
 - Linux网络编程-----Socket地址API
			
(1) 通用socket地址 socket网络编程接口中表示socket地址的是结构体sockaddr,其定义如下: #include<bits/socket.h> struct sock ...
 - winfrom拷贝文件
			
//File.Copy(@"C:\Users\Administrator\Pictures\bg.png", @"g:\images\bg.png", true ...
 - javascript日用代码集合(一)
			
获取url参数 function get_url_param(name){ var reg = new RegExp("(^|&)" + name + "=([^ ...
 - hadoop 各种counter 解读
			
http://blog.sina.com.cn/s/blog_61ef49250100uxwh.html 经过了两天的休息与放松,精神饱满了吧?上星期我们学习了MapReduce的过程,了解了其基本过 ...
 - List Of All Machine Learning Sorted By Citation
			
List Of All Machine Learning Sorted By Citation With > 300 citations 2013-10-10 See Citation Anal ...