Click
Here to go back to the homepage.
Misa Solution:
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> neighbors(const bool seating[], int R, int S, int currR, int currS){
vector<pair<int, int>> result;
for(int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
if(i == 0 && j == 0){
continue;
}
int tempR = currR + i;
int tempS = currS + j;
if(tempR >= 0 && tempR < R && tempS >= 0 && tempS < S && seating[tempR * S + tempS]){
result.emplace_back(tempR, tempS);
}
}
}
return result;
}
int main(){
// #ifndef TESTING
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int R, S;
cin >> R >> S;
bool seats[R * S];
for(int i = 0; i < R; i++){
for(int j = 0; j < S; j++){
char seat;
cin >> seat;
seats[i * S + j] = seat == 'o';
}
}
pair<int, int> mirkoSeat;
int mirkoNeighbors = 0;
for(int i = 0; i < R; i++){
for(int j = 0; j < S; j++){
if(!seats[i * S + j]){
int neighborCount = neighbors(seats, R, S, i, j).size();
if(neighborCount > mirkoNeighbors){
mirkoNeighbors = neighborCount;
mirkoSeat = {i, j};
}
}
}
}
if(mirkoNeighbors > 0){
seats[mirkoSeat.first * S + mirkoSeat.second] = true;
}
set<pair<pair<int, int>, pair<int, int>>> handshakes;
for(int i = 0; i < R; i++){
for(int j = 0; j < S; j++){
if(seats[i * S + j]){
auto surrounding = neighbors(seats, R, S, i, j);
for(auto person: surrounding){
handshakes.insert({{i, j}, {person.first, person.second}});
handshakes.insert({{person.first, person.second}, {i, j}});
}
}
}
}
cout << handshakes.size() / 2 << endl;
return 0;
}