[Codeforces] Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2) J. Robot Factory

Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2) J. Robot Factory

Solution

BFS에 N(8) E(4) S(2) W(1)가 벽인 지점이다.
!(metrix[i][j] & N E S W)가 벽이 아닌 지점으로 체크하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <bits/stdc++.h>

using namespace std;

//Speed
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Sumfi cout.tie(NULL);

//Aliases
using ll= long long;
using lld= long double;
using ull= unsigned long long;

//Constants
const lld pi= 3.141592653589793238;
const ll INF= 1e18;
const ll mod=1e9+7;

//TypeDEf
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll,ll> umll;
typedef map<ll,ll> mll;

// Macros
#define ff first
#define ss second
#define pb push_back
#define ist insert
#define has count
#define mp make_pair
#define fl(i,n) for(int i=0;i<n;i++)
#define rl(i,m,n) for(int i=n;i>=m;i--)
#define py cout<<"YES\n";
#define pm cout<<"-1\n";
#define pn cout<<"NO\n";
#define vr(v) v.begin(),v.end()
#define rv(v) v.end(),v.begin()

//Debug
#ifdef AbhinavAwasthi
#define debug(x) cerr << #x<<" "; cerr<<x<<" "; cerr << endl;
#else
#define debug(x);
#endif

// Operator overloads
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }

// Utility functions
template <typename T>
void print(T &&t) { cout << t << "\n"; }
void printarr(ll arr[], ll n){fl(i,n) cout << arr[i] << " ";cout << "\n";}
template<typename T>
void printvec(vector<T>v){ll n=v.size();fl(i,n)cout<<v[i]<<" ";cout<<"\n";}
template<typename T>
ll sumvec(vector<T>v){ll n=v.size();ll s=0;fl(i,n)s+=v[i];return s;}

// Mathematical functions
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}
ll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;}
ll powermod(ll x, ll y, ll p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
ll powerll(ll x, ll n){ll res=1;while(n){if (n & 1){res=res * x;}n>>=1;x*=x;}return res;}


//Graph-dfs
// bool gone[MN];
// vector<int> adj[MN];
// void dfs(int loc){
// gone[loc]=true;
// for(auto x:adj[loc])if(!gone[x])dfs(x);
// }

//To Remember
// vector<array<int, 2>> ranges(n);
// cin >> ranges;

// sort(all(ranges), [] (const array<int, 2>& X, const array<int, 2>& Y){
// return X[1] - X[0] < Y[1] - Y[0];
// });

//Sorting
bool sorta(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
bool sortd(const pair<int,int> &a,const pair<int,int> &b){return (a.second > b.second);}

//Bits
string decToBinary(int n){string s="";int i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;}
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for(int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}

//Check
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}
bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}

//Code by Sumfi
//Language C++
//Practice->Success

//Code
ll cnt(vll v, ll l, ll r) {
return v[r] - (l ? v[l-1] : 0);
};
struct compare{
bool operator()(const pll& a, const pll& b) {
return a.ss - a.ff == b.ss - b.ff ? a.ss < b.ss : a.ss - a.ff > b.ss - b.ff;
}
};

void sumfi()
{
int arr[1000][1000], dy[4]{0,1,0,-1}, dx[4]{-1,0,1,0};
bool v[1000][1000] {false,};
vll res;
ll n,m;
cin>>n>>m;
fl(i,n) {
fl(j, m) {
cin >> arr[i][j];
}
}

fl(i,n) {
fl(j,m) {
if(v[i][j]) continue;
ll cnt = 1;
queue<pll> q;
q.push(mp(i,j));
v[i][j] = true;
while(!q.empty()) {
auto [y, x] = q.front();
q.pop();
fl(k,4) {
ll nx = x + dx[k], ny = y + dy[k];
if(!v[ny][nx] && !(arr[y][x] & (1<<k))) {
v[ny][nx] = true;
q.push(mp(ny,nx));
++cnt;
}
}
}
res.pb(cnt);
}
}
sort(vr(res), greater<ll>());
for(auto ret : res) cout<<ret<<" ";
}
//Main
int main()
{
Code By Sumfi
ll t;
//cin>>t;
//fl(i,t)
{
sumfi();
}
return 0;
}
//End
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/27/PS/Codeforces/div2-bubblecup14-j/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.