深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)
将问题的各状态之间的转移关系描述
为一个图,则深度优先搜索遍历整个图的
框架为:
Dfs(v) {
if( v 访问过)
return;
将v标记为访问过;
对和v相邻的每个点u: Dfs(u);
}
int main() {
while(在图中能找到未访问过的点 k)
Dfs(k);
}
例题:
POJ1164 The Castle
Description
- 1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(Figure 1)- # = Wall
| = No wall
- = No wall
Figure 1 shows the map of a castle.Write a program that calculates
1. how many rooms the castle has
2. how big the largest room is
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls.
Input
Output
Sample Input
- 4
- 7
- 11 6 11 6 3 10 6
- 7 9 6 13 5 15 5
- 1 10 12 7 13 7 5
- 13 11 10 8 10 12 13
Sample Output
- 5
- 9
Source
百练2815 城堡问题
描述
- 1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(图 1)- # = Wall
| = No wall
- = No wall
图1是一个城堡的地形图。请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。城堡被分割成mn(m≤50,n≤50)个方块,每个方块可以有0~4面墙。
输入程序从标准输入设备读入数据。第一行是两个整数,分别是南北向、东西向的方块数。在接下来的输入行里,每个方块用一个数字(0≤p≤15)描述。用一个数字表示方块周围的墙,1表示西墙,2表示北墙,4表示东墙,8表示南墙。每个方块用代表其周围墙的数字之和表示。城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。输入的数据保证城堡至少有两个房间。输出城堡的房间数、城堡中最大房间所包括的方块数。结果显示在标准输出设备上。
样例输入
- 4
- 7
- 11 6 11 6 3 10 6
- 7 9 6 13 5 15 5
- 1 10 12 7 13 7 5
- 13 11 10 8 10 12 13
样例输出
- 5
- 9
解题思路
对每一个 方块,深度优先搜索,从而给这个方
块能够到达的所有位置染色。最后统计一共用
了几种颜色,以及每种颜色的数量。
比如
1 1 2 2 3 3 3
1 1 1 2 3 4 3
1 1 1 5 3 5 3
1 5 5 5 5 5 3
从而一共有5个房间,最大的房间(1)占据9
个格子
- // By LYLtim
- // 2015.2.16
- #include <iostream>
- using namespace std;
- int m, n, roomNum = , maxRoomAero = , curRoomAera;
- int map[][], color[][] = {};
- void dfs(int i, int j) {
- color[i][j] = roomNum;
- curRoomAera++;
- if (((map[i][j] & ) == ) && (j > ) && !color[i][j-]) dfs(i, j-);
- if (((map[i][j] & ) == ) && (i > ) && !color[i-][j]) dfs(i-, j);
- if (((map[i][j] & ) == ) && (j+ < n) && !color[i][j+]) dfs(i, j+);
- if (((map[i][j] & ) == ) && (i+ < m) && !color[i+][j]) dfs(i+, j);
- }
- int main()
- {
- cin >> m >> n;
- for( int i = ; i < m; i++)
- for (int j = ; j < n; j++)
- cin >> map[i][j];
- for( int i = ; i < m; i++)
- for (int j = ; j < n; j++)
- if (!color[i][j]) {
- roomNum++;
- curRoomAera = ;
- dfs(i, j);
- if (curRoomAera > maxRoomAero)
- maxRoomAero = curRoomAera;
- }
- cout << roomNum << endl << maxRoomAero;
- }
- // By LYLtim
- // 2015.2.17
- #include <iostream>
- #include <stack>
- using namespace std;
- int m, n, roomNum = , curRoomAera;
- int map[][], color[][] = {};
- struct Room
- {
- int x, y;
- Room(int x, int y):x(x),y(y) {}
- };
- void dfs(int startX, int startY) {
- stack<Room> stack;
- stack.push(Room(startX, startY));
- int x, y;
- while (!stack.empty()) {
- Room topRoom = stack.top();
- x = topRoom.x;
- y = topRoom.y;
- if (color[x][y])
- stack.pop();
- else {
- curRoomAera++;
- color[x][y] = roomNum;
- if (((map[x][y] & ) == ) && (y > ) && !color[x][y-])
- stack.push(Room(x, y-));
- if (((map[x][y] & ) == ) && (x > ) && !color[x-][y])
- stack.push(Room(x-, y));
- if (((map[x][y] & ) == ) && (y+ < n) && !color[x][y+])
- stack.push(Room(x, y+));
- if (((map[x][y] & ) == ) && (x+ < m) && !color[x+][y])
- stack.push(Room(x+, y));
- }
- }
- }
- int main()
- {
- int maxRoomAero = ;
- cin >> m >> n;
- for( int i = ; i < m; i++)
- for (int j = ; j < n; j++)
- cin >> map[i][j];
- for( int i = ; i < m; i++)
- for (int j = ; j < n; j++)
- if (!color[i][j]) {
- roomNum++;
- curRoomAera = ;
- dfs(i, j);
- if (curRoomAera > maxRoomAero)
- maxRoomAero = curRoomAera;
- }
- cout << roomNum << endl << maxRoomAero <<;
- }
深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)的更多相关文章
- 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)
从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...
- 深度优先搜索(dfs),城堡问题
题目链接:http://poj.org/problem?id=1164 1.深搜,每个点都访问一次,没有标记的话,就做深搜,同时标记. #include <iostream> #inclu ...
- POJ - 1321 深度优先搜索入门
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> us ...
- 回溯算法 DFS深度优先搜索 (递归与非递归实现)
回溯法是一种选优搜索法(试探法),被称为通用的解题方法,这种方法适用于解一些组合数相当大的问题.通过剪枝(约束+限界)可以大幅减少解决问题的计算量(搜索量). 基本思想 将n元问题P的状态空间E表示成 ...
- python--递归(附利用栈和队列模拟递归)
博客地址:http://www.cnblogs.com/yudanqu/ 一.递归 递归调用:一个函数,调用的自身,称为递归调用 递归函数:一个可以调用自身的函数称为递归函数 凡是循环能干的事,递归都 ...
- 二叉树遍历,递归,栈,Morris
一篇质量非常高的关于二叉树遍历的帖子,转帖自http://noalgo.info/832.html 二叉树遍历(递归.非递归.Morris遍历) 2015年01月06日 | 分类:数据结构 | 标 ...
- castle problem——(深度优先搜索,递归实现和stack实现)
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
- 【11】python 递归,深度优先搜索与广度优先搜索算法模拟实现
一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件 2.找出这一次和上一次关系 3.假设 ...
随机推荐
- [解决]JS失效,提示HTML1114: (UNICODE 字节顺序标记)的代码页 utf-8 覆盖(META 标记)的冲突的代码页 utf-8
上网找了找,木有找到相关的解决办法,索性自己试了试. 原页面是这样写的: <html> <head> <meta http-equiv="Content-Typ ...
- php+mysql中文无法检索出数据
将数据库字符集设置为latin,并将字符转码成gb2312 将字符长度打印出来发现:因为latin中一个字符占用2个字节,utf-8中一个字符占用3个字节,这样一来检索的时候就会出现错误 实验过程: ...
- 解决Eclipse Maven插件的最佳方案
最近在尝试使用GAE,要求项目必须使用Maven,于是下载了Maven3.1.1配置了环境变量.但是在为Eclipse EE安装Maven插件的时候遇到了问题. 官网的建议是: 在Eclipse EE ...
- vue的seo方案 prerender-seo-plugin
利用vue cli 3.0安装脚手架.记住:勾选vue-router. 在vue.config.js里添加配置: 2, var path = require('path') 3, const Prer ...
- GEO--工具 ScanGEO
http://scangeo.dartmouth.edu/ScanGEO/ ScanGEO - parallel mining of high-throughput gene expression d ...
- 【Python】更优的字符串格式化方式 -- "format"替代"%s"
背景 前段时间看了一篇介绍Python的代码技巧的文章,建议格式化字符串时使用"format"代替使用"%",但是没有说明原因.各博客网站介绍相关用法的博客很多 ...
- js三种对话框
<html> <head> <script> function tip1(){ //这个基本没有什么说的,就是弹出一个提醒的对话框 alert("这个第一 ...
- javascript中对变量类型的判断
本文正式地址:http://www.xiabingbao.com/javascript/2015/07/04/javascript-type 在JavaScript中,有5种基本数据类型和1种复杂数据 ...
- phalcon: 目录分组后的acl权限控制
phalcon: 目录分组后的acl权限控制 楼主在做acl权限的时候,发现官方的acl只能针对未分组的目录,如下: app/ ___|./controller ___|./logic ___|./p ...
- FlexboxLayout——Android弹性布局
FlexboxLayout是一个Android平台上与CSS的 Flexible box 弹性盒子布局模块 有相似功能的库.Flexbox 是CSS 的一种布局方案,可以简单.快捷的实现复杂布局. F ...