Skip to content
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

[이분탐색] 11월 8일 #12

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions 10_이분탐색/14500.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//10_이분탐색 필수2: 14500

#include <iostream>
#include <vector>

using namespace std;

int n, m, ans;
vector<vector<int>> map;
vector<vector<bool>> visit;
vector<int> dy = {-1, 1, 0, 0};
vector<int> dx = {0, 0, -1, 1};
Comment on lines +9 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

계속해서 쓰이는 벡터들을 전역으로 선언해 주셨네요!👍


void dfs(int y, int x, int cnt, int sum, vector<vector<bool>>& visit)
{
if (cnt >= 4)
{
ans = max(ans, sum);
return;
}

for (int k = 0; k < 4; k++)
{
int ny = y + dy[k];
int nx = x + dx[k];

if (ny < 0 || nx < 0 || ny >= n || nx >= m || visit[ny][nx]) {
continue;
}

visit[ny][nx] = true;
dfs(ny, nx, cnt + 1, sum + map[ny][nx], visit);
visit[ny][nx] = false;
}
}

void check(int y, int x)
{
if (y < n - 2 && x < m - 1){
ans = max(ans, map[y][x] + map[y + 1][x] + map[y + 2][x] + map[y + 1][x + 1]);
}

if (y < n - 2 && x > 0){
ans = max(ans, map[y][x] + map[y + 1][x] + map[y + 2][x] + map[y + 1][x - 1]);
}

if (y < n - 1 && x < m - 2){
ans = max(ans, map[y][x] + map[y][x + 1] + map[y][x + 2] + map[y + 1][x + 1]);
}

if (y > 0 && x < m - 2){
ans = max(ans, map[y][x] + map[y][x + 1] + map[y][x + 2] + map[y - 1][x + 1]);
}
}
Comment on lines +37 to +54
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅜ 모양의 테트로미노에 대해 섬세하게 경우를 나눠 구현해 주셨네요. 코드도 간결해 보기 좋았습니다.👍 다만 해설강의에서 말씀드렸드시 dfs함수 내에서 cnt==2인 경우마다 현재 위치에서 다시 dfs를 진행하여 처리할 수도 있다는 점을 알아두시면 좋을 것 같습니다.


int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);

cin >> n >> m;
map.assign(n, vector<int>(m));
visit.assign(n, vector<bool>(m, false));

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
visit[i][j] = true;
dfs(i, j, 1, map[i][j], visit);

visit[i][j] = false;
check(i, j);
Comment on lines +76 to +80
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visit벡터를 사용하지 않아도 map벡터에 해당하는 칸의 수를 0으로 잠시 바꾸어두는 방식으로도 문제를 해결할 수 있습니다. 샘플코드 참고하셔서 한 번 알아두시는 것도 좋을 것 같습니다.😊

}
}

cout << ans << "\n";

return 0;
}