-
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
17-kokeunho #64
base: 16-kokeunho
Are you sure you want to change the base?
17-kokeunho #64
Conversation
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < n; j++) { | ||
map[i][j] = sc.nextInt(); | ||
max_height = Math.max(max_height, map[i][j]); |
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.
์ด๋ ๊ฒ ํด์ h์ ํ์ ๋ฒ์๋ฅผ ์ค์ด์ จ๊ตฐ์! ์ข์ ์์ด๋์ด๋ค์
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 ๋๋ bfs ๋ฅผ ์ฌ์ฉํด์ ์์ญ์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ + ๋ธ๋ฃจํธ ํฌ์ค๋ก ๋ชจ๋ h์ ๊ฒฝ์ฐ ์กฐ์ฌ
์ด๋ ๊ฒ ๋๊ฐ์ง ๋ฌธ์ ๋ฅผ ์์ด๋์ผ๋๊น ๋ด๊ฐ ์ ํ๊ณ ์๋์ง ์ธ์ง๊ฐ ์ ์๋๋๋ผ๊ณ ์...
1์ฐจ ๊ตฌํ์ ๋๋ด๊ณ ์คํ์ ์์ผ๋ดค์ต๋๋ค.
๊ทผ๋ฐ... 5x5 ๋ฉด 25, 7x7 ์ด๋ฉด 49 ์ฒ๋ผ N^2 ์ด ์ถ๋ ฅ๋๋ ๋ฒ๊ทธ๊ฐ ์์์ต๋๋ค..
ํ 5๋ถ ๋์ด ๋น ์ ธ๋ผ ๊ฒ์ฌํด๋ณด๋ ์กฐ๊ฑด๋ฌธ์ ์กฐ๊ฑด์ ํ๋ ๋นผ๋จน์๋๋ผ๊ตฌ์..
if (!visited[y][x] && graph[y][x] >= level) { // ์ฌ๊ธฐ!
visited[y][x] = true;
bfs(graph, visited, x, y, level);
cnt++;
}
์ ๊ฒ์ ๊ณ ์น๋ ์ ๋ต์ด์์ต๋๋ค!
์ ๋ ์ฝ๋๊ฐ ๋๋ฌ์ด ๋๋์ด ์์ด์ ์ ๋ฆฌํ๋ฒ ์น ํด์คฌ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ๊ทผํธ๋ ์ฝ๋์ ์ ์ฌํฉ๋๋ค.
ํผ๋๋ฐฑ ์๊ตฌํ์
จ๋๋ฐ ์ ๋์๋ ์ ์ง์ฌ์ง ์ฝ๋ ๊ฐ์์!
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N;
int offset[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void bfs(vector<vector<int>>& graph, vector<vector<bool>>& visited, int cur_x, int cur_y, int level) {
queue<pair<int, int>> q;
q.push({cur_x, cur_y});
visited[cur_y][cur_x] = true;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (int dir = 0; dir < 4; dir++) {
int x_ = x + offset[dir][1];
int y_ = y + offset[dir][0];
if (0 <= x_ && x_ < N && 0 <= y_ && y_ < N && !visited[y_][x_] && graph[y_][x_] >= level) {
q.push({x_, y_});
visited[y_][x_] = true;
}
}
}
}
int getSafeArea(vector<vector<int>> graph, int level) {
vector<vector<bool>> visited(N, vector<bool>(N, false));
int cnt = 0;
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
if (!visited[y][x] && graph[y][x] >= level) {
visited[y][x] = true;
bfs(graph, visited, x, y, level);
cnt++;
}
}
}
return cnt;
}
int main() {
cin >> N;
vector<vector<int>> graph(N, vector<int>(N));
for (int y = 0; y < N; y++) {
for (int x = 0; x < N; x++) {
cin >> graph[y][x];
}
}
int maxArea = 0;
for (int level = 0; level <= 100; level++) {
maxArea = max(getSafeArea(graph, level), maxArea);
}
cout << maxArea;
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.
๊ทธ๋ํ ํ์์ ์ธ์ ๋ ํ๋ฒ์ ํ๋ฆฌ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค์.. ์ด๋ฒ์๋ ๋ชจ๋ ๋์๊ฐ ๋ฌผ์ ์ ๊ธฐ๋ ๊ฒฝ์ฐ
๋ฅผ ๊ณ ๋ คํ์ง ๋ชปํด ๋์ด๊ฐ ๊ฐ์ฅ ๋ฎ์ ์ง์ - 1 ๋ถํฐ ์์ํด์ผํ๋๋ฐ ๊ทธ๊ฑธ ๋ชป๋ด์ ์ข ์ค๋ ๊ฑธ๋ ธ๋ค์.
ํ์ง๋ง ์ด๋ฐ ์ ํ์ ๋ง์ด ํ๋ค๋ณด๋ visited๋ฐฐ์ด์ ์์น๋ queue๋ฅผ ์ ์ธํ๋ ์์น๋ฅผ ์ ์ ํ ๊ณณ์ ๋ฐ๋ก ๋ฐฐ์นํ์ฌ ์ฌ๋ฌ ํ ์คํธ๋ฅผ ๋น ๋ฅด๊ฒ ํต๊ณผํ ์ ์์๋ค์.
์ฝ๋๋ ๊ทผํธ๋๊ณผ ๊ฑฐ์ ๋น์ทํฉ๋๋ค. ๊ณ ์ํ์ จ์ต๋๋ค
code
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n, high = -1, low = 1000;
int city[105][105] = {0};
int offset[4][2] = {{1, 0},{0,1},{-1,0},{0,-1}};
bool visited[105][105] = {false};
bool canMove(int x, int y, int level) {
return 0<=x&&x<n&&0<=y&&y<n
&& !visited[y][x] && city[y][x] > level;
}
int bfs(int level, int x, int y) {
queue<pair<int, int>> q;
q.emplace(x, y);
visited[y][x] = true;
while(!q.empty()) {
auto [cx, cy] = q.front(); q.pop();
for(auto& dir : offset) {
int nx = cx + dir[0];
int ny = cy + dir[1];
if(!canMove(nx, ny, level)) continue;
q.emplace(nx, ny);
visited[ny][nx] = true;
}
}
return 1;
}
int main() {
cin>>n;
for(int y=0;y<n;y++) for(int x=0;x<n;x++) {
cin>>city[y][x];
if(high < city[y][x]) high = city[y][x];
if(low > city[y][x]) low = city[y][x];
}
int maxSafe = -1;
for(int level=low-1; level<=high; level++) {
memset(visited, false, sizeof(visited));
int safe = 0;
for(int y=0;y<n;y++) for(int x=0;x<n;x++)
if(city[y][x] > level && !visited[y][x]) safe += bfs(level, x, y);
if(maxSafe < safe) maxSafe = safe;
}
cout<<maxSafe;
return 0;
}
๐ ๋ฌธ์ ๋งํฌ
[BOJ] ์์ ์์ญ https://www.acmicpc.net/problem/2468
โ๏ธ ์์๋ ์๊ฐ
40min
โจ ์๋ ์ฝ๋
์ฐ์ 2์ฐจ์ ๋ฐฐ์ด์ ๋ ๊ฐ ์ฌ์ฉํฉ๋๋ค.
๊ฐ ์์์ ๋์ด๋ฅผ ๋ด๊ณ ์๋
map[][]
๊ณผ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ๋ด๊ณ ์๋visited[][]
๋ฅผ ์ฌ์ฉํ์์ต๋๋ค.map์ ํด๋น ์ง์ ์ ๋์ด๋ฅผ ๋์ ํ๋ฉด์ map์์ ๊ฐ์ฅ ๋์ ์ง์ ์ ๋์ด max_height๋ฅผ ๊ตฌํฉ๋๋ค.
์นจ์ ๋์ด๋ฅผ 0์์ max_height๊น์ง ๋๋ ค๊ฐ๋ฉด์
๋ฐฉ๋ฌธํ์ง ์์๊ณ ์นจ์๋์ง ์์(ํด๋น ์ง์ ๋์ด > max_height) ์ง์ ์์
dfs๋ฅผ ์คํํ๊ณ safe_area๋ฅผ +1 ํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๊ฐ ์นจ์ ๋์ด์์ ๊ธฐ์กด์ max_area(์ต๋ ์์ ์์ญ)๊ณผ safe_area๋ฅผ ๋น๊ตํ์ฌ
max_area๋ฅผ ๊ฐฑ์ ํฉ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ด์ ์ฐจ์์ ์ด์ด ๊ทธ๋ํ ํ์์ ๊ฐ์ ์ด์ด๊ฐ๊ธฐ ์ํ์ฌ
dfs ๋ฌธ์ ๋ฅผ ๊ณจ๋์ต๋๋ค.
์ฒ์์๋ ํ๋ฉด์ ๋๋ฌด ์ฝ๋๊ฐ ๋๋ฝ์ง ์๋ ์ถ์๋๋ฐ ํ๋ฆฌ๋๊ตฐ์..
ํน์ ๋ ๋์ ์ฝ๋๊ฐ ์๋ค๋ฉด ํ ์ ๋ถํ๋๋ฆฝ๋๋ค~!