-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from HYU-PS-STUDY/wonkyDD
[week-09] 14500, 1992
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |