-
백준(BOJ) 16988번 Baaaaaaaaaduk2 (Easy)알고리즘 풀이/백준(Boj) 2019. 12. 2. 01:21
문제 : https://www.acmicpc.net/problem/16988
16988번: Baaaaaaaaaduk2 (Easy)
서기 2116년, 인간은 더 이상 AI의 상대가 되지 못하게 되었다. 근력, 순발력, 창의력, 사고력, 문제해결능력, 심지어 인간미조차 AI가 인간을 앞선다. AI가 온 지구를 관리하며 이미 인류는 지구의 주인 자리에서 쫓겨난지 오래이다. 그나마 다행인 것은 AI가 인간을 적대적으로 대하지 않고, 도리어 AI가 쌓아올린 눈부신 기술의 발전으로 모든 사람이 무제한적인 재화를 사용할 수 있게 되어 한 세기 전의 사람들이 바라던 돈 많은 백수와 같은 삶을 누릴
www.acmicpc.net
풀이 :
우선 좌표값이 0인 좌표들만 v 배열에 넣는다. 그 후 2개씩 뽑아서 각각의 경우를 확인한다.
좌표값이 2인 좌표를 고른 후 BFS를 통해 탐색해주는데 이때 각 좌표들은 모두 1로 둘러싸여
있어야 한다. 이 조건은 상하좌우가 0이 아니라는 조건과 일치하기 때문에 만약 0이 나온다면
bool 조건을 false로 주어서 더하지 못하게 막아준다.
코드 ( C++ )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <queue> using namespace std; int a[20][20]; const int dy[4] = { 1,-1,0,0 }; const int dx[4] = { 0,0,1,-1 }; int n, m; int best; int d[20][20]; int bfs() { memset(d, 0, sizeof(d)); int ret = 0; for(int i=0; i<n; ++i) for (int j = 0; j < m; ++j) { if (a[i][j] == 2 && d[i][j] == 0) { bool isDead = true; queue<pair<int, int>> q; q.push({ i,j }); d[i][j] = 1; int num = 0; while (!q.empty()) { num++; int y = q.front().first; int x = q.front().second; q.pop(); for (int k = 0; k < 4; ++k) { int ny = y + dy[k]; int nx = x + dx[k]; if (!(0 <= ny && ny < n && 0 <= nx && nx < m)) continue; if (a[ny][nx] == 0) isDead = false; else if (a[ny][nx] == 2 && d[ny][nx] == 0) { d[ny][nx] = 1; q.push({ ny,nx }); } } } if (isDead) { ret += num; } } } return ret; } int main() { vector<pair<int, int>> v; cin >> n >> m; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { cin >> a[i][j]; if (a[i][j] == 0) { v.push_back({ i,j }); } } int ans = 0; for (int i = 0; i < v.size(); ++i) for (int j = 0; j < v.size(); ++j) { if (i == j) continue; int fy = v[i].first; int fx = v[i].second; int sy = v[j].first; int sx = v[j].second; a[fy][fx] = 1; a[sy][sx] = 1; int cand = bfs(); if (ans < cand) ans = cand; a[fy][fx] = 0; a[sy][sx] = 0; } cout << ans << "\n"; return 0; } '알고리즘 풀이 > 백준(Boj)' 카테고리의 다른 글
백준(BOJ) 16922번 로마 숫자 만들기 (0) 2019.12.09 백준(BOJ) 16917번 양념 반 후라이드 반 (0) 2019.12.08 백준(BOJ) 16935번 배열 돌리기 3 (0) 2019.12.01 백준(BOJ) 16987번 계란으로 계란치기 (0) 2019.11.30 백준(BOJ) 16939번 2x2x2 큐브 (0) 2019.11.30