翻译:
你已经收到了Bubble机器人的数据。你知道你的任务是制造工厂设施,但在你开始之前,你需要知道工厂有多大,有多少房间。当你查看数据时,你会发现你有这个结构的尺寸,它是矩形的:N x M。
然后在接下来的N行中有M个数字。这些数字代表工厂瓷砖,可以从0到15。每个数字都应该以二进制形式显示。因为从每个数字可以知道瓷砖的哪一面有墙。例如,二进制形式的数字10是1010,这意味着它有一堵来自北侧的墙,没有来自东侧的墙。它在南侧有一堵墙,在西侧没有墙。所以它向北、东、南、西延伸。
可以保证建筑的边缘总是有墙。输入将正确。
您的任务是从最大到最小打印房间的大小。
输入
第一行有两个数字,分别是N和M,即建筑的大小。两者都是整数:
𝑛 (1≤𝑛≤103)
𝑚 (1≤𝑚≤103)
下一个N x M数字代表每个建筑瓷砖。
输出
处理完数据后,输出由一行组成,按房间大小从大到小排序。
实例
输入副本
4 5
9 14 11 12 13
5 15 11 6 7
5 9 14 9 14
3 2 14 3 14
输出副本
9 4 4 2 1
思路:画下图,然后可以发现,沿着边进行模拟即可。用dfs深度遍历就可以。
代码实现:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdio.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<numeric>
using namespace::std;
typedef long long ll;
int n,m;
bool bj[1005][1005];
int ap[1005][1005];
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
int dfs(int x,int y){
int an=1;
bj[x][y]=true;
int ds=ap[x][y];
for (int i =0; i<=3; i++) {
if (((1<<i)&ds)==0) {
if (bj[x+dx[i]][y+dy[i]]) {
continue;
}
an+=dfs(x+dx[i], y+dy[i]);
}
}
return an;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
cin>>n>>m;
for (int i =0 ; i<n; i++) {
for (int j =0; j<m; j++) {
cin>>ap[i][j];
}
}
vector<int>ans;
for (int i = 0; i<n; i++) {
for (int j =0; j<m; j++) {
if (!bj[i][j]) {
ans.push_back(dfs(i, j));
}
}
}
sort(ans.begin(), ans.end(),greater<>());
for(auto jk:ans){
printf("%d ",jk);
}
return 0;
}