1일1알

백준 5212번 지구 온난화 C++ 본문

알고리즘

백준 5212번 지구 온난화 C++

영춘권의달인 2022. 12. 7. 10:38

https://www.acmicpc.net/problem/5212

 

5212번: 지구 온난화

첫째 줄에 지도의 크기 R과 C (1 ≤ R, C ≤ 10)가 주어진다. 다음 R개 줄에는 현재 지도가 주어진다.

www.acmicpc.net

 

잠겨버릴 땅을 찾아서 전부 지우고 남은 모든 섬을 포함하는 제일 작은 직사각형 범위를 출력하였다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int r, c;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };

vector<vector<char>> board;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> r >> c;
    board = vector<vector<char>>(r, vector<char>(c));
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cin >> board[i][j];
        }
    }
    vector<pair<int, int>> v;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (board[i][j] == '.') continue;
            int cnt = 0;
            for (int k = 0; k < 4; k++) {
                int nextRow = i + dRow[k];
                int nextCol = j + dCol[k];
                if (nextRow < 0 || nextRow >= r || nextCol < 0 || nextCol >= c) {
                    cnt++;
                    continue;
                }
                if (board[nextRow][nextCol] == '.') {
                    cnt++;
                }
            }
            if (cnt >= 3) v.push_back({ i,j });
        }
    }
    for (auto a : v) {
        board[a.first][a.second] = '.';
    }
    int startRow = r - 1;
    int startCol = c - 1;
    int endRow = 0;
    int endCol = 0;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (board[i][j] == '.') continue;
            startRow = min(startRow, i);
            startCol = min(startCol, j);
            endRow = max(endRow, i);
            endCol = max(endCol, j);
        }
    }
    for (int i = startRow; i <= endRow; i++) {
        for (int j = startCol; j <= endCol; j++) {
            cout << board[i][j];
        }
        cout << "\n";
    }
}

'알고리즘' 카테고리의 다른 글

백준 1613번 역사 C++  (0) 2022.12.09
백준 22352번 항체 인식 C++  (0) 2022.12.08
백준 14248번 점프 점프 C++  (0) 2022.12.06
백준 12018번 Yonsei TOTO C++  (1) 2022.12.05
백준 2992번 크면서 작은 수 C++  (0) 2022.12.04