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想知道 ...
随机推荐
- Spring Boot 调用 MongoRepository时报org.springframework.beans.factory.NoSuchBeanDefinitionException错误的解决办法
这个问题整整折腾了我两天,现在记录下来,希望可以帮助和我一样,遇到相同问题的小伙伴. 项目是分层的(Intellij IDEA中的模块Module),有API(Core)层,Service&D ...
- 解决wps/office 1-2自动转换1月2日,用样式解决此问题
添加样式: td{mso-number-format:"\@"; }
- 3.静态AOP实现-代理模式
通过代理模式实现在RegUser()方法本身业务前后加上一些自己的功能,如:BeforeProceed和AfterProceed,即不修改UserProcessor类又能增加新功能 定义1个用户接口, ...
- [转]SASS用法指南
[转]SASS用法指南 转自阮一峰 SASS用法指南 一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了 ...
- laravel5.8笔记八:数据库(单库和多库)
数据库配置:根目录下/.env, 单个数据库 .env配置 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT= DB_DATABASE=shop DB_USE ...
- 简单的Verilog测试模板结构
这里记录一下曾经用到的简单的测试模板,如下所示: //timescale `timescale 1ns/1ns module tb_module(); //the Internal motivatio ...
- Redis 主从配置(Windows版)
安装从库 1.复制一份 Redis 文件,当做从库. 2.修改从库文件中 redis.windows.conf 的端口号. 3.安装服务,需要重新设置名称.然后去服务中,开启“redis6380”(此 ...
- 进程间通信之——队列Queue
队列是先进先出. from multiprocessing import Queue q = Queue(6) # 队列容纳上限 q.put(1) # 放到队列里面 q.put(2) q.put(3) ...
- 使用soap遇到的缓存问题
- hibernate的面试总结
hibenate的面试总结. 可能现在大家常常还会遇到一个些面试的时候问一些关于hibernate的问题,我个人觉得,这些东西一般做过开发的人在使用上没有任何的问题的,但是如果是要你来说就不一定能够说 ...