【DFS】Java解决The Castle城堡问题(IOI 1994、POJ1164)

news/2025/2/9 11:13:37/

文章目录

  • 问题描述
  • 解决思路
  • DFS遍历图的递归算法框架
  • Java代码

问题描述

题目来源于1994年IOI信息学奥林匹克竞赛。
POJ1166 题目提交网址:http://poj.org/problem?id=1164
在这里插入图片描述
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

解决思路

把方块看成节点,相邻的两个方块如果之间没有墙,则在方块之间连接一条边,这样城堡就能转换成一个图。
题目中给出的“地图”即可转换为如下图所示的图,寻找城堡数即转换为寻找极大联通子图的数量。
在这里插入图片描述

根据深度优先搜索的算法,从一个节点出发,便可以找到该节点的极大联通子图,并做标记(以防重复寻找),然后继续寻找下一个节点所在极大联通子图。

思路:依次对每一个节点(房间)开始进行深度优先搜索,给该节点的极大联通子图上色,并全部标记为旧节点,然后继续寻找下一个为被搜索的节点(房间)。
在这里插入图片描述
上图中可以看出一共有5个房间(极大联通子图),最大的有9个节点

DFS遍历图的递归算法框架

DFS(V){if(V是旧节点) return;V标记为旧节点;对和V相邻的每个节点U{DFS(U);}
}
int main(){初始化;将所有节点标记为新点;while(图中能找到新点W){DFS(W);}
}

Java代码


import java.util.Scanner;public class Main {public static void main(String[] args) {Solution solution = new Main().new Solution();int R,C;Scanner scanner = new Scanner(System.in);R = scanner.nextInt(); // 几行C = scanner.nextInt(); // 几列int[][] rooms = new int[R][C];for (int i = 0; i < R; i++) {for (int j = 0; j < C; j++) {rooms[i][j] = scanner.nextInt();}}solution.solve(R,C,rooms);}class Solution{int R,C;int maxRoomArea = 0;int colorNum = 0;int roomArea = 0; // 用于临时记录个房间的大小int[][] colors,rooms; // rooms 原始房间地图  colors用于记录颜色()public void solve(int R,int C,int[][] rooms){this.R = R;this.C = C;this.maxRoomArea = 0; // 最大房间的大小this.colorNum = 0; // 颜色序号,0为未上色(没有搜过),this.roomArea = 0;this.colors = new int[R][C]; // 默认初始化所有点为0this.rooms = rooms;for(int i = 0;i < R;i++){for(int j = 0;j < C;j++){if(colors[i][j]==0){ // 找到一个新节点colorNum++; // roomArea = 0;DFS(i,j);maxRoomArea = Math.max(maxRoomArea,roomArea);}}}System.out.println(colorNum);System.out.println(maxRoomArea);}public void DFS(int i,int j) {if(colors[i][j] != 0)return; // 旧点,返回colors[i][j] = colorNum; // 上色roomArea++; if(((rooms[i][j]) & 1) ==0) DFS(i,j-1); // 向左找if(((rooms[i][j]) & 2) ==0) DFS(i -1 ,j); // 向上找if(((rooms[i][j]) & 4) ==0) DFS(i,j+1); if(((rooms[i][j]) & 8) ==0) DFS(i + 1,j);}}
}

在这里插入图片描述
参考: 《算法基础与在线实践》刘家瑛等


http://www.ppmy.cn/news/453780.html

相关文章

某园区3个机房的UPS电源及环境集中监控项目方案

一、方案背景&#xff1a; 东莞某园区负责人通过网络联系到我们&#xff0c;希望能在线集中监控3个机房的UPS电源及环境监测设备&#xff0c;随时掌握每一台UPS电源的实时运行状态及机房环境变化的状况… 1.1用户设备概况&#xff1a; 一楼机房面积为36㎡&#xff0c;有1台山…

云服务器超级鸟,超级鸟的自述_A3_新浪游戏_新浪网

科那多的夜晚&#xff0c;幽静而美丽&#xff0c;新鲜的微风吹来&#xff0c;抚摩着我蓬松的羽毛&#xff0c;我站在枝头&#xff0c;仰 望浩瀚的星空&#xff0c;象往日一样&#xff0c;心中涌现出莫名的哀伤&#xff1a;因为...我是个孤儿&#xff01; 我从来都不知道我的父母…

三维翼智发光字3D字壳打印机有什么功能?

3D打印 三维翼智发光字3D字壳打印机有什么功能发光字3D字壳打印机的九大功能 三维翼智发光字3D字壳打印机有什么功能 随着科技的发展&#xff0c;3d打印机也被广泛的运用&#xff0c;那么在挑选3d打印机的时候&#xff0c;我们都会先了解3d打印机有什么功能&#xff0c;不同的…

宿舍管理系统代码_无惧宿舍断电:山特TG-BOX850 UPS电源体验

本内容来源于什么值得买APP&#xff0c;观点仅代表作者本人 &#xff5c;作者&#xff1a;咸鱼特里 使用UPS电源的理由 对于大学生来说&#xff0c;宿舍晚上断电是一个让很多人非常头疼的事情&#xff0c;现在的大学生晚上不仅打游戏&#xff0c;还经常需要用电脑做作业、写论文…

UPS配置计算(山特C3KS)

<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /> 负载功率估算&#xff1a;一般服务器功率为 600w &#xff0c;以 3 台服务器计算所需功率为 1800w&#xff1b; UPS 功率选择&#xff1a; 1800w/0.72571VA &#xff0c;0.7为功…

QQ空间迁移_【山特C3KS_连接ESXI虚拟机】

山特C3KS 连接ESXI虚拟机 2020-05-29 00:04:57 事情的起因是要对UPS做定期的放电测试&#xff0c;翻看山特winpower的说明书是支持这种方式的 UPS的USB口插到ESXI主机的USB口&#xff0c;然后虚拟机线添加USB控制器 再添加主机设备 大概就是下面这个TOP结构 虚拟机的方式按照管…

Elasticsearch 基本使用(三)条件查询

条件查询 单条件查询matchdebug 查看分词结果match_phrase 多条件查询bool 子元素区别 单条件查询 match match 匹配字段&#xff0c;会对条件分词&#xff0c;然后每个词以or的关系在文档倒排索引内进行查询 GET bank/_search {"query": {"match": {&q…

Windows10完全卸载oracle19c

Windows10完全卸载oracle19c 1.停止服务2.卸载产品3.清理注册表4.清理环境变量5.清理文件夹 1.停止服务 winR输入service.msc进入服务列表&#xff0c;停止所有的服务 2.卸载产品 点击开始菜找到Oracle&#xff0c;然后点击Oracle安装产品&#xff0c;再点击Universal Inst…