๋ฌธ์ ๋งํฌ
23289๋ฒ: ์จํ๊ธฐ ์๋ ! (acmicpc.net)
์จํ๊ธฐ๋ฅผ ์ด์ฉํด์ ์กฐ์ฌํ๋ ์นธ์ ์จ๋๊ฐ ๋ชจ๋ ํน์ ์จ๋ ์ด์์ด ๋๋๋ก ํ๋๋ฐ ํ ์คํธ ๊ณผ์ ์ ๋ช ๋ฒ ๊ฑฐ์ณ์ผํ๋์ง ๊ณ์ฐํ๋ ๊ตฌํ ๋ฌธ์ .
์ฌ๊ธฐ์๋ ํ ์คํธ ๊ณผ์ ํ ๋ฒ์ ์ด์ฝ๋ฆฟ์ ํ๋ ๋จน๋ ๊ฒ์ผ๋ก ํํํ๋ค.
ํ์ด
R x C ํฌ๊ธฐ์ ๊ฒฉ์์ ์จํ๊ธฐ๊ฐ ๋์ฌ์๋ค. ์ด ์จํ๊ธฐ์์ ๋ฐ๋์ด ํ ๋ฒ ๋์ค๋ฉด ๋ฌธ์ ์์ ์ ์๋ ๊ทธ๋ฆผ๋๋ก ๊ฒฉ์ ์นธ์ ์จ๋๊ฐ ์์นํ๊ฒ ๋๋ค.
์ฑ๋ฅ ํ ์คํธ์ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. ์ง์ ์๋ ๋ชจ๋ ์จํ๊ธฐ์์ ๋ฐ๋์ด ํ ๋ฒ ๋์ด
2. ์จ๋๊ฐ ์กฐ์ ๋จ
3. ์จ๋๊ฐ 1 ์ด์์ธ ๊ฐ์ฅ ๋ฐ๊นฅ์ชฝ ์นธ์ ์จ๋๊ฐ 1์ฉ ๊ฐ์
4. ์ด์ฝ๋ฆฟ์ ํ๋ ๋จน๋๋ค.
5. ์กฐ์ฌํ๋ ๋ชจ๋ ์นธ์ ์จ๋๊ฐ K ์ด์์ด ๋์๋์ง ๊ฒ์ฌ.
๋ชจ๋ ์นธ์ ์จ๋๊ฐ K์ด์์ด๋ฉด ํ
์คํธ๋ฅผ ์ค๋จํ๊ณ , ์๋๋ฉด 1๋ถํฐ ๋ค์ ์์ํ๋ค.
๋ค์๊ณผ ๊ฐ์ ์ฑ๋ฅํ ์คํธ๊ฐ ์งํ๋จ์ ์์ด ์ฐ๋ฆฌ๊ฐ ์ ์ฅํ๊ณ ์์ด์ผ ํ ์ ๋ณด๋ค์ ์ด๋ค ๊ฒ๋ค์ด ์์๊น?
1. ๊ฒฉ์ํ์ ํ์ฌ ์จ๋
๊ฐ ์นธ์ด ํ์ฌ ๋ช ๋์ธ์ง ์ ์ฅํ๊ณ ์์ด์ ์จ๋ ๋ณํ์ ์จ๋ ์กฐ์ฌ๊ฐ ๊ฐ๋ฅํ๋ค.
int ํ์ 2์ฐจ์๋ฐฐ์ด Map[MAX_N][MAX_N]์ ๋ง๋ค์ด์ ๊ฐ ์นธ๋ง๋ค์ ์จ๋๋ฅผ ์ ์ฅํ๋ค.
์จ๋์ ๋ณํ๊ฐ ๋ฐ์ํ๋ฉด Map์ ๊ฐ์ ๋ณ๊ฒฝ์์ผ์ฃผ์ด์ผ ํ๋ค.
2. ์จํ๊ธฐ์ ์์น, ๋ฐฉํฅ
์จํ๊ธฐ์ ์ขํ์ ๋ฐ๋ผ๋ณด๊ณ ์๋ ๋ฐฉํฅ์ ์์์ผ ๋ฐ๋์ ๋ณด๋ผ ์๊ฐ ์๋ค.
๊ทธ๋ฐ๋ฐ ์จํ๊ธฐ๋ ๋ช ๊ฐ๊ฐ ์ค์น๋ ์ง ๋ชจ๋ฅด๋ฏ๋ก vector๋ฅผ ํตํด ๊ฐ์ ์ ์ฅํ๋ ๊ฒ์ด ์ข๋ค.
vector<pair<pair<int,int>,int>> ๋ฅผ ๋ง๋ค์ด์ ์จํ๊ธฐ์ ์ขํ์, ๋ฐฉํฅ ๋ ๊ฐ์ง ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
3. ๋ฒฝ์ ์์น
๋ฒฝ์ ์ธ์ ํ ๋ ์นธ ์ฌ์ด์ ์ค์น๋ ์ ์๋ค. 2์ฐจ์ ๋ฐฐ์ด๋ก ๋ฒฝ์ ์์น๋ฅผ ํํํ๊ธฐ์๋ ํ๊ณ๊ฐ ์๋ค๊ณ ์๊ฐํ๋ค.
๋ง์ฝ ( y, x )์ (y+1, x) ์ฌ์ด์ ๋ฒฝ์ด ์๋ค๊ณ ํ์ ๋, ๋ฒฝ์ ์์น๋ฅผ 2์ฐจ์ ๋ฐฐ์ด๋ก Wall[y][x] ๋ผ๊ณ ํ์ํ๋ค๋ฉด (y,x) ์นธ์ ์ํ์ข์ฐ ๋ฐฉํฅ ์ค ์ด๋ค ์์น์ ๋ฒฝ์ด ์ค์น๋์ด์๋์ง ์ ์ ์๋ค.
๊ทธ๋์ ๋๋ ๋ ์นธ ์ฌ์ด์ ๋ฒฝ์ด ๋์ฌ์์์ ํ์ํ๊ธฐ ์ํด 4์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์๋ค.
Wall[y][x][y+1][x] ๊ฐ์ด true๋ฉด ๋ ์ขํ ์ฌ์ด์ ๋ฒฝ์ด ๋์ฌ์๋ค๋ ๊ฑธ๋ก ํ๋จํ๋ค.
์ด๋ Wall[y][x][y+1][x] ์ Wall[y+1][x][y][x] ์ ๊ฐ์ ํญ์ ๊ฐ์์ผํ๋ค.
4. ์กฐ์ฌํด์ผํ ์นธ์ ์ขํ
์ขํ์ค ์ ๋ ฅ๊ฐ์ผ๋ก 5๋ฅผ ๋ฐ๋ ์์น๋ง ์จ๋ ์กฐ์ฌ๋ฅผ ํด์ฃผ๋ฉด ๋๋ค.
์ ๋ ฅ๊ฐ์ด 5๋ผ๋ฉด ํด๋น ์นธ์ inspect๋ผ๋ pair<int,int> ๋ฒกํฐ์ ์ขํ๋ฅผ ์ ์ฅํด์ ์จ๋๋ฅผ ์กฐ์ฌํ ๋ ์ฌ์ฉํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ํ์ํ ์ ๋ณด๋ค์ ์ด ์ ๋๊ฐ ์๋ค.
๋๋จธ์ง๋ ํจ์๋ฅผ ์ด๋ป๊ฒ ์ง๋๋์ ๋ฐ๋ผ ์๊ณ ์์ด์ผ ํ ์ ๋ณด๋ค์ด ๋ค๋ฅผ ๊ฒ์ด๋ค.
- ์จํ๊ธฐ ๋ฐ๋์ ์งํ
์จํ๊ธฐ ๋ฐ๋์ ๋์ค๋ ๋ฐฉํฅ์ผ๋ก ๋ถ์ฑ๊ผด ๋ชจ์์ผ๋ก ํผ์ ธ๋๊ฐ๋ฉฐ ์จํ๊ธฐ์์ ๋ฉ์ด์ง ์๋ก ์ฆ๊ฐํ๋ ์จ๋๊ฐ 5->4->3->2->1 ์์๋๋ก ์ค์ด๋ ๋ค.
๋ฐ๋์ ์งํํ๋ ๋ฐฉํฅ์ ๊ธฐ์ค์ผ๋ก ์งํ๋ฐฉํฅ ์ผ์ชฝ ์ ๋๊ฐ์ , ์, ์ค๋ฅธ์ชฝ ์ ๋๊ฐ์ ๋ฐฉํฅ ์ด๋ ๊ฒ 3๋ฐฉํฅ์ผ๋ก ํผ์ ธ๋๊ฐ๋ค.
์ด๋ ์งํํ๋ ค๋ ๋ฐฉํฅ์ ๋ฒฝ์ด ์ค์น๋์ด์๋ค๋ฉด ํด๋น ๋ฐฉํฅ์ผ๋ก๋ ๋ฐ๋์ ๋ ์ด์ ์งํํ์ง ๋ชปํ๋ค.
๋๋ BFS๋ฅผ ์ด์ฉํด์ ๋ฐ๋์ ์งํ์ ๊ตฌํํ๋ค.
์ฒ์ ๋ฐ๋์ด ๋์ค๋ ์นธ์ ๋ฒฝ์ผ๋ก ๋งํ์์ง ์๋ค๋ ์กฐ๊ฑด์ด ๋ช ์๋์ด ์์ผ๋ฏ๋ก ํ์ ์ด๊ธฐ๊ฐ์ผ๋ก ์จํ๊ธฐ ๋ฐ๋ก ์ ์นธ์ ์ขํ์ ์ฆ๊ฐํ๋ ์จ๋์ธ 5๋ฅผ ๋ฃ๋๋ค. ์ดํ ์์ ๋งํ 3๋ฐฉํฅ ์ค ์งํ์ด ๊ฐ๋ฅํ ๋ฐฉํฅ์ ์ขํ๋ฅผ ํ์ ์ถ๊ฐ์ ์ผ๋ก ๋ฃ์ด์ค๋ค. ์ด๋ ์ฆ๊ฐํ๋ ์จ๋ ๊ฐ์ 1์ฉ ์ค์ด๋ฉด์ ๋ฃ๋๋ค. ์ฆ๊ฐํ๋ ์จ๋๊ฐ 1์ธ ๋ฐ๋์ ๋ ์ด์ ์งํํด๋ ์จ๋๋ณํ์ ์๋ฏธ๊ฐ ์์ผ๋ฏ๋ก ๊ทธ ๋ฐ๋์ ์งํ์ ์ข ๋ฃํ๋ค.
์งํํ๋ ๋ฐฉํฅ 3๋ฐฉํฅ์ ํน๋ณํ ๊ท์น์ด ์์ด ๋ ธ๊ฐ๋ค๋ก ๋ฐฉํฅ์ ์ ํด์ฃผ์๋ค.
- ์จ๋ ์กฐ์
๋ชจ๋ ์นธ์ ๋ํด์ ์ธ์ ํ 4์นธ์ ๋ํด ํด๋น์นธ๊ณผ ์ธ์ ํ ์นธ์ (์จ๋ ์ฐจ์ด / 4)๋ฅผ ๋ด๋ฆผํ ๋งํผ ์จ๋๊ฐ ์ค๊ณ ๊ฐ๋ค. ์ด๋ ์จ๋๊ฐ ๋์ ์นธ์์ ๋ฎ์ ์นธ์ผ๋ก ์ด๋ํ๋ค. ์จ๋์ฐจ์ด๊ฐ 4์ด์ ๋์ง์์ผ๋ฉด ํด๋น์ฌํญ์ด ์๋ค.
์ด๋ ์จ๋ ์กฐ์ ์ ๋ชจ๋ ์นธ์ ๋ํด์ ๋์์ ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ์ ๊ฐ์ ๋ฐ๋ก ์กฐ์ ํ์ง ์๊ณ ์กฐ์ ๋ ๊ฐ์ temp๋ผ๋ ๋ฐฐ์ด์ ๋ฏธ๋ฆฌ ๋ด์๋๊ณ ๋ง์ง๋ง์ ํ ๋ฒ์ ์จ๋๊ฐ์ ์กฐ์ ํ๋ค.
์์์ ๋ ์นธ์ ๋ํด ์จ๋ ์กฐ์ ์ 1๋ฒ๋ง ์ผ์ด๋๊ธฐ ๋๋ฌธ์ ์ค๋ณต์ ๋ฐฉ์งํ๊ธฐ ์ํด visited๋ฐฐ์ด์ ๋ง๋ค์ด์ ํด๋น ๋ ์นธ ์ฌ์ด์ ์ด๋ฏธ ์จ๋ ๊ต๋ฅ๊ฐ ์ผ์ด๋ฌ๋์ง๋ฅผ ๊ธฐ๋กํ๋ค.
- ์จ๋ ๊ฐ์
์จ๋๊ฐ 1 ์ด์์ธ ๊ฐ์ฅ ๋ฐ๊นฅ์ชฝ ์นธ์ ์จ๋๊ฐ 1์ฉ ๊ฐ์ํ๋ค๋ผ๊ณ ๋์ด์๋ค. ์ฆ ํ ๋๋ฆฌ์ ๋ํด์ ์จ๋๊ฐ 1์ด์์ด๋ฉด ์จ๋๊ฐ 1 ๊ฐ์ํ๋ค๋ ๋ป์ด๋ค.
๋ฐ๋ณต๋ฌธ์ผ๋ก ํ ๋๋ฆฌ๋ง ๋๋ฉด์ ์จ๋๊ฐ 1 ์ด์์ด๋ฉด ์จ๋๋ฅผ 1 ๋ฎ์ถฐ์ฃผ๋ฉด ๋๋ค.
pair<int, int> direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
pair<int, int> now = { 1,1 };
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) {
for (int j = 1; j < C; j++) {
now = { now.first + direct[i].first, now.second + direct[i].second };
if (Map[now.first][now.second])
Map[now.first][now.second]--;
}
}
else {
for (int j = 1; j < R; j++) {
now = { now.first + direct[i].first, now.second + direct[i].second };
if (Map[now.first][now.second])
Map[now.first][now.second]--;
}
}
}
๋๋ ์ด๋ฐ์์ผ๋ก ๋ฐฉํฅ์ ์ค๋ฅธ์ชฝ, ์๋, ์ผ์ชฝ, ์ ๋ฐฉํฅ์ ์ค์ ํด๋๊ณ now ์ขํ๋ฅผ ํ ์นธ์ฉ ์ด๋ํ๋ฉด์ ํ ๋๋ฆฌ๋ฅผ ๋์๋ค.
- ์จ๋ ์กฐ์ฌ
์จ๋ ์กฐ์ฌ๋ ๋ง ๊ทธ๋๋ก ์ด๊ธฐ์ ๋ฐ์๋ inspect ๋ฐฐ์ด์ ์ด์ฉํด์ ์กฐ์ฌํด์ผํ ์ขํ๋ค์ ์จ๋ ์ค ํ๋๋ผ๋ K๋ฏธ๋ง์ธ ์นธ์ด ์์ผ๋ฉด 1๋ฒ ๊ณผ์ ์ผ๋ก ๋์๊ฐ๊ณ ์๋๋ฉด ํ ์คํธ๋ฅผ ์ค๋จํ๋ค.
ํ ์คํธ ๊ณผ์ ์ ๋ช ๋ฒ ๊ฑฐ์ณค๋์ง ์นด์ดํธํด์ ๋ง์ง๋ง์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค. ์ด๋ ์นด์ดํธ๊ฐ 100์ ๋์ด๊ฐ๊ฒ๋๋ฉด ํ ์คํธ๋ฅผ ์ค๋จํ๊ณ 101์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
<์ฝ๋>
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 20 + 5;
int Map[MAX_N][MAX_N];
vector<pair<pair<int, int>, int>> Fan;
vector<pair<int, int>> inspect;
bool Wall[MAX_N][MAX_N][MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
bool tmp_visited[MAX_N][MAX_N][MAX_N][MAX_N];
int R, C, K, W;
pair<int, int> st_direction[] = { {0,1},{0,-1},{-1,0},{1,0} };
pair<int, int> dir[4][8] = { {{-1,1},{0,1},{1,1},{-1,0},{0,1},{0,1},{1,0},{0,1}}, {{1,-1},{0,-1},{-1,-1},{1,0},{0,-1},{0,-1},{-1,0},{0,-1}},{{-1,-1},{-1,0},{-1,1},{0, -1}, { -1,0 }, { -1,0 }, { 0,1 }, { -1,0 }}, { {1,1},{1,0},{1,-1},{0,1},{1,0},{1,0},{0,-1},{1,0}} };
void input();
void Wind(pair<int, int>, int);
bool is_ok(pair<int, int>);
bool check_temperature();
void Down_temp();
void Adjust_temp();
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
input();
int Chocolate = 0;
while (true) {
for (int i = 0; i < Fan.size(); i++) {
Wind(Fan[i].first, Fan[i].second);
}
Adjust_temp();
Down_temp();
Chocolate++;
if (check_temperature())
break;
else if (Chocolate > 100)
break;
}
cout << Chocolate << "\n";
return 0;
}
void input() {
cin >> R >> C >> K;
for (int i = 1; i <= R; i++)
for (int j = 1; j <= C; j++) {
int num;
cin >> num;
if (num >= 1 && num <= 4) {
Fan.push_back({ {i,j},num - 1 });
}
else if (num == 5)
inspect.push_back({ i,j });
}
cin >> W;
for (int i = 1; i <= W; i++) {
int y, x, wall;
cin >> y >> x >> wall;
if (wall == 1) {
Wall[y][x][y][x + 1] = true;
Wall[y][x + 1][y][x] = true;
}
else if (wall == 0) {
Wall[y][x][y - 1][x] = true;
Wall[y - 1][x][y][x] = true;
}
}
}
void Wind(pair<int, int> st, int Direction) {
queue < pair<pair<int, int>, int>> q;
memset(visited, false, sizeof(visited));
q.push({ {st.first + st_direction[Direction].first, st.second + st_direction[Direction].second}, 5 });
while (!q.empty()) {
pair<int, int> now = q.front().first;
int strength = q.front().second;
q.pop();
if (visited[now.first][now.second])
continue;
visited[now.first][now.second] = true;
Map[now.first][now.second] += strength;
if (strength == 1)
continue;
for (int i = 0; i < 3; i++) {
pair<int, int> next = { now.first + dir[Direction][i].first, now.second + dir[Direction][i].second };
if (!is_ok(next))
continue;
if (visited[next.first][next.second])
continue;
if (i == 0) {
pair<int, int> next_ = { now.first + dir[Direction][3].first, now.second + dir[Direction][3].second };
if (Wall[now.first][now.second][next_.first][next_.second])
continue;
if (Wall[next_.first][next_.second][next_.first + dir[Direction][4].first][next_.second + dir[Direction][4].second])
continue;
q.push({ next, strength - 1 });
}
else if (i == 1) {
pair<int, int> next_ = { now.first + dir[Direction][5].first, now.second + dir[Direction][5].second };
if (Wall[now.first][now.second][next_.first][next_.second])
continue;
q.push({ next,strength - 1 });
}
else if (i == 2) {
pair<int, int> next_ = { now.first + dir[Direction][6].first, now.second + dir[Direction][6].second };
if (Wall[now.first][now.second][next_.first][next_.second])
continue;
if (Wall[next_.first][next_.second][next_.first + dir[Direction][7].first][next_.second + dir[Direction][7].second])
continue;
q.push({ next, strength - 1 });
}
}
}
}
bool is_ok(pair<int, int> pos) {
if (pos.first < 1 || pos.first > R || pos.second < 1 || pos.second > C)
return false;
return true;
}
bool check_temperature() {
for (int i = 0; i < inspect.size(); i++) {
if (Map[inspect[i].first][inspect[i].second] < K)
return false;
}
return true;
}
void Down_temp() {
pair<int, int> direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
pair<int, int> now = { 1,1 };
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) {
for (int j = 1; j < C; j++) {
now = { now.first + direct[i].first, now.second + direct[i].second };
if (Map[now.first][now.second])
Map[now.first][now.second]--;
}
}
else {
for (int j = 1; j < R; j++) {
now = { now.first + direct[i].first, now.second + direct[i].second };
if (Map[now.first][now.second])
Map[now.first][now.second]--;
}
}
}
}
void Adjust_temp() {
int temp[MAX_N][MAX_N];
memset(temp, 0, sizeof(temp));
memset(tmp_visited, false, sizeof(tmp_visited));
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k < 4; k++) {
pair<int, int> next = { i + st_direction[k].first, j + st_direction[k].second };
if (!is_ok(next))
continue;
if (Wall[i][j][next.first][next.second] || Wall[next.first][next.second][i][j])
continue;
if (tmp_visited[i][j][next.first][next.second] || tmp_visited[next.first][next.second][i][j])
continue;
tmp_visited[i][j][next.first][next.second] = true;
tmp_visited[next.first][next.second][i][j] = true;
int Value = (int)floor(abs(Map[next.first][next.second] - Map[i][j]) / 4);
if (Value >= 1) {
if (Map[next.first][next.second] > Map[i][j]) {
temp[i][j] += Value;
temp[next.first][next.second] -= Value;
}
else {
temp[i][j] -= Value;
temp[next.first][next.second] += Value;
}
}
}
}
}
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++)
Map[i][j] += temp[i][j];
}
}
|
cs |
์์ ์์ ํ ์คํธ์ผ์ด์ค๋ฅผ ๋๋ถ๋ถ ์จํ๊ธฐ์ ์์น๋ฅผ ๊ฐ๊ฒ ์ค์ ์ด๋์ ์ค๋ฅ๊ฐ ๋ฌ๋์ง ์ฐพ๊ธฐ๊ฐ ์ด๋ ค์ ๋ค. ํ๋ฆฐ ์ฝ๋์๋๋ฐ๋ ์ฐ์ฐํ ์์ ๋ ๋ค ๋ง๊ฒ๋์์๋ค.
'๋ฐฑ์ค > Platinum' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 5373 : ํ๋น (0) | 2022.05.16 |
---|---|
[BOJ] 10251 : ์ด์ ๋ฉดํ ์ํ (0) | 2022.03.25 |
[BOJ] 1028 : ๋ค์ด์๋ชฌ๋ ๊ด์ฐ (0) | 2022.03.23 |
[BOJ] 13308 : ์ฃผ์ ์ (0) | 2022.03.15 |
[BOJ] 10272 : ํ์๊ธ ์ฌ๋ฅ๊พผ ๊น์ ์ (0) | 2022.03.11 |