-
Notifications
You must be signed in to change notification settings - Fork 1
33-wnsmir #135
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
Conversation
kokeunho
left a comment
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.
์์ ์ ํ์๋ ๋ฌธ์ ์์ต๋๋ค.
๊ทธ๋์ ๊ตฌํ์ ์๋ก ํด๋ณด์ง ์์์ต๋๋ค ใ
ใ
;
์ ์์ ํ์ด๋ฅผ ๋ณด๊ธฐ ์ ์ ์ด๋ป๊ฒ ํ์ง ์๊ฐํด๋ดค๋๋ฐ์.
ํ์ด๋ฒ์ ์ ๋๋ก ๋ ์ฌ๋ฆด ์ ์์์ต๋๋ค.
N * N ๋งต์ ์ ์ฒด์์ ๋ชจ๋ ์นธ์ ๋ฐฉ๋ฌธํ ๋๊น์ง bfs๋ฅผ ์๋ํฉ๋๋ค.
๋ชจ๋ ์นธ์ ๋ฐฉ๋ฌธํ๋ค๋ฉด day count๋ฅผ +1 ํด์ค๋๋ค.
bfs๋ฅผ ํด์๋ ํ์ฌ ์นธ ์ฌ๋ฐฉ์ผ๋ก ์ฐํฉ์ด ๋ ์ ์๋์ง ํ์ธํ๊ณ
๊ฐ๋ฅํ๋ค๋ฉด bfs ํ์ ์ฐํฉ์ ์ขํ ์ ๋ณด๋ฅผ ๋ด๋ union ๋ฆฌ์คํธ์ ์ขํ๋ฅผ ๋ฃ์ต๋๋ค.
๋ง์ฝ ์ฐํฉ์ size๊ฐ 1์ด๋ผ๋ฉด 0์ ๋ฐํํ๊ณ 1๋ณด๋ค ํฌ๋ค๋ฉด ์ฐํฉ์ ์ด ์ธ๊ตฌ์๋ฅผ ๋ฐํํฉ๋๋ค.
๊ทธ๋ ๊ฒ ๋ฐํ ๋ฐ์ ์ด ์ธ๊ตฌ์๊ฐ 0๋ณด๋ค ํฌ๋ค๋ฉด ์ฐํฉ์ size๋ก ๋๋์ด ๊ฐ ์ฐํฉ์ ๋ถ๋ฐฐํฉ๋๋ค.
0์ด๋ผ๋ฉด ์ธ๊ตฌ ์ด๋์ ๋ฉ์ถ๊ณ day count๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
ํ ๋ฒ ํผ ๋ฌธ์ ๋ฅผ ๋์๋ณผ ์ ์์์ต๋๋ค.
๋๋ถ์ ์ ์๋ ์ฝ๋๋ ๋ค์ ๋๋์๋ณผ ์ ์์๋ค์.
์๊ณ ํ์
จ์ต๋๋ค!
Details
import java.sql.SQLOutput;
import java.util.*;
public class Main {
static int N, R, L;
static int[][] map;
static boolean[][] visited;
static int[][] direction = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
L = sc.nextInt();
R = sc.nextInt();
map = new int[N][N];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
map[i][j] = sc.nextInt();
}
}
int day_count = 0;
while(true) {
visited = new boolean[N][N];
boolean moved = false;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(!visited[i][j]) {
List<int[]> union = new ArrayList<> ();
int populationSum = bfs(i, j, union);
if(populationSum > 0) {
moved = true;
int newPopulation = populationSum / union.size();
for(int[] country : union) {
map[country[0]][country[1]] = newPopulation;
}
}
}
}
}
if(!moved) {
break;
}
day_count++;
}
System.out.println(day_count);
}
private static int bfs(int x, int y, List<int[]> union) {
Queue<int[]> queue = new LinkedList<> ();
queue.add(new int[]{x, y});
union.add(new int[]{x, y});
visited[x][y] = true;
int populationSum = map[x][y];
while(!queue.isEmpty()) {
int[] now_position = queue.poll();
int nowX = now_position[0];
int nowY = now_position[1];
for(int[] dir : direction) {
int nx = nowX + dir[0];
int ny = nowY + dir[1];
if(nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {
int diff = Math.abs(map[nowX][nowY] - map[nx][ny]);
if(diff >= L && diff <= R) {
visited[nx][ny] = true;
queue.add(new int[]{nx, ny});
union.add(new int[]{nx, ny});
populationSum += map[nx][ny];
}
}
}
}
return union.size() > 1 ? populationSum : 0;
}
}
kangrae-jo
left a comment
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.
์ ๋ ๋ณด๋๊น 9๋ฌ์ ์ ํ์๋ ๋ฌธ์ ๋ผ๊ณ ๋จ๋๋ผ๊ตฌ์.
์ค๋๋ง์ ๋ค์ ํ์ด๋ณด๋๊น ์ ๋ 20๋ถ-25๋ถ ์ ๋ ๊ฑธ๋ฆฌ๋ค์.
๋งํ์์ด ํ์ดํ๋ง ํ ๊ฒ ๊ฐ์๋ฐ... ์๊ฐ์ด ๊ฝค๋ ํ๋ฅด๋ค์.
๊ทธ๋ฆฌ๊ณ ์์ ์ ์๋ง gpt๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๊ฐ์๋ฐ... ์ด์ 20๋ถ๋ ์ปท ๋ผ์์์ด์ ๊ธฐ๋ถ์ด ์ข์ต๋๋ค.
๊ณจ๋ 4์ง๋ง ๋ง์ด์ฃ ใ
ใ
์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค.
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
const int OFFSET[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> board;
int N, L, R;
bool isIn(int y, int x) { return 0 <= y && y < N && 0 <= x && x < N; }
bool canOpen(int a, int b) {
int diff = abs(a - b);
return L <= diff && diff <= R;
}
bool move(vector<vector<bool>>& visited, int sY, int sX) {
queue<pair<int, int>> q;
q.push({sY, sX});
visited[sY][sX] = true;
queue<pair<int, int>> temp;
int people = 0;
bool isMoved = false;
while (!q.empty()) {
auto [y, x] = q.front();
q.pop();
people += board[y][x];
temp.push({y, x});
for (int dir = 0; dir < 4; dir++) {
int y_ = y + OFFSET[dir][0];
int x_ = x + OFFSET[dir][1];
if (isIn(y_, x_) && !visited[y_][x_] && canOpen(board[y][x], board[y_][x_])) {
q.push({y_, x_});
visited[y_][x_] = true;
isMoved = true;
}
}
}
int moved = people / temp.size();
while (!temp.empty()) {
auto [y, x] = temp.front();
temp.pop();
board[y][x] = moved;
}
return isMoved;
}
int main() {
cin >> N >> L >> R;
board = vector<vector<int>>(N, vector<int>(N, 0));
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
cin >> board[y][x];
}
}
int answer = 0;
while (true) {
bool isMoved = false;
vector<vector<bool>> visited(N, vector<bool>(N, false));
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
if (!visited[y][x] && move(visited, y, x)) {
isMoved = true;
}
}
}
if (!isMoved) break;
else answer ++;
}
cout << answer << '\n';
return 0;
}
9kyo-hwang
left a comment
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๋ ์ ์ ์ฒ์ ํ์์ ๋ 556ms๋ก ํต๊ณผํ์๋๋ฐ, 3๋ฌ ์ ์ ๋ค์ ํ์์ ๋ 20ms๋ก ํต๊ณผํ๋ค์(์ด์ผ ํ์ง?)...
๊ธฐ์ต์ด ์๋์ ์ฝ๋๋ง ๋์ ธ๋ด ๋๋ค ํํ
์ฐธ๊ณ ๋ก ์๊ฑฐ ๋ค์ด๋ฒ ๊ธฐ์ถ๋ก ์๊ณ ์์ต๋๋ค. ๊ทธ๋์ ๊ฑฐ์ ํ์ ๋ฌธ์ ์ค ํ๋์ฃ :)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using FVector2 = pair<int, int>;
const vector<FVector2> Offset
{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
};
int N, L, R;
vector<vector<int>> A;
vector<vector<bool>> Visited;
queue<FVector2> PositionQ;
bool OutOfBound(int x, int y)
{
return x < 0 || x >= N || y < 0 || y >= N;
}
bool ShouldOpenBorderBetween(int x, int y, int nx, int ny)
{
int PopulationDifferences = abs(A[x][y] - A[nx][ny]);
if(L <= PopulationDifferences && PopulationDifferences <= R)
{
return true;
}
return false;
}
bool BFS(int StartX, int StartY)
{
Visited[StartX][StartY] = true;
queue<FVector2> Q;
Q.emplace(StartX, StartY);
vector<FVector2> OpenedCells;
int SumPopulation = 0;
bool bOpenBorder = false;
while(!Q.empty())
{
const auto [x, y] = Q.front(); Q.pop();
OpenedCells.emplace_back(x, y);
SumPopulation += A[x][y];
for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny) || Visited[nx][ny])
{
continue;
}
if(ShouldOpenBorderBetween(x, y, nx, ny))
{
Visited[nx][ny] = true;
Q.emplace(nx, ny);
bOpenBorder = true;
}
}
}
if(!bOpenBorder)
{
return false;
}
int Population = SumPopulation / OpenedCells.size();
for(const auto& [x, y] : OpenedCells)
{
A[x][y] = Population;
PositionQ.emplace(x, y);
}
return true;
}
bool ShouldMigration(int x, int y)
{
for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny))
{
continue;
}
if(ShouldOpenBorderBetween(x, y, nx, ny))
{
return true;
}
}
return false;
}
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
cin >> N >> L >> R;
A.resize(N, vector<int>(N));
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < N; ++j)
{
cin >> A[i][j];
PositionQ.emplace(i, j);
}
}
int Day = 0;
while(true)
{
Visited.assign(N, vector<bool>(N, false));
bool bPopulationMoved = false;
for(int Repeat = PositionQ.size(); Repeat > 0; --Repeat)
{
const auto [x, y] = PositionQ.front(); PositionQ.pop();
if(Visited[x][y] || !ShouldMigration(x, y))
{
continue;
}
if(BFS(x, y))
{
bPopulationMoved = true;
}
}
if(!bPopulationMoved)
{
break;
}
++Day;
}
cout << Day;
return 0;
}
g0rnn
left a comment
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๋ก ์ฐํฉ์ ๊ตฌ์ฑํ๊ณ flatten์ผ๋ก ์ธ๊ตฌ ์๋ฅผ ๋ง์ถฅ๋๋ค. flatten์ ์ํํ ๋ ์ด์ ๊ณผ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์์ ๋๋ ๊ฒฝ์ฐ ์ธ๊ตฌ ์ด๋์ผ๋ก ๊ฐ์งํ๊ณ moved ํ๋๊ทธ๋ฅผ ๋ณ๊ฒฝ์์ผ ์ธ๊ตฌ ์ด๋ ์ข ๋ฃ๋ฅผ ํ์ธํฉ๋๋ค.
์ข ๋ ์ง์คํด์ ํ์๋ค๋ฉด ๊ธ๋ฐฉ ํ์์๊ฒ ๊ฐ๋ค์ ์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋น
package beakjoon;
import java.io.*;
import java.util.*;
public class Sol16234 {
static int n, l, r;
static int[][] city;
static int[][] offset = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
l = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
city = new int[n][n];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
city[i][j] = Integer.parseInt(st.nextToken());
}
}
int answer = 0;
boolean[][] visited;
while (true) {
// ๋ชจ๋ ๋์๋ฅผ ์ํํ๋ฉฐ
// ์ฐ๊ฒฐ๊ฐ๋ฅํ๋ฉด ์ฐ๊ฒฐํ๊ณ
// ์ธ๊ตฌ ์ด๋
visited = new boolean[n][n];
boolean isMoved = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j]) continue;
List<int[]> part = bfs(j, i, visited, city);
isMoved = isMoved | flatten(city, part);
}
}
if (!isMoved) {
break;
}
answer++;
}
System.out.println(answer);
br.close();
}
private static List<int[]> bfs(int x, int y, boolean[][] visited, int[][] city) {
List<int[]> result = new ArrayList<>();
Queue<int[]> q = new ArrayDeque<>();
q.add(new int[]{x, y});
result.add(new int[]{x, y});
visited[y][x] = true;
while (!q.isEmpty()) {
int cx = q.peek()[0];
int cy = q.poll()[1];
for (int[] o : offset) {
int nx = cx + o[0];
int ny = cy + o[1];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (visited[ny][nx]) continue;
int diff = Math.abs(city[cy][cx] - city[ny][nx]);
if (!(l <= diff && diff <= r)) continue;
q.add(new int[]{nx, ny});
result.add(new int[]{nx, ny});
visited[ny][nx] = true;
}
}
return result;
}
private static boolean flatten(int[][] city, List<int[]> part) {
boolean moved = false;
int size = part.size();
if (size == 1) return moved;
int sum = 0;
for (int i = 0; i < size; i++) {
int x = part.get(i)[0];
int y = part.get(i)[1];
sum += city[y][x];
}
final int avg = sum / size;
for (int[] p : part) {
if (city[p[1]][p[0]] != avg) moved = true;
city[p[1]][p[0]] = avg;
}
return moved;
}
}
๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/16234
โ๏ธ ์์๋ ์๊ฐ
25min
โจ ์๋ ์ฝ๋
์ค๋๋ง์ ์ฌ์ด ๊ตฌํ๋ฌธ์ ๋ค๊ณ ์์ต๋๋ค.
์ด ๋ฌธ์ ์ ํต์ฌ์ ๋ถ์ด์๋ ๊ตญ๊ฐ๋ค์ ๊ตญ๊ฒฝ์ ์ฌ๋๊ฒ์ ์ด๋ป๊ฒ ํํํ ๊ฒ์ธ๊ฐ ์ ๋๋ค.
์ ๋ ์ ์ผ์ฒ์ ๋ ์ฌ๋ฆฐ๊ฒ bfs์์ต๋๋ค. ์ํ์ข์ฐ๋ฅผ ํ์ํ๋๋ฐ ๊ทธ๋งํ๊ฒ ์๊ธฐ ๋๋ฌธ์ด์ฃ .
๊ทธ๋ฌ๋ ๊ธฐ์กด BFS์ ๊ทธ๋ฆฌ๋๋ฒ์์ visited์กฐ๊ฑด ์ด์ธ์ ์ฐจ์ด์ ์ ๋๊ฐ์ด R๊ณผ L์ฌ์ด์ ๊ฐ ์์ ๋ค์ด์ค๋๊ฒ๋ง queue์ appendํ๊ณ visited์ ์ถ๊ฐํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ต๋๋ค.
์ดํ ์ถ๊ฐํ๋ฉด์ ํด๋น ๋ฐ๋ณตํด์ BFS๋ฉ์ด๋ฆฌ์ ๋ค์ด๊ฐ๋ ๋ชจ๋ ์ขํ๋ค์ ๋ฐ๋ก ๋ฆฌ์คํธ์ ์ถ๊ฐํด์ฃผ๊ณ , ํด๋น ํด์ด ๋๋ ๋ ๊ทธ ๋ธ๋ก๋ค์ ํ๊ท ๊ฐ์ผ๋ก
land(์๋ ์ขํ)๋ฅผ ์ ๋ฐ์ดํธํด์ฃผ๊ณ ๋ค์ํด์ผ๋ก ๋์ด๊ฐ๋๋ค.
๋งคํด i, j ๋ฅผ ๋ชจ๋ bfs๋ฅผ ๋๊ฒ ์์ผฐ๋๋ 80%์ ์๊ฐ์ด๊ณผ๊ฐ ๋์ ๋ฐ๋ณต๋ฌธ ์์์ visited[i][j]๊ฐ True๋ฉด continue๋๋๋ก ๋ฐ๊ฟ ํต๊ณผํ์ต๋๋ค.
์ค๋๋ง์ ์ํ๊ณ ๊ฐ์ญ์์!