Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Unreal Engine 5
- 다익스트라
- 누적 합
- 브루트포스
- 투 포인터
- XR Interaction Toolkit
- 유니온 파인드
- 트리
- 시뮬레이션
- Team Fortress 2
- DFS
- c++
- 유니티
- 그리디 알고리즘
- 알고리즘
- 재귀
- 수학
- 문자열
- 우선순위 큐
- 스택
- BFS
- 자료구조
- 정렬
- 구현
- 백트래킹
- 백준
- VR
- ue5
- 다이나믹 프로그래밍
- 그래프
Archives
- Today
- Total
1일1알
백준 5212번 지구 온난화 C++ 본문
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 |