POJ1164——The Castle
2021/4/17 18:55:19
本文主要是介绍POJ1164——The Castle,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目来自:http://poj.org/problem?id=1164
Description
1 2 3 4 5 6 7 ############################# # | # | # | | # #####---#####---#---#####---# # # | # # # # # #---#####---#####---#####---# # | | # # # # # #---#########---#####---#---# # # | | | | # # ############################# (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
Source
IOI 1994 作者解释:输入一个长m,宽n的矩阵,每个矩阵中的数代表当前房间的墙,1(西墙),2(北墙),4(东墙),8(南墙),数字有可能是这4个数的和。求出房间个数和最大房间大小。 作者分析:深度优先搜索(DFS),朝着连通的地方搜索。#include <iostream> #include <cstring> using namespace std; int n,m,a[51][51],ans = 0,maxn = -1,cnt = 0,id[51][51]; bool vis[51][51]; void dfs(int x,int y){ if (vis[x][y]) return ; if (x > n || x < 1 || y > m || y < 1) return ; vis[x][y] = true; ans++; id[x][y] = cnt; if (a[x][y] == 14){dfs(x,y - 1);} if (a[x][y] == 13){dfs(x - 1,y);} if (a[x][y] == 12){dfs(x,y - 1);dfs(x - 1,y);} if (a[x][y] == 11){dfs(x,y + 1);} if (a[x][y] == 10){dfs(x,y - 1);dfs(x,y + 1);} if (a[x][y] == 9){dfs(x - 1,y);dfs(x,y + 1);} if (a[x][y] == 8){dfs(x,y - 1);dfs(x - 1,y);dfs(x,y + 1);} if (a[x][y] == 7){dfs(x + 1,y);} if (a[x][y] == 6){dfs(x,y - 1);dfs(x + 1,y);} if (a[x][y] == 5){dfs(x - 1,y);dfs(x + 1,y);} if (a[x][y] == 4){dfs(x - 1,y);dfs(x,y - 1);dfs(x + 1,y);} if (a[x][y] == 3){dfs(x,y + 1);dfs(x + 1,y);} if (a[x][y] == 2){dfs(x,y - 1);dfs(x,y + 1);dfs(x + 1,y);} if (a[x][y] == 1){dfs(x - 1,y);dfs(x,y + 1);dfs(x + 1,y);} if (a[x][y] == 0){dfs(x - 1,y);dfs(x,y + 1);dfs(x + 1,y);dfs(x,y - 1);} } int main(){ memset(vis,false,sizeof(vis)); cin >> n >> m; for (int i = 1;i <= n;i++){ for (int j = 1;j <= m;j++){ cin >> a[i][j]; } } for (int i = 1;i <= n;i++){ for (int j = 1;j <= m;j++){ if (!vis[i][j]){ cnt++; ans = 0; dfs(i,j); maxn = max(ans,maxn); } } } cout << cnt << endl << maxn; return 0; }
这篇关于POJ1164——The Castle的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc