-
Notifications
You must be signed in to change notification settings - Fork 2
24-hyeokbini #83
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
24-hyeokbini #83
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.
์ ๋ ์ข ์์ ๋ค๋ฅธ ํ์ด๋ก ์ ๊ทผํ์ต๋๋ค.
์ผ๋จ land์ ๊ฐ์ BFS๋ก ๊ตฌํ ๊ฐ ์ปดํฌ๋ํธ์ ํฌ๊ธฐ๋ก ์ฑ์๋๋ค.
์๋ฅผ ๋ค์ด

์ด ์์์ธ ๊ฒฝ์ฐ
12 0 3 0 2 2
12 0 3 0 0 0
12 0 3 0 0 1
12 0 0 2 0 0
12 0 0 2 0 1
12 0 0 0 0 0
12 12 12 12 12 12
์ด๋ฐ ์์ผ๋ก land ๋ฐฐ์ด์ ๊ฐฑ์ ํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ง์ ๋ชจ๋ ์ด์ ๋ํด์ ์ธ๋ก๋ฐฉํฅ์ผ๋ก ํ์์ ํ๋๋ฐ ์๋ก ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ผ๋ฆฌ๋ง ๊ฐ์ ๋ํด์ผํ๊ธฐ ๋๋ฌธ์ visited ๋ฐฐ์ด์ ์ด์ฉํด ๊ฐ ์ปดํฌ๋ํธ๋ ์๋ก ๋ค๋ฅธ ์ซ์๋ฅผ ๊ฐ์ง๋๋ก comp_id๋ฅผ ๋ถ์ฌํ์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ ์์์ ๊ฒฝ์ฐ
1 0 2 0 3 3
1 0 2 0 0 0
1 0 2 0 0 4
1 0 0 5 0 0
1 0 0 5 0 6
1 0 0 0 0 0
1 1 1 1 1 1
์ด๋ ๊ฒ ๋ฉ๋๋ค.
์ด๋ ๊ฒ ํ์ผ๋ฉด ์ธ๋ก ๋ฐฉํฅ์ผ๋ก ๋ชจ๋ ํ์ํ๋ฉด์ ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ํด๋นํ๋ land์ ๊ฐ์ ๋ชจ๋ ๋ํ๋ฉด ๋ฉ๋๋ค.
์ฝ๋๋ ์ข ์ฅํฉํ๊ธด ํฉ๋๋ค.
from collections import deque
def solution(land):
dy, dx = [-1, 0, 1, 0], [0, 1, 0, -1]
n, m = len(land), len(land[0])
visited = [[0 for _ in range(m)] for _ in range(n)]
comp_id = 0
for i in range(n):
for j in range(m):
if land[i][j] != 1 or visited[i][j]:
continue
# 1 (์์ )๋ฅผ ๋ฐ๊ฒฌํ๋ฉด BFS ์์
comp_id += 1
queue = deque([(i, j)])
visited[i][j] = comp_id
cells = [(i, j)]
while queue:
y, x = queue.popleft()
for k in range(4):
ny, nx = y + dy[k], x + dx[k]
if 0 <= ny < n and 0 <= nx < m and not visited[ny][nx] and land[ny][nx] == 1:
queue.append((ny, nx))
visited[ny][nx] = comp_id
cells.append((ny, nx))
# ์์ ์ปดํฌ๋ํธ์ ๊ฐ์ ์ปดํฌ๋ํธ์ ํฌ๊ธฐ๋ก ์์ ํจ
size = len(cells)
for y, x in cells:
land[y][x] = size
# ๋ชจ๋ ์ด์ ๋ํด์ ์์ ์ ์์ ๊ณ์ฐ
answer = -1
for i in range(m):
seen = set()
temp = 0
for j in range(n):
cid = visited[j][i]
if cid != 0 and cid not in seen:
seen.add(cid)
temp += land[j][i]
answer = max(answer, temp)
return answer์ค์ ์ ๋ฆฌ
์ด ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ํ ๊ฐ์ง ์ค์๋ฅผ ํด์ ์๊ฐ์ ์ก์๋จน์๋๋ฐ
๋ฌด์จ ์ค์๋ฅผ ํ ๊ฑด์ง ๋ณด์ด์๋์?
์ ์ผ ์์ for ๋ฌธ์ผ๋ก ์ฌ์ฉํ ๋ณ์๋ก i, j๋ฅผ ์ด๋ฏธ ์ฌ์ฉํด ๋๊ณ ๋ฐ์์ ๋ค์ i ์ธ๋ฑ์ค ๋ณ์๋ฅผ ์ฌ์ฉํ์ต๋๋ค.
์ด๋ฐ ์ค์๋ ์ ๋๋ก ํ๋ฉด ์๋๊ฒ ์ต๋๋ค!!
๋ฌธ์ ํผ๋ค๊ณ ์๊ณ ํ์ต๋๋ค!!!!!
|
์ ๋ ๋ฐ๋ก ๋ฉ์ด๋ฆฌ ๋ณ๋ก index๋ฅผ ๋ถ์ฌ์ฃผ์ด์ ํ์ดํ์ต๋๋ค. index๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์๋๋ ์์ ๊ฐ ์์ ๋ 1์ด์ง๋ง -1๋ก ์ด๊ธฐํํด์ฃผ๋ ์์
์ ์ฒ์์ ํ์ต๋๋ค. ๋ํ 0์ ์์ ๊ฐ ์๋ ๊ฒ์ ์๋ฏธํ๋ฏ๋ก index๋ฅผ 1๋ถํฐ ์ฌ์ฉํ์ต๋๋ค. ov๋ผ๋ ๋ฐฐ์ด์ index๋ณ๋ก ๋ฉ์ด๋ฆฌ์ ํฌ๊ธฐ๋ฅผ ์ ์ฅํด๋์ต๋๋ค. ์์ ์์ถ#include<bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int solution(vector<vector<int>> v) {
vector<int> ov;
ov.push_back(0); // ๋ถํ์ํ ๊ฐ
int n = v.size(), m = v[0].size();
int idx = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(v[i][j] == 1) {
v[i][j] = -1;
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(v[i][j] == -1) {
v[i][j] = idx;
int size = 1; // ๋ฉ์ด๋ฆฌ ํฌ๊ธฐ
queue<pair<int, int>> q;
q.push({i, j});
while(!q.empty()) {
auto tp = q.front(); q.pop();
for(int d = 0; d < 4; d++) {
int xx = tp.first + dx[d];
int yy = tp.second + dy[d];
if(xx >=0 && xx < n && yy >=0 && yy < m && v[xx][yy] == -1) {
v[xx][yy] = idx;
size++;
q.push({xx, yy});
}
}
}
ov.push_back(size);
idx++;
}
}
}
int res = 0;
for(int i = 0; i < m; i++) {
vector<bool> visited(ov.size(), false);
int sum = 0;
for(int j = 0; j < n; j++) {
int temp = v[j][i];
if(temp > 0 && !visited[temp]) {
visited[temp] = true;
sum += ov[temp];
}
}
res = max(res, sum);
}
return res;
}
|
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.
DFS๋ก ์์ ๋ฉ์ด๋ฆฌ๋ฅผ ๋ถ๋ฆฌํ๊ณ ํฌ๊ธฐ์ ์ด ๋ฒ์๋ฅผ ๊ตฌํ ๋ค, ๋ฐฐ์ด๊ณผ prefix sum ๊ธฐ๋ฒ์ผ๋ก ๊ฐ ์ด์ ์์ ๋์ ํจ์จ์ ์ผ๋ก ๊ณ์ฐํ๋ ํ์ด๋ฅผ ์ฌ์ฉํ์ต๋๋ค
์์ ์์ถ
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int nRows, nCols;
vector<vector<int>> landMap;
vector<vector<bool>> visited;
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
vector<long long> colDiff;
int dfs(int x, int y, int &minCol, int &maxCol) {
stack<pair<int,int>> st;
st.push({x, y});
visited[x][y] = true;
int compSize = 0;
while (!st.empty()) {
auto p = st.top();
st.pop();
int cx = p.first;
int cy = p.second;
compSize++;
if (cy < minCol) {
minCol = cy;
}
if (cy > maxCol) {
maxCol = cy;
}
for (int dir = 0; dir < 4; dir++) {
int nx = cx + dx[dir];
int ny = cy + dy[dir];
if (nx < 0 || nx >= nRows || ny < 0 || ny >= nCols) {
continue;
}
if (visited[nx][ny]) {
continue;
}
if (landMap[nx][ny] == 0) {
continue;
}
visited[nx][ny] = true;
st.push({nx, ny});
}
}
return compSize;
}
long long solution(vector<vector<int>> land) {
nRows = land.size();
nCols = land[0].size();
landMap = land;
visited.assign(nRows, vector<bool>(nCols, false));
colDiff.assign(nCols + 1, 0);
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
if (!visited[i][j] && landMap[i][j] == 1) {
visited[i][j] = true;
int minCol = j;
int maxCol = j;
int compSz = dfs(i, j, minCol, maxCol);
colDiff[minCol] += compSz;
if (maxCol + 1 < nCols) {
colDiff[maxCol + 1] -= compSz;
}
}
}
}
long long ans = 0;
long long running = 0;
for (int col = 0; col < nCols; col++) {
running += colDiff[col];
if (running > ans) {
ans = running;
}
}
return ans;
}
๋ฌธ์ ํธ๋๋ผ ๊ณ ์ํ์ จ์ด์
๐ ๋ฌธ์ ๋งํฌ
PCCP ๊ธฐ์ถ ๋ฌธ์ ๋ ๋ฒจ 2
์์ ์์ถ
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
โจ ์๋ ์ฝ๋
๋ฌธ์ ์์ ์๊ตฌํ๋ ๋ด์ฉ์ ํฌ๊ฒ ์ด๋ ต์ง ์์ต๋๋ค. ์ง๊ด์ ์ผ๋ก ๋ ์ค๋ฅด๋ ํ์ด๋ ํ ๊ฐ๋ก ์ธ๋ฑ์ค์ ๋ํด ๋ชจ๋ ๋์ด์์ BFS๋ฅผ ๋๋ ค ๋ณด๊ณ , ์์ ๊ฐ์ ๋ชจ๋ ๋ํ ๋ค์ ๋ต ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ ๋ก์ง์ผ๋ก ์ถฉ๋ถํ ๋ต์ ๊ตฌํ ์ ์์ต๋๋ค. ๋ฐฑ์ค 1926 ๊ทธ๋ฆผ์ ํ์ด์ ์ ์ฌํ๊ฒ ๋ค์.
์ํ์ฐฉ์ค
ํ์ง๋ง ๋จ์ํ๊ฒ ์ ๋ด์ฉ์ ๊ทธ๋๋ก ๊ตฌํํด์ ๋ชจ๋ ์ง์ ์์ BFS๋ฅผ ๋๋ฆฐ๋ค๋ฉด, ์ ํ์ฑ ํ ์คํธ๋ 100์ ์ด์ง๋ง ํจ์จ์ฑ ํ ์คํธ์์ 0์ ์ ๋ฐ์ต๋๋ค.
์์ ์์ถ / ์๊ฐ์ด๊ณผ
์ด๋ป๊ฒ ๋ณด๋ฉด ๋น์ฐํฉ๋๋ค. ํ ๊ฐ๋ก ์ธ๋ฑ์ค์ ๋ํด์๋ visited๋ฅผ ์ฌ์ฉํด์ ์ต์ ํ๋ฅผ ์งํํ๋ค ํด๋, ๋ค์ ๊ฐ๋ก ์ธ๋ฑ์ค๋ก ๋์ด๊ฐ๋ค๋ฉด ์ฒ์๋ถํฐ ๋ค์ ํ์ํ๋ ๋นํจ์จ์ฑ์ด ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ฐ๋ผ์ ์ด๋ป๊ฒ๋ ์ต์ ํ๋ฅผ ์์ผ์ผ ํ๋๋ฐ, ์ ๋ ์ด์ง ์ ๋ํน..? ๋๋์ผ๋ก ์ต์ ํ๋ฅผ ์งํํ์ต๋๋ค.
์ต์ ํ
๋ฌธ์ ์์ ๊ธฐ์ค์ ์ผ๋ก ์ก๋ ๊ฒ์ ํ ๊ฐ๋ก ์ธ๋ฑ์ค์ ๋ํ ๋ชจ๋ ์ธ๋ก ์ธ๋ฑ์ค์์ ์ป์ ์ ์๋ ์์ ๋ฉ์ด๋ฆฌ ์ ์ ๋๋ค.
land ๋ฒกํฐ์ ๊ฐ๋ก ๊ธธ์ด๋ฅผ ๊ฐ์ง๋ v ๋ฒกํฐ๋ฅผ ๋ง๋ค์ด์ค ํ, BFS๋ฅผ ๋ชจ๋ ์ง์ ์ ๋ํด 1ํ ์งํํ๋ฉด์ ์์ ๊ฐ ๊ฐ์ง๋๋ค๋ฉด ํด๋น ์์ ๋ฉ์ด๋ฆฌ์ ๊ฐ๋ก ์ต์ / ์ต๋ ์ธ๋ฑ์ค(ymin, ymax)๋ฅผ ๊ธฐ๋กํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ BFS๋ฅผ ํตํด ์ป์ ์์ ๋ฉ์ด๋ฆฌ์ ๋์ด๋ฅผ v๋ฒกํฐ์ ymin ~ ymax ์ธ๋ฑ์ค์ ๋์ ํด์ค๋๋ค.
๋ฌธ์ ์์ ์์๋ก ์ฃผ๋ ์ด๋ฏธ์ง์ธ๋ฐ, ์ ์ด๋ฏธ์ง๋ฅผ ์์๋ก ๋ ๋ค๋ฉด
ํฌ๊ธฐ 8์ง๋ฆฌ ์์ ๋ฉ์ด๋ฆฌ๋ 0-based ์ธ๋ฑ์ค์ v ๋ฒกํฐ๋ฅผ 0 ~ 2 ์ธ๋ฑ์ค์์ 8๋งํผ ์ฆ๊ฐ์ํต๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก 7 ํฌ๊ธฐ๋ 3 ~ 6 ์ธ๋ฑ์ค๋ฅผ 7๋งํผ,
2 ํฌ๊ธฐ๋ 6 ~ 7 ์ธ๋ฑ์ค๋ฅผ 2๋งํผ ์ฆ๊ฐ์ํต๋๋ค.
์ต์ข ์ ์ผ๋ก ๋ชจ๋ ์นธ์ ๋ํด BFS๋ฅผ ์งํํ๋ค๋ฉด, v ๋ฒกํฐ๋
[8,8,8,7,7,7,9,2] ๊ฐ ๋๊ณ , ์ต๋๊ฐ์ธ 9๊ฐ answer๊ฐ ๋ฉ๋๋ค.
์ ๋ฐฉ์์ ํ๋ฒ BFS๊ฐ ์งํ๋ ์์ ๋ฉ์ด๋ฆฌ์ ๋ํด ๋ค์ BFS๊ฐ ์งํ๋์ง ์๊ณ , ๋ชจ๋ ์นธ์ ํ๋ฒ์ฉ๋ง ์ํํ๊ธฐ ๋๋ฌธ์ O(N * M) ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ง๊ด์ ์ผ๋ก ๋ณด์ด๋ ํ์ด๊ฐ ๋๋ฌด ์ต์ํด ๋ณด์ฌ์ ์คํ๋ ค ์ํ์ฐฉ์ค๋ฅผ ๊ฒช์ ๊ฒ ๊ฐ๋ค์. ๊ตฌํ๋ ๊ฐ์ ๋ชฉ์ ..? ์ ๋ฐ๋ผ ๋ต์ ๊ธฐ๋กํ๋ ๋ฐฉ์์ ์ ์ฐํ๊ฒ ๋ฐ๊ฟ ๊ฐ๋ฉฐ ์๊ฐํ ์ ์๋ค๋ ๊ด์ ์ ๋ฐฐ์๊ฐ๋ ๊ฒ ๊ฐ์ต๋๋ค.