-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8-kokeunho #28
8-kokeunho #28
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¬Έμ λ₯Ό ν΄κ²°νλ λ°©μκ³Ό λ°©ν₯μ μκ°λ³΄λ€ λΉ λ₯΄κ² μ νμμ΅λλ€.
κ·Όλ° κ·Έ μκ°μ΄ ꡬν κ°λ₯ν μκ°μΈμ§ μλ¬Έμ νμμ±λ‘ μΌλ¨ ꡬννμμ΅λλ€.
2μ°¨μ visitedλ°°μ΄μ νλμ© μ΄ν΄λ³΄λ©΄μ λ°©λ¬Έλμ§ μμ κ³³μ΄μλ€λ©΄,
ν΄λΉ μ’νμ λμΌν κ΅κ°λ‘λΆν° bfsλ₯Ό μμνκ³ ,
bfsκ° μ§νλ κ΅κ°λ€μλν΄μ μΈκ΅¬μ΄λμ νκ³ ,
bfsμμ μΈκ΅¬μ΄λμ΄ μΌμ΄λ¬λ€λ©΄, λ€μλ μλ λμΌνκ² μ§ννμ΅λλ€.
μ΄λ° λ‘μ§λ€ μ¬μ΄μ μ½ν bool λ³μλ€μ 컨νΈλ‘€νλλ° μκ°μ μ’ λ§μ΄ μ»μ΅λλ€.
(μ΄λ° 볡μ‘ν λ‘μ§μ μ€λͺ
νλ κ²μ΄ μ’ μ΄λ ΅λ€μ... prμ΄λ 리뷰λ₯Ό νλ©΄μ μ€λͺ
νλ λ₯λ ₯λ λ§μ΄ κΈΈλ¬μΌκ² μ΄μ)
κ·Έλ κ² 1μκ° μ λ μλͺ¨ν΄μ ꡬνμ μλ£νκ³ λ³΄λ.....
μ μ½λκ° μμ²μμ² λμ΄λΈν λλμ΄ μλκ²λλ€.
κ·Έλμ κΉλν μ½λλ ν¬κΈ°νμ... νκ³ κ·ΌνΈλ μ½λλ₯Ό λ΄€λλ°
κ·ΌνΈλ μ½λλ μ μ½λκ° μμ λΉμ·νλ€μ.
κ·Έλ¦¬κ³ λ€λ₯Έ μ¬λλ€μ μ½λλ₯Ό κ²μν΄λ΄€λλ°,
μ‘°κΈμ©μ μ°¨μ΄κ° μμ§λ§ μμ 볡μ‘ν λ‘μ§μΌλ‘ ꡬνν΄λ¨λκ΅°μ.
μ΄ λ¬Έμ λ μλ κ·Έλ° λ¬Έμ μΈ κ² κ°λ€μ.
CPP CODE
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N, L, R;
int offset[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> A;
vector<vector<bool>> visited;
bool canOpen(int x, int y) {
return 0 <= x && x < N && 0 <= y && y < N && !visited[y][x];
}
bool bfs(vector<vector<bool>>& visited, int x, int y) {
bool isMoved = false;
int people = 0;
int country = 0;
queue<pair<int, int>> q, updates;
q.push({y, x});
updates.push({y, x});
visited[y][x] = true;
people += A[y][x];
country++;
while (!q.empty()) {
int cy = q.front().first;
int cx = q.front().second;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int ny = cy + offset[dir][0];
int nx = cx + offset[dir][1];
if (canOpen(nx, ny)) {
int diff = abs(A[cy][cx] - A[ny][nx]);
if (L <= diff && diff <= R) {
isMoved = true;
visited[ny][nx] = true;
q.push({ny, nx});
updates.push({ny, nx});
people += A[ny][nx];
country++;
}
}
}
}
int newPeople = people / country;
while (!updates.empty()) {
int cy = updates.front().first;
int cx = updates.front().second;
updates.pop();
A[cy][cx] = newPeople;
}
return isMoved;
}
int solution(int days) {
while (true) {
visited = vector<vector<bool>>(N, vector<bool>(N, false));
bool isMoved = false;
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
if (!visited[y][x] && bfs(visited, x, y))
isMoved = true;
}
}
if (!isMoved) break;
days++;
}
return days;
}
int main() {
cin >> N >> L >> R;
A = vector<vector<int>>(N, vector<int>(N));
for (int y = 0; y < N; y++)
for (int x = 0; x < N; x++) cin >> A[y][x];
cout << solution(0);
return 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ꡬνμ΄ μ§μ§ μ΄λ ΅λ€μ.. μλμ½λλ λΉ λ₯΄κ² μμ±νμ§λ§ ꡬνμ λ΅ λ΄λ μ΄λ ΅λκ΅°μ
νλ£¨κ° μ§λλ©΄ μλ‘μ΄ visitedλ₯Ό μ¬μ©ν΄μΌνκΈ° λλ¬Έμ μ μλ²μλ‘ μ μΈνκ³ whileλ¬Έ λ΄λΆμμ λ§€λ² μλ‘μ΄ κ°μ²΄λ₯Ό μμ±νλ λ°©λ²μ μ μ μμλ€μ! κ·Έλ¦¬κ³ bfsλ μ λ§ λ€μν κ³³μμ νμ©ν μ μλ€λ κ²μ μ μ μμμ΄μ. μ’μ λ¬Έμ κ°μ¬ν©λλΉ;)
code
//νλμ μ°ν©μ μμ±
bool bfs(int x, int y, vector<vector<bool>>& visited) {
int sum = world[y][x];
queue<pair<int, int>> uni, tmp;
uni.push({ x, y }); tmp.push({ x, y });
visited[y][x] = true;
while (!uni.empty()) {
int cx = uni.front().first;
int cy = uni.front().second;
uni.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = cx + offset[dir][0];
int ny = cy + offset[dir][1];
if (0 <= nx && nx < n && 0 <= ny && ny < n && !visited[ny][nx]) {
int diff = abs(world[cy][cx] - world[ny][nx]);
if (l <= diff && diff <= r) {
visited[ny][nx] = true;
uni.push({ nx, ny }); tmp.push({ nx, ny });
sum += world[ny][nx];
}
}
}
}
//μ°ν©μ μμ±νμ§ λͺ»ν κ²½μ°
if (tmp.size() == 1) return false;
//μΈκ΅¬ μ΄λ
int newPop = sum / tmp.size();
while (!tmp.empty()) {
int x = tmp.front().first;
int y = tmp.front().second;
tmp.pop();
world[y][x] = newPop;
}
return true;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ·Έλν λ¬Έμ λ BFSλ₯Ό μ¬μ©ν κ²μΈμ§ BFSλ₯Ό μ¬μ©ν κ²μΈμ§ κ²°μ νλκ²μ΄ μ€μνλ€κ³ μκ°ν©λλ€.
μ΄ λ¬Έμ μμλ κ΅κ²½μ μ΄ μ΄λ¦¬λ λͺ¨λ λλΌλ₯Ό λμμ νμν μ μλ BFSκ° μ μ νλ€κ³ νλ¨νμ΅λλ€.
μμν κ΅κ°λ₯Ό (0,0)μ μΆκ°νκ³ , μΈμ ν λλΌ μ€ μ‘°κ±΄μ λ§μ‘±νλ©΄, νμ μΆκ°νκ³ νλ² μ λΆ νμν λκΉμ§ ν루μ΄λ©° λμ΄μ μλ λκΉμ§ λ°λ³΅νλ λ‘μ§μΌλ‘ ꡬννμμ΅λλ€.
μ 리νμλ©΄,
BFSλ‘ ν루λμ μ°ν©μ νμνλ©° 쑰건μ λ§μ‘±νλ μ°ν©μ νμ±νλ€.
μ‘°κ±΄μ΄ λ§λ λλΌλ€ λΌλ¦¬ μ°ν©μ λ§λ€κ³ , μΈκ΅¬λ₯Ό μ¬λΆλ°°νλ€.
νλ£¨κ° μ§λλ μ°ν©μ΄ λ§λ€μ΄μ§μ§ μμΌλ©΄ whileλ¬Έμ μ’
λ£νκ³ dayλ₯Ό μΆλ ₯νλ€.
μμ¦ κ·Έλνλ¬Έμ μ μνμ΄λ³΄κ³ μμλλ° ν λ€μ΄μμ μκ°μ΄ μ’ κ±Έλ Έμ΅λλ€ γ ,,
from collections import deque
n, l, r = map(int, input().split())
graph = []
for i in range(n):
graph.append(list(map(int, input().split())))
# BFS νμ ν¨μ
def bfs(x, y, n, l, r, graph, visited):
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # μνμ’μ° μ΄λ
queue = deque([(x, y)])
union = [(x, y)]
population_sum = graph[x][y]
visited[x][y] = True
while queue:
cx, cy = queue.popleft()
for dx, dy in directions:
nx, ny = cx + dx, cy + dy
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
if l <= abs(graph[cx][cy] - graph[nx][ny]) <= r:
visited[nx][ny] = True
queue.append((nx, ny))
union.append((nx, ny))
population_sum += graph[nx][ny]
return union, population_sum
# μΈκ΅¬ μ΄λ μ²λ¦¬ ν¨μ
def human_move(n, l, r, graph):
days = 0
while True:
visited = [[False] * n for _ in range(n)]
movement = False
for i in range(n):
for j in range(n):
if not visited[i][j]:
# μ°ν© μ°ΎκΈ°
union, total_population = bfs(i, j, n, l, r, graph, visited)
if len(union) > 1: # μ°ν©μ΄ νμ±λ κ²½μ°
movement = True
new_population = total_population // len(union)
for ux, uy in union:
graph[ux][uy] = new_population
if not movement:
break
days += 1
return days
print(human_move(n, l, r, graph))
π λ¬Έμ λ§ν¬
[BOJ](μΈκ΅¬ μ΄λ)https://www.acmicpc.net/problem/16234
βοΈ μμλ μκ°
2h
β¨ μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μκ³ λ¦¬μ¦ μ€ν°λνλ©΄μ λ§μ΄ λ³Έ BFSμΈλ° μ΄λ²μλ κ²μνλ©° νμμ΅λλ€...
λΉμ·ν μ νμ κ³μ νμ΄λ΄μΌκ² μ΅λλ€.
μ΄λ κ² λ―μ€κ² μκΈ΄ forλ¬Έμ μ΄λ²μ μκ² λμλλ°,
μκΈ΄νκ² μΈ μ μμ κ² κ°μ΅λλ€.
union 리μ€νΈμ κ° μμλ ν μ΄λ‘ μ΄λ£¨μ΄μ Έμλλ°
μ΄λ₯Ό country[0] (ν), country[1] (μ΄)μ κ°κ° λλμ΄ λ΄λ κ²μ λλ€.