#include<iostream>                  //用栈进行的解决;
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std; const int maxn = ; int room_map[maxn][maxn];
bool color[maxn][maxn]; int RoomArea;
int maxRoomArea;
int RoomNum; struct room
{
int r, c;
room(int rr, int cc):r(rr),c(cc) {};
}; void bfs(int r,int c)
{
stack<room> stk;
stk.push(room(r, c));
while (!stk.empty())
{
room rm = stk.top();
int x = rm.r;
int y = rm.c;
if (color[x][y])
stk.pop();
else
{
RoomArea++;
color[x][y] = true;
if ((room_map[x][y] & ) == ) stk.push(room(x, y - )); //向西;
if ((room_map[x][y] & ) == ) stk.push(room(x - , y)); // 向北;
if ((room_map[x][y] & ) == ) stk.push(room(x, y + )); //向东;
if ((room_map[x][y] & ) == ) stk.push(room(x + , y)); //向南;
}
}
}
int main()
{
int r, c;
while (~scanf("%d%d", &r, &c))
{
for (int i = ; i <= r; i++)
for (int j = ; j <= c; j++)
scanf("%d", &room_map[i][j]);
memset(color, false, sizeof(color));
maxRoomArea = ;
RoomNum = ;
for (int i = ; i <= r; i++)
for (int j = ; j <= c; j++)
{
if (!color[i][j])
{
RoomNum++;
RoomArea = ;
bfs(i, j);
maxRoomArea = max(maxRoomArea, RoomArea);
}
}
printf("%d\n", RoomNum);
printf("%d\n", maxRoomArea);
}
return ;
}
#include<algorithm>
#include<cstring>
#include<cstdio> //用栈;
using namespace std; const int maxn = ;
int roommap[maxn][maxn]; //城堡地图;
int color[maxn][maxn]; //房间染色; int room_num, room_area ;
int maxroom_aera; void bfs(int x, int y)
{
if (color[x][y]) return ;
room_area++;
color[x][y] = room_num; //给访问过的房间染色; if ((roommap[x][y] & ) == ) bfs(x, y - ); //向西;
if ((roommap[x][y] & ) == ) bfs(x - , y); //向北;
if ((roommap[x][y] & ) == ) bfs(x, y + ); //向东;
if ((roommap[x][y] & ) == ) bfs(x + , y); //向南;
} int main()
{
void bfs(int x, int y);
int row, cross;
while (~scanf("%d%d", &row,&cross))
{
memset(color, , sizeof(color));
maxroom_aera = ;
room_area = ;
room_num = ;
for (int i = ; i <= row; i++)
{
for (int j = ; j <= cross; j++)
{
scanf("%d", &roommap[i][j]);
}
} for (int i = ; i <= row; i++)
{
for (int j = ; j <= cross; j++)
{
if (!color[i][j])
{
room_num++;
room_area = ;
bfs(i, j);
maxroom_aera = max(maxroom_aera, room_area);
}
}
}
printf("%d\n", room_num);
printf("%d\n", maxroom_aera);
}
return ;
}

poj 1164 深度优先搜索模板题的更多相关文章

  1. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  2. POJ 3009 深度优先搜索

    问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不能将冰球往那个方向打.冰球出界就当输,超过10次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...

  3. POJ 3041 匈牙利算法模板题

    一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...

  4. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  5. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  6. POJ 2823 Sliding Window​ (模板题)【单调队列】

    <题目链接> <转载于>>> > 题目大意: 给你一段序列和一个长为k的窗口,这个窗口从最左边逐渐向右滑,直到滑到最右边,问你,该窗口在滑动的过程中,最大值和 ...

  7. POJ 3348 Cows | 凸包模板题

    题目: 给几个点,用绳子圈出最大的面积养牛,输出最大面积/50 题解: Graham凸包算法的模板题 下面给出做法 1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上) 2.其他点进 ...

  8. POJ 3041 Asteroids(二分图模板题)

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  9. POJ 1088 滑雪(模板题 DFS+记忆化)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

随机推荐

  1. mac使用minikube

    brew install kubectl 需要加代理! minikube start --vm-driver=xhyve --docker-env HTTP_PROXY=http://your-htt ...

  2. Mysql优化-大数据量下的分页策略

    一.前言 通常,我们分页时怎么实现呢? 1 SELECT * FROM table ORDER BY id LIMIT 1000, 10; 但是,数据量猛增以后呢? 1 SELECT * FROM t ...

  3. WebStorm Chinese Language Pack(中文语言包

    https://github.com/ewen0930/WebStorm-Chinese http://ewen0930.github.io/2016/04/webstorm-chinese-lang ...

  4. 框架源码系列十一:事务管理(Spring事务管理的特点、事务概念学习、Spring事务使用学习、Spring事务管理API学习、Spring事务源码学习)

    一.Spring事务管理的特点 Spring框架为事务管理提供一套统一的抽象,带来的好处有:1. 跨不同事务API的统一的编程模型,无论你使用的是jdbc.jta.jpa.hibernate.2. 支 ...

  5. Ubuntu下安装open-falcon-v0.2.1

    在Ubuntu下安装open-falcon和Centos下安装的方法有点区别,因为Ubuntu使用的包管理器是apt-get,而Centos下使用的是Yum,建议不要再Ubuntu下使用yum 建议自 ...

  6. Hive学习笔记——安装和内部表CRUD

    1.首先需要安装Hadoop和Hive 安装的时候参考 http://blog.csdn.net/jdplus/article/details/46493553 安装的版本是apache-hive-2 ...

  7. 21 go并发编程-下

    如何等待一组goroutine结束 1. 使用不带缓冲区的channel实现. 原理: 每个goroutine都往一个channel里写入一个值,然后我们去遍历这个管道的数值,由于不带缓冲区,那么必须 ...

  8. 手机访问PC网站自动跳转到手机版

    随着智能手机的流行,4G时代来临,手机用户越来越多,在生活中甚至手机比电脑用的还多,当前开发的网站大都是PC和WAP版并存,但是很少有用户愿意去记住一个网站的两个端的不同域名,所以需要我们做一些设置, ...

  9. 写在开始前---ajax中的会话过期与重新登录

    一般情况下,点击<a>链接或浏览器输入url时,请求到后端,服务器判断会话是否过期.过期,重定向到登录页,或返回登录页的页面.在ajax中,返回重定向无效,这个时候就需要自己在ajax的逻 ...

  10. 盘点 React 16.0 ~ 16.5 主要更新及其应用

    目录 0. 生命周期函数的更新 1. 全新的 Content API 2. React Strict Mode 3. Portal 4. Refs 5. Fragment 6. 其他 7. 总结 生命 ...