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

Description

  1. 1 2 3 4 5 6 7
  2.  
  3. #############################
  4.  
  5. 1 # | # | # | | #
  6.  
  7. #####---#####---#---#####---#
  8.  
  9. 2 # # | # # # # #
  10.  
  11. #---#####---#####---#####---#
  12.  
  13. 3 # | | # # # # #
  14.  
  15. #---#########---#####---#---#
  16.  
  17. 4 # # | | | | # #
  18.  
  19. #############################
  20.  
  21. (Figure 1)
  22.  
  23. # = Wall
  24.  
  25. | = No wall
  26.  
  27. - = 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

  1. 4
  2. 7
  3. 11 6 11 6 3 10 6
  4. 7 9 6 13 5 15 5
  5. 1 10 12 7 13 7 5
  6. 13 11 10 8 10 12 13

Sample Output

  1. 5
  2. 9

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

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

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

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6. #include <cstring>
  7. #pragma warning(disable:4996)
  8. using namespace std;
  9.  
  10. int row,col,i,j,sum,result1,result2;
  11. int value[70][70];
  12. int color[70][70];
  13.  
  14. int move_x[5]={1,0,-1,0};
  15. int move_y[5]={0,1,0,-1};
  16.  
  17. void dfs(int a,int b)
  18. {
  19. color[a][b]=1;
  20. sum++;
  21. int a_x,b_x;
  22.  
  23. if((value[a][b]&1)==0)
  24. {
  25. a_x=a+move_x[3];
  26. b_x=b+move_y[3];
  27. if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
  28. {
  29. dfs(a_x,b_x);
  30. }
  31. }
  32. if((value[a][b]&2)==0)
  33. {
  34. a_x=a+move_x[2];
  35. b_x=b+move_y[2];
  36. if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
  37. {
  38. dfs(a_x,b_x);
  39. }
  40. }
  41. if((value[a][b]&4)==0)
  42. {
  43. a_x=a+move_x[1];
  44. b_x=b+move_y[1];
  45. if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
  46. {
  47. dfs(a_x,b_x);
  48. }
  49. }
  50. if((value[a][b]&8)==0)
  51. {
  52. a_x=a+move_x[0];
  53. b_x=b+move_y[0];
  54. if(a>=1 && a<=row && b>=1 && b<=col && color[a_x][b_x]==0)
  55. {
  56. dfs(a_x,b_x);
  57. }
  58. }
  59.  
  60. }
  61.  
  62. int main()
  63. {
  64. memset(color,0,sizeof(color));
  65.  
  66. cin>>row>>col;
  67. for(i=1;i<=row;i++)
  68. {
  69. for(j=1;j<=col;j++)
  70. {
  71. cin>>value[i][j];
  72. }
  73. }
  74. result1=0;
  75. result2=0;
  76. for(i=1;i<=row;i++)
  77. {
  78. for(j=1;j<=col;j++)
  79. {
  80. sum=0;
  81. if(color[i][j]==0)
  82. {
  83. dfs(i,j);
  84. result2++;
  85. }
  86. result1=max(sum,result1);
  87. }
  88. }
  89.  
  90. cout<<result2<<endl;
  91. cout<<result1<<endl;
  92. return 0;
  93. }

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

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. Centos7 rsync+inotify两台服务器同步文件(单向)

    注:本篇介绍的是单向同步,即A文件同步到B,但B的文件不同步到A,双向同步的在下一篇文章中. rsync与inotify不再赘述,直接进入实战. 0.背景 两台服务器IP地址分别为: 源服务器:192 ...

  2. $('#myModal').modal('show') //显示$('#myModal').modal('hide')隐藏

    你这样试试,这是官方文档的写法 $('#myModal').modal('show') //显示$('#myModal').modal('hide')隐藏 //重复点击的隐藏显示有一个很更方便的写法$ ...

  3. 新手小白如何向GitHub上提交项目

    首先你得注册一个自己的GitHub账号,注册网址:https://github.com/join 创建一个新的项目,填写项目名称,描述 创建完成之后,跳转到下面的页面,下面红框中的网址要记住,在后面上 ...

  4. Java笔记--网络编程

    1.IP地址:InetAddress类 --唯一的标识Internet上的计算机 --本地回环地址(hostAddress)127.0.0.1 主机名(hostName):localhost //根据 ...

  5. 将xml字符串的所有叶标签转换成Map集合

    实际问题:对方服务器接口采用webservice方式返回xml报文,现需解析xml获取所有叶节点的标签名及携带的值 解决方案:利用dom4j解析xml并利用递归获取叶节点,将标签名及标签值封装到Map ...

  6. 009.Oracle数据库 , between关键字判断日期在两者之间

    /*Oracle数据库查询日期在两者之间*/ SELECT PKID, OCCUR_DATE, ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date ...

  7. mysql 索引入门

    创建索引的语法结构:

  8. NOR Flash驱动

      驱动程序 1 ] ] );81         ;83 }84 85 86 static void __exit nor_exit(void)87 {88         iounmap(nor_ ...

  9. 08 SSM整合案例(企业权限管理系统):09.用户和角色操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 09.用户和角色操作 1. 用 ...

  10. Codeforces1243C Tile Painting

    原题面:https://codeforces.com/contest/1243/problem/C 题目大意:给定数字n,有n个方块,第i个和第j个之间的距离(abs(i-j))如果是n的因子,那么第 ...