Skip to content

Commit

Permalink
Merge pull request #55 from HYU-PS-STUDY/wonkyDD
Browse files Browse the repository at this point in the history
[week-09] 14500, 1992
  • Loading branch information
clean2001 authored Mar 22, 2024
2 parents 977363a + 9c92229 commit 9d8c87f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
Empty file removed week09/wonkyDD/.gitkeep
Empty file.
74 changes: 74 additions & 0 deletions week09/wonkyDD/14500.cpp
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);
}

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);
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;
}

0 comments on commit 9d8c87f

Please sign in to comment.