POJ 1164 城堡问题【DFS/位运算/种子填充法/染色法】
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
- how many rooms the castle has
- 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
Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.
Output
Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).
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
【题意】:给出一个 n*m 矩阵,矩阵代表了一个大房间,然后矩阵的每个元素代表了该模块的信息,用 1 表示 WEST 方向是墙壁,2 表示 NORTH 方向是墙, 4 表示 EAST 方向是墙, 8 表示 SOUTH 方向是墙,题目所给矩阵的元素值就是该模块所有墙壁数值总和,现在要求统计这个大房间被分成了几个区域,其中最大区域包含几个模块。
【分析】:关键是怎么把数字矩阵转化成我们想要的图形矩阵,就是从一个模块所有墙壁数值总和看出这个模块都有哪几面墙,这是这道题的亮点。
1 的2进制 0001,2 的是 0010,4 的是 0100,8 的是 1000,例如 11 取2进制 1011,只有 4 和 11 取 & 运算是 0 ,即该模块在 4 方向(也就是 EAST 方向)是通的。
把方块看作是节点,相邻两个方块之间如果没有墙,则在方块之间连一条边,这样城堡就能转换成一个图。
求房间个数,实际上就是在求图中有多少个极大连通子图。
一个连通子图,往里头加任何一个图里的其他点,就会变得不连通,那么这个连通子图就是极大连通子图。(如:(8,5,6))
对每一个房间,深度优先搜索,从而给这个房间能够到达的所有位置染色。最后统计一共用了几种颜色,以及每种颜色的数量。
比如
1122333
1112343
1115353
1555553
从而一共有5个房间,最大的房间(1)占据9个格子
【注意】:这个题是没有已知的终点的,所以在判断是否停下的时候只能使用标记来进行停止到最后一个
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e6;
const int maxm = 100;
const double PI = acos(-1.0);
const double eps = 1e-8;
//const int dx[] = {-1,1,0,0,1,1,-1,-1};
//const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n, m;
int num=0,ans,Max=0,tot=0;
int a[maxm][maxm];//1左 2上 4右 8下
int vis[maxm][maxm];
void dfs(int i,int j)
{
if(vis[i][j]) return ;
tot++; //
vis[i][j]=num; //染色
if((a[i][j]&1)==0) dfs(i,j-1);
if((a[i][j]&2)==0) dfs(i-1,j);
if((a[i][j]&4)==0) dfs(i,j+1);
if((a[i][j]&8)==0) dfs(i+1,j);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
ms(vis,0);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!vis[i][j])
{
tot=0;
num++;
dfs(i,j);
Max = max(Max,tot);
}
}
}
printf("%d\n%d\n",num,Max);
}
/*
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
*/
POJ 1164 城堡问题【DFS/位运算/种子填充法/染色法】的更多相关文章
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...
- [计算机图形学] 基于C#窗口的Bresenham直线扫描算法、种子填充法、扫描线填充法模拟软件设计(二)
上一节链接:http://www.cnblogs.com/zjutlitao/p/4116783.html 前言: 在上一节中我们已经大致介绍了该软件的是什么.可以干什么以及界面的大致样子.此外还详细 ...
- UVa 818Cutting Chains (暴力dfs+位运算+二进制法)
题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...
- 数独求解问题(DFS+位运算优化)
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- [poj]开关类问题 枚举 位运算
poj 1222 EXTENDED LIGHTS OUT 开关只有两种方案 按和不按,按两次相当于关 只用枚举第一排开关的按法即可,剩下的行为使上一排的灯全部关闭,按法可以确定,并且是唯一的. 最后 ...
- POJ 1166 The Clocks [BFS] [位运算]
1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...
- [LeetCode]78. 子集(位运算;回溯法待做)
题目 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], ...
- LeetCode 78. 子集 C++(位运算和回溯法)
位运算 class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { ...
- [POJ] 2453 An Easy Problem [位运算]
An Easy Problem Description As we known, data stored in the computers is in binary form. The probl ...
随机推荐
- AGC018D Tree and Hamilton Path(树+树的重心)
题目大意: 给你一棵n个结点树,然后根据这棵树构造一个完全图,求完全图的一条最长的哈密顿路径. 构造方式是,完全图中的dis(u, v)就等于树上的u和v的距离. 题解: 这...这..不就是杜教的那 ...
- API网关Kong部署和使用文档
KONG安装使用说明 系统版本:ubuntu14 1.下载安装包 $ wget https://github.com/Mashape/kong/releases/download/0.8.3/kong ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- ng4转义html
https://stackoverflow.com/questions/31548311/angular-html-binding <div [innerHTML]="content& ...
- tomcat内存配置(二)
Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个Java虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,所以在本文开始时,应该先对JavaJVM有关内存方面的知识进 ...
- express添加拦截器
var express = require('express') , routes = require('./routes') , http = require('http') , pat ...
- 2016"百度之星" - 初赛(Astar Round2A)--HDU 5690 |数学转化+快速幂
Sample Input 3 1 3 5 2 1 3 5 1 3 5 99 69 Sample Output Case #1: No Case #2: Yes Case #3: Yes Hint ...
- DOM常用对象
一.select对象 HEML中的下拉列表 属性: 1.options 获得当前select下所有option 2.options[i] 获得当前select下i位置的option 3.selecte ...
- 51nod 1076 2条不相交的路径
给出一个无向图G的顶点V和边E.进行Q次查询,查询从G的某个顶点V[s]到另一个顶点V[t],是否存在2条不相交的路径.(两条路径不经过相同的边) (注,无向图中不存在重边,也就是说确定起点和终点 ...
- 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会
[算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...