-
Notifications
You must be signed in to change notification settings - Fork 1
30-kangrae-jo #117
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
30-kangrae-jo #117
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.
์ฒ์์ ๋ด๋ถ ๊ณต๊ฐ ๋๋ฌธ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํด์ผ ํ๋ ๊ณ ๋ฏผํ์ต๋๋ค.
๊ทธ๋๋ ์์ด๋ฌธ์ ๋ฅผ ํ๋ค์์์ผ๊น์. ๋น๊ต์ ์ฝ๊ฒ ํ ์ ์์์ต๋๋ค.
(0, 0)์ง์ ์์๋ถํฐ bfs๋ก 0์ธ ๊ฐ์ ๋ฐ๋ผ๊ฐ๋ฉด์ ์น์ฆ๊ฐ ์๋ ๊ฐ์ ๋ง๋๋ฉด ํด๋น ์น์ฆ ์ง์ ์ ์ ์ด๋ฉด ๊ฐ์ +1 ํด์ฃผ๊ณ
bfs๊ฐ ๋๋ ์์ ์ ์ ์ด๋ฉด ๊ฐ์ด 2์ด์์ธ ์น์ฆ๋ฅผ ์ ๊ฑฐํด์ฃผ์์ต๋๋ค.
๋ด๋ถ ๊ณต๊ฐ์ bfs๋ก ์ด์ฐจํผ ๋๋ฌํ ์ ์์ผ๋ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋ฉ๋๋ค.
์ด๋ฅผ ์น์ฆ๊ฐ ๋ชจ๋ ์์ด์ง ๋๊น์ง ๋ฐ๋ณตํ์ฌ ๋ต์ ์ป์ ์ ์์์ต๋๋ค.
์ฌ๋ฐ๋ ๋ฌธ์ ์์ต๋๋ค. ์๊ณ ํ์
จ์ต๋๋ค~!!@@
Details
import java.util.*;
public class Main {
static int N, M;
static int[][] map;
static boolean[][] visited;
static int[][] contact;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
map = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
map[i][j] = sc.nextInt();
}
}
visited = new boolean[N][M];
contact = new int[N][M];
int deleteCount = 0;
while (true) {
if (checkMap()) {
bfs(0, 0);
deleteCheese();
deleteCount++;
initArr();
} else {
break;
}
}
System.out.println(deleteCount);
}
static void bfs (int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
visited[x][y] = true;
while (!queue.isEmpty()) {
int[] current = queue.poll();
for (int i = 0; i < 4; i++) {
int nx = current[0] + dx[i];
int ny = current[1] + dy[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
if (map[nx][ny] == 0 && !visited[nx][ny]) {
queue.add(new int[]{nx, ny});
visited[nx][ny] = true;
}
if (map[nx][ny] == 1) contact[nx][ny] += 1;
}
}
}
}
static boolean checkMap() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 1) return true;
}
}
return false;
}
static void deleteCheese() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (contact[i][j] >= 2) map[i][j] = 0;
}
}
}
static void initArr() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
visited[i][j] = false;
contact[i][j] = 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.
์กฐ๊ธ ๋ ์ฌํํ๊ฒ ์ ๊ทผํ ์ ์๋ ๊ฒ์ด, (0, 0) ์์น์์ bfs๋ฅผ ์ํํด ๋น ์นธ์ผ๋ก๋ง ์ด๋ํ ์ ์๊ฒ ํ๋ฉด ์์ฐ์ค๋ฝ๊ฒ 1. ๊ฐ ์น์ฆ ๋ณ ๊ณต๊ธฐ์ ์ ์ดํ ํ์๋ฅผ ํ๋ํ ์ ์์ผ๋ฉฐ 2. ์น์ฆ ์์ชฝ์ ๋น ๊ณต๊ฐ์ ํ์ํ์ง ์๊ฒ ๋ฉ๋๋ค.
vector<vector<int>> GetNumContactsCheeses() {
queue<pair<int, int>> q;
vector<vector<bool>> visited(N, vector<bool>(M, false));
q.emplace(0, 0);
visited[0][0] = true;
vector<vector<int>> numContactsCheeses(N, vector<int>(M, 0));
// ๋น ์นธ์ผ๋ก๋ง ์ด๋ํ ์ ์๋ bfs
while(!q.empty()) {
const auto [x, y] = q.front();
q.pop();
for(const auto& [dx, dy] : OFFSET) {
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny) || visited[nx][ny]) {
continue;
}
if(isCheese[nx][ny]) { // ๋ค์ ์นธ์ด ์น์ฆ๋ผ๋ฉด ๊ณต๊ธฐ์ ๋ง๋ฟ๋ ๊ฒ
numContactsCheeses[nx][ny]++;
} else { // ์๋๋ผ๋ฉด ํ์
visited[nx][ny] = true;
q.emplace(nx, ny);
}
}
}
return numContactsCheeses;
}์ด๋ฐ ์์ผ๋ก ํ ๋ฒ bfs๋ฅผ ์ํํ ๋๋ง๋ค ๋ชจ๋ ์น์ฆ ๋ณ๋ก "๊ณต๊ธฐ์ ๋ช ๋ฒ ์ ์ดํ๋๊ฐ"๋ฅผ ํ๋ํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ด ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก
for(int i = 0; i < N; ++i) {
for(int j = 0; j < M; ++j) {
if(numContactsCheeses[i][j] >= 2) {
isCheese[i][j] = false;
}
}
}2ํ ์ด์ ๊ณต๊ธฐ์ ์ ์ดํ ์น์ฆ๋ง ๋ น์ด๋ฉด(์ฌ๊ธฐ์๋ bool ๊ฐ์ false๋ก ์ธํ ) ๋ฉ๋๋ค.
์ด 2๊ฐ์ง ๊ณผ์ ์ "๋ชจ๋ ์น์ฆ๊ฐ ๋ น์ ๋๊น์ง" ๋ฐ๋ณตํ๊ณ , ๊ทธ ๋ฃจํ ํ์๋ฅผ ์ถ๋ ฅํ๋ฉด ๋ฉ๋๋ค.
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<int>> isCheese;
const vector<pair<int, int>> OFFSET {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
inline bool OutOfBound(int x, int y) {
return x < 0 || x >= N || y < 0 || y >= M;
}
bool IsAllMelted() {
for(int i = 0; i < N; ++i) {
for(int j = 0; j < M; ++j) {
if(isCheese[i][j]) {
return false;
}
}
}
return true;
}
vector<vector<int>> GetNumContactsCheeses() {
queue<pair<int, int>> q;
vector<vector<bool>> visited(N, vector<bool>(M, false));
q.emplace(0, 0);
visited[0][0] = true;
vector<vector<int>> numContactsCheeses(N, vector<int>(M, 0));
while(!q.empty()) {
const auto [x, y] = q.front();
q.pop();
for(const auto& [dx, dy] : OFFSET) {
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny) || visited[nx][ny]) {
continue;
}
if(isCheese[nx][ny]) {
numContactsCheeses[nx][ny]++;
} else {
visited[nx][ny] = true;
q.emplace(nx, ny);
}
}
}
return numContactsCheeses;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
isCheese.resize(N, vector<int>(M));
for(auto& row : isCheese) {
for(auto& col : row) {
cin >> col;
}
}
int tick;
for(tick = 0; !IsAllMelted(); ++tick) {
vector<vector<int>> numContactsCheeses = GetNumContactsCheeses();
for(int i = 0; i < N; ++i) {
for(int j = 0; j < M; ++j) {
if(numContactsCheeses[i][j] >= 2) {
isCheese[i][j] = false;
}
}
}
}
cout << tick;
return 0;
}
๊ทธ๋ ๋ค์. ๊ทธ๋ฅ ์ธ๋ถ์์ bfs ๋๋ฆฌ๊ณ |
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๋ฅผ ๋๋ ธ์ต๋๋ค.
๋ฐฉ๋ฌธํ๋ ์์น๊ฐ ์น์ฆ๋ผ๋ฉด cheese[y][x] = true ํด๋๊ณ , ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์น์ฆ๋ผ๋ฉด ์ ๊ฑฐํด์ผํ ์น์ฆ๋ฆฌ์คํธ์ ์ฝ์
ํ์์ต๋๋ค. ๋ ๋ฒ ๋ฐฉ๋ฌธํ๋ค๋๊ฑด ๊ฐ์ฅ์๋ฆฌ๋ผ๋ ๋ป์ด๋๊น์
์ฌ๋ฐ๋ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค :0 ๊ณ ์ํ์ จ์ด์
package beakjoon;
import java.util.*;
import java.io.*;
public class Sol2638_1 {
static int n, m;
static int[][] board;
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());
m = Integer.parseInt(st.nextToken());
board = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
br.close();
System.out.println(melt());
}
private static int melt() {
int hour = 0;
List<int[]> pos;
while (true) {
pos = bfs();
if (pos.isEmpty()) break;
for (int[] p : pos) {
int x = p[0], y = p[1];
board[y][x] = 0;
}
hour++;
}
return hour;
}
private static List<int[]> bfs() {
List<int[]> corner = new ArrayList<>();
Queue<int[]> q = new ArrayDeque<>();
boolean[][] visited = new boolean[n][m];
boolean[][] cheese = new boolean[n][m];
q.offer(new int[]{0, 0}); // ๊ฐ์ฅ์๋ฆฌ๋ ํญ์ ๋น์ด์๊ธฐ ๋๋ฌธ์
visited[0][0] = true;
while (!q.isEmpty()) {
int x = q.peek()[0];
int y = q.poll()[1];
for (int[] o : offset) {
int nx = x + o[0];
int ny = y + o[1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (visited[ny][nx]) continue;
if (board[ny][nx] == 1) {
if (cheese[ny][nx]) corner.add(new int[]{nx, ny});
cheese[ny][nx] = true;
continue;
}
q.offer(new int[]{nx, ny});
visited[ny][nx] = true;
}
}
return corner;
}
}
wnsmir
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.
์์ด๋ฌธ์ ๋ฅผ ์๋๋ง์ด ํ๋ค๋ณด๋๊น ์ด๋ฌธ์ ๋ 20๋ถ์กฐ๊ธ ์๋์ด ๋คํ์๋ค์
์ฒ์์ ๋ฌธ์ ๋ฅผ ๋์ถฉ์ฝ์ด ๊ณต๊ธฐ์ ์ ์ดํ๋ฉด ์ ๋ถ ํด์ข
๋ฃ์ ์ 0์ผ๋ก ๋ฐ๊ฟ์ฃผ์๋ค๊ฐ ํ๋ ค์ ๋ค์๋ดค์ต๋๋ค.
๋ด๋ถ๊ณต๊ธฐ ์ธ๋ถ๊ณต๊ธฐ๋ ๋ค๋ฅด๋๋ผ๊ตฌ์,,
๋ฌธ์ ๋ ์ฐฉํ๊ฒ ์ธ๋ถํ๋๋ฆฌ๋ฅผ ๋ฌด์กฐ๊ฑด ๊ณต๊ธฐ๋ก ๋๊ฒ ํด๋์์ต๋๋ค.
์ด์ ๋ฅผ ๋ฐ๋ก ํ์
ํ๊ฒ ์ด์ฐจํผ ์ธ๋ถ๊ณต๊ธฐ๋ ํ๋๋ฆฌ์ ์ ํด์๋๊ณณ์ด๋ผ 0,0์์ bfs๋ฅผ ๋๋ฆฌ๋ฉด ์ธ๋ถ๊ณต๊ธฐ๋ ํ๋์ ๋ฉ์ด๋ฆฌ๊ฐ ๋ฉ๋๋ค.
์ฆ ์ธ๋ถ๊ณต๊ธฐ๋ฅผ bfs๋ฅผ ํตํด ํ๋์ ๋ฉ์ด๋ฆฌ๋ก ๋ง๋ค์ด์ฃผ๊ณ , ๋งคํด๋ง๋ค ์ธ๋ถ๊ณต๊ธฐ์ 2๊ฐ์ด์ ์ ์ดํ ์น์ฆ๋ melt ๋ฆฌ์คํธ์ ๋ฃ์ด์ฃผ์ด ๊ฐ์๋ฅผ ์๊ณ ๋ค์ 0์ผ๋ก ๋ฐ๊พธ์ด์ค๋๋ค.
๋ค์ํด์ ๋ค์ 0,0 bfs๋ฅผ 1ํ ์งํํ์ฌ ์ธ๋ถ๊ณต๊ธฐ์ ์ถ๊ฐํด์ฃผ๊ณ ๋ค์ ๋ น์ผ ์น์ฆ๋ฅผ ์ ์ ํด์ฃผ์์ต๋๋ค.
from collections import deque
R, C = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(R)]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
turn = 0
last_cheezes = 0
while True:
# ๋จ์ ์น์ฆ ๊ฐ์ ์ธ๊ธฐ
cheezes = sum(grid[i][j] == 1 for i in range(R) for j in range(C))
if cheezes == 0:
print(turn)
break
last_cheezes = cheezes
turn += 1
# ์ธ๋ถ ๊ณต๊ธฐ๋ง BFS๋ก ํ์
air = [[False]*C for _ in range(R)]
queue = deque()
queue.append((0, 0))
air[0][0] = True
while queue:
x, y = queue.popleft()
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
# ์ธ๋ถ๊ณต๊ธฐ๊ฐ ์๋๋ฉด์ ์ผ๋ฐ๊ณต๊ธฐ๋ผ๋ฉด
if 0 <= nx < R and 0 <= ny < C and not air[nx][ny] and grid[nx][ny] == 0:
air[nx][ny] = True
queue.append((nx, ny))
# ์ธ๋ถ ๊ณต๊ธฐ์ ์ ์ดํ ์น์ฆ ์์น ์์ง
melt = []
for x in range(R):
for y in range(C):
if grid[x][y] == 1:
count = 0
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
if air[nx][ny]:
count += 1
if count >1:
melt.append((x, y))
break
# ์น์ฆ ๋
น์ด๊ธฐ
for x, y in melt:
grid[x][y] = 0
๐ ๋ฌธ์ ๋งํฌ
[์น์ฆ]
โ๏ธ ์์๋ ์๊ฐ
1h 30m...
โจ ์๋ ์ฝ๋
์ค๋๋ง์ด๋ค์.
์ค๋์ ์ฌ๋ฐ๋ ๊ตฌํ ๋ฌธ์ ๋ค๊ณ ์๋ดค์ต๋๋ค.
์์ด์๋ฆฌ์ฆ๋ณด๋ค๋ ํจ์ฌ ์ฝ์ต๋๋ค.
์ ๋ ์ฒ์์ ์ด ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ์ด๋ ๊ฒ ์๊ฐ์ ํ์ด์.
์.. ๊ทผ๋ฐ ์ ๋ ์ด ๋ฐฉ์์ N๊ณผ M์ด ์ต๋ 100์ด๊ธฐ ๋๋ฌธ์ ์๊ฐ ์ด๊ณผ๊ฐ ๋๋ค๊ณ ์๊ฐํ์ด์.
๊ทธ๋ฐ๋ฐ ๊ฒฐ๋ก ๋ง ๋จผ์ ๋ง์๋๋ฆฌ์๋ฉด ์ด๋ ๊ฒ ํด๋ ๋ฉ๋๋ค....
์ ๋ ๊ทธ๊ฑธ ๋ชจ๋ฅด๊ณ ๋บ ๋๋ฌ ๊ฐ๋ค์.
๊ทธ๋๋ ํ๋ฆฐ ํ์ด๋ ์ด์ง ๋ง์๋๋ ค๋ณผ๊ฒ์.
์ ๋ฐฉ์์ ๋ฌธ์ ๋ ์๋์ฝ๋ 1, 2๋ฒ์์ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฌ๊ธฐ ๋๋ฌธ์ด๋ผ๊ณ ์๊ฐํ์ด์.
๊ทธ๋์ ํ์ฌ ๋ น์์์ง ์์ ์น์ฆ๋ฅผ ํ์ ๋ฃ๊ณ , ํ์ฌ "์ธ๋ถ๊ณต๊ธฐ"๋ฅผ ํ์๋ฃ๊ณ ์ด๋ฆฌ์ ๋ฆฌ ๊ตด๋ฆฌ๋... ๊ทธ๋ฐ๊ฑธ ํ์ด์.
ํ ์ผ๋ ๋ง์์ง๋ง, ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋จ๋๋ผ๊ตฌ์.
์๋ง ํน์ ์ผ์ด์ค์์ ํ๊ฐ ๋ฌดํ ํฝ์ฐฝ ํ๋๋ด์..
๊ทธ๋์ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ ๋์ค '์ฒ์์ผ๋ก ์๊ฐํ ๋ฐฉ์์ผ๋ก ํ๋ฒ ํด๋ณด์ ์๊ฐ์ด๊ณผ๊ฐ ๋๋์ง ๊ถ๊ธํ๋ค' ๋ผ๋ ์๊ฐ์ด ๋ฌ์ด์.
๊ตฌํ์ ํด๋ณด๋.. ์ ๋ต์ด์์ต๋๋ค.
์ฌ๋ฌ๋ถ๋ค๋ ์ด๋ ต๊ฒ ์๊ฐํ์ง ๋ง๊ณ ์ฝ๊ฒ ์๊ฐํด์ ๋ฐ๋ก ํ์ด๋ณด์๊ธธ ๋ฐ๋๋๋ค.
+) ์ ์์ฑ๋ ์ฝ๋์์๋ resetAir ๋ ์์ด์ ์๊ฐ๋ณต์ก๋๊ฐ *2 ๊ฐ ์๋๋ผ *3 ์ธ๊ฒ ๊ฐ๋ค์.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
c++์์ 1์ด๋ ์ฝ 100,000,000๋ฒ ์ฐ์ฐ์ด ๋๋ค๊ณ ํ๋ค์.
์ด ๋ฌธ์ ๋ ์ฝ 1,000,000๋ฒ ์ฐ์ฐ์ด ํ์ํ๋ ํต๊ณผ์ธ๊ฐ ๋ด ๋๋ค.