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

[week-09] 14500, 1992 #55

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Empty file removed week09/wonkyDD/.gitkeep
Empty file.
74 changes: 74 additions & 0 deletions week09/wonkyDD/14500.cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 모양 하나하나에 대해 다 처리해주었는데.. 이렇게 푸는 문제였군요😭 풀이 잘봤습니다!!

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
using namespace std;
#define MAX 500
int N,M;
int map[MAX][MAX];
bool visited[MAX][MAX];
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};
int ans = 0;

bool check(int y, int x) {
return y>=0 && y<N && x>=0 && x<M;
}

void dfs(int y, int x, int cnt, int cur) {
if (cnt == 4) {
ans = max(ans, cur);
return;
}

for (int i=0; i<4; ++i) {
int ny = y+dy[i], nx = x+dx[i];
if (!check(ny, nx) || visited[ny][nx]) continue;

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

void ooh(int y, int x) {
int sum = map[y][x];
int cnt = 0;
int tmp = 1001;
for (int i=0; i<4; ++i) {
int ny = y+dy[i], nx = x+dx[i];
if (!check(ny, nx)) continue;
++cnt;
sum += map[ny][nx];
tmp = min(tmp, map[ny][nx]);
}

if (cnt < 3) return;
if (cnt == 3) {
ans = max(ans, sum);
return;
}

// cnt == 4
ans = max(ans, sum - tmp);
}
Copy link
Member

Choose a reason for hiding this comment

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

ㅜ 모양을 어떻게 처리할지 고민이었는데 cnt로 처리하는 방법 잘 봤어요!👍🏻


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

cin >> N >> M;
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) {
visited[i][j] = true;
dfs(i, j, 1, map[i][j]);
visited[i][j] = false;
//
ooh(i, j);
}
}
cout << ans;
return 0;
}
55 changes: 55 additions & 0 deletions week09/wonkyDD/1992.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <string>
using namespace std;
#define MAX 64
int N;
char map[MAX][MAX];

void dfs(int y, int x, int len) {
// NOTE : 길이가 1이면 압축 불가능
if (len == 1) {
cout << map[y][x];
return;
}

bool flag = true;
char c = map[y][x];
// NOTE : 모든 dfs 깊이마다 압축 가능한지 여부를 판단한다
for (int i=y; i<y+len; ++i) {
for (int j=x; j<x+len; ++j) {
if (c != map[i][j]) {
flag = false;
break;
}
}
if (!flag) break;
}

// NOTE : 압축 가능했다면...
if (flag) {
cout << c;
return;
}

// NOTE : 왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래 순서
cout << "(";
dfs(y, x, len/2);
dfs(y, x+len/2, len/2);
dfs(y+len/2, x, len/2);
dfs(y+len/2, x+len/2, len/2);
Copy link
Contributor

Choose a reason for hiding this comment

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

            sb.append("(");
            solve(sy, sx, sy+n/2-1, sx+n/2-1);
            solve(sy, sx+n/2, sy+n/2-1, ex);
            solve(sy+n/2, sx, ey, sx+n/2-1);
            solve(sy+n/2, sx+n/2, ey, ex);
            sb.append(")");

y, x의 시작 지점과 탐색범위(len)을 넘기는 함수로 작성하는게 훨씬 간단해보이네요.
잘봤습니다! :)

cout << ")";
}

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

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

dfs(0, 0, N);
return 0;
}