The Castle
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6677   Accepted: 3767

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

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

题意是一个城堡分成了m*n个块,然后给出了每个块一个数字,这个数字代表门的情况,如果这个块西面有门,那么1就要加到这个数字中。如果这个块北面有门,那么2就要加到这个数字中。如果这个块东面有门,那么4就要加到这个数字中。如果这个块南面有门,那么8就加到这个数字中。

所以就可以使用这个数&1 &2 &4 &8来判断哪一个方向有门,连通着的算一个房间,要求的是房间的数量和最大房间的块数。

应该算是深度搜索的模板题了吧。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int row,col,i,j,sum,result1,result2;
int value[70][70];
int color[70][70]; int move_x[5]={1,0,-1,0};
int move_y[5]={0,1,0,-1}; void dfs(int a,int b)
{
color[a][b]=1;
sum++;
int a_x,b_x; if((value[a][b]&1)==0)
{
a_x=a+move_x[3];
b_x=b+move_y[3];
if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
{
dfs(a_x,b_x);
}
}
if((value[a][b]&2)==0)
{
a_x=a+move_x[2];
b_x=b+move_y[2];
if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
{
dfs(a_x,b_x);
}
}
if((value[a][b]&4)==0)
{
a_x=a+move_x[1];
b_x=b+move_y[1];
if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
{
dfs(a_x,b_x);
}
}
if((value[a][b]&8)==0)
{
a_x=a+move_x[0];
b_x=b+move_y[0];
if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
{
dfs(a_x,b_x);
}
} } int main()
{
memset(color,0,sizeof(color)); cin>>row>>col;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
cin>>value[i][j];
}
}
result1=0;
result2=0;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
sum=0;
if(color[i][j]==0)
{
dfs(i,j);
result2++;
}
result1=max(sum,result1);
}
} cout<<result2<<endl;
cout<<result1<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1164:The Castle的更多相关文章

  1. [POJ]1164 The Castle

    //markdown复制进来一堆问题 还是链接方便点 POJ 1164 The Castle 首先想到用9个方格来表示一个房间,如此一来复杂许多,MLE代码如下: //Writer:GhostCai ...

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  4. OpenJudge 2815 城堡问题 / Poj 1164 The Castle

    1.链接地址: http://bailian.openjudge.cn/practice/2815/ http://poj.org/problem?id=1164 2.题目: 总时间限制: 1000m ...

  5. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  8. POJ 3281:Dining(最大流)

    http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...

  9. POJ 1113:Wall

    原文链接:https://www.dreamwings.cn/poj1113/2832.html Wall Time Limit: 1000MS   Memory Limit: 10000K Tota ...

随机推荐

  1. pytorch & numpy广播法则

    广播法则 所有数组向维度最高的数组看齐,若维度不足则在最前面的维度用1补齐 扩展维度后,所有数组在某一维度相同或者长度为1,否则不能计算 当可以计算时,将长度为1的维度扩展为另一数组相应维度的长度 a ...

  2. CentOS 7 搭建本地YUM仓库,并定期同步阿里云源

    目录导航: 1. 系统环境 2. 修改yum 源为阿里云源 3. 安装yum相关的软件 4. 根据源标识同步源到本地目录 5. 安装nginx开启目录权限保证本地机器可以直接本地yum源 6. 客户端 ...

  3. vSphere中Storage vMotion的流程详解

    内容预览: 1. Storage vMotion的迁移方式 2. 影响Storage vMotion效率的因素 3. Storage vMotion的详细流程 企业部署虚拟化后,如果发现存储的性能出现 ...

  4. vSphere 计算vMotion的迁移原理

    1. 计算vMotion 的应用场景 1). 计划内停机维护 2). 提高资源的利用率 2. 计算vMotion 需求: 1).共享存储 vMotion需要解决的核心问题就是:将VMs的内存从源ESX ...

  5. xcode windows版安装使用教程

    随着iPhone.iPad.Mac等苹果产品越来越火爆,越来越多的初学者想要了解和尝试苹果平台,包括苹果操作系统Mac OS X.苹果演示软件Keynote.苹果开发工具Xcode等.然而,苹果电脑价 ...

  6. [Java] Eclipse 设置相同变量背景色高亮显示

    在Eclipse中,鼠标选中或者光标移动到java类的变量名时,相同变量会被标识显示(设置背景色高亮), 并且侧边滚动条会标出变量的位置, 查找变量十分方便. 1.相同变量标识高亮显示:Window ...

  7. Day6 - D - Tree 园丁的烦恼 HYSBZ - 1935

    很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思索一个问题, ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-step-forward

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  9. 在 Windows 系统上安装 Jekyll

    目录 安装 Ruby 环境 用 Bundler 安装 Jekyll 本文是写给完全未用过 Ruby 乃至命令行工具者的.对于一般的开发者,Jekyll 官方文档的相关内容已然足够. 本文为钱院学辅技术 ...

  10. C语言小游戏: 2048.c

    概要:2048.c是一个C语言编写的2048游戏,本文将详细分析它的源码和实现.C语言是一种经典实用的编程语言,本身也不复杂,但是学会C语言和能够编写实用的程序还是有一道鸿沟的.本文试图通过一个例子展 ...