poj 1164 深度优先搜索模板题
#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 深度优先搜索模板题的更多相关文章
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ 3009 深度优先搜索
问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不能将冰球往那个方向打.冰球出界就当输,超过10次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
- POJ 2823 Sliding Window (模板题)【单调队列】
<题目链接> <转载于>>> > 题目大意: 给你一段序列和一个长为k的窗口,这个窗口从最左边逐渐向右滑,直到滑到最右边,问你,该窗口在滑动的过程中,最大值和 ...
- POJ 3348 Cows | 凸包模板题
题目: 给几个点,用绳子圈出最大的面积养牛,输出最大面积/50 题解: Graham凸包算法的模板题 下面给出做法 1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上) 2.其他点进 ...
- POJ 3041 Asteroids(二分图模板题)
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...
- POJ 1088 滑雪(模板题 DFS+记忆化)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
随机推荐
- Bayesian RL and PGMRL
简介: PGMRL: PGMRL就是把RL问题建模成一个概率图模型,如下图所示: 然后通过variational inference的方法进行学习: PGMRL给RL问题的表示给了一个范例,对解决很多 ...
- Netty:option和childOption参数设置说明
Channel配置参数 (1).通用参数 CONNECT_TIMEOUT_MILLIS : Netty参数,连接超时毫秒数,默认值30000毫秒即30秒. MAX_MESSAGES_PER_REA ...
- The client and server cannot communicate, because they do not possess a common algorithm
The client and server cannot communicate, because they do not possess a common algorithm This was re ...
- WebViewJavascriptBridge的使用说明
WebViewJavascriptBridge 项目介绍 在Obj-C 和 WKWebView, UIWebView 中的 Javascript之间传送信息的桥梁. 项目地址 如何使用 Javascr ...
- Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
使用 HTML5 的图片上传api的时候报如下错误: Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLIn ...
- PowerBI与Visio
前言 如何在Power BI中使用Visio, 刚好最近微软推出了适用于Power BI 的 Visio自定义可视化对象预览,分享给大家. 我们先看一下效果: 通过自定义可视化对象,将Visio ...
- css的position中absolute和fixed的区别
fixed:固定定位 absolute:绝对定位 区别很简单: 1.没有滚动条的情况下没有差异 2.在有滚动条的情况下,fixed定位不会随滚动条移动而移动,而absolute则会随滚动条移动 常用场 ...
- nw.js---创建一个点击菜单
使用nw.js创建一个可点击的菜单: <!doctype html> <html lang="en"> <head> <meta char ...
- zabbix的安装部署及自定义监控的实现
此篇感谢我的小师傅. 1. Zabbix主要功能和优劣势说明 1. Zabbix主要功能和优劣势说明 1.1 Zabbix主要功能: 1)Application monitoring 应用监控 数据库 ...
- adb 常用命名
adb是Android Debug Bridge的简称, 就是起到调试桥的作用,用来操作android设备 adb help (显示帮助信息) adb devices (获取设备列表及设备状态) ad ...