forked from encrypted-def/basic-algo-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14500_2.cpp
66 lines (57 loc) · 1.38 KB
/
14500_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Authored by : scsc3204
// Co-authored by : -
// http://boj.kr/d1845ccb4a6345f597884753edba728e
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool vis[502][502];
int b[502][502];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int mx, sum;
bool oob(int x, int y) {
return (x >= n || x < 0 || y >= m || y < 0);
}
void solve(int cx, int cy, int cnt) {
if(cnt == 4) {
mx = max(mx, sum);
return;
}
int nx, ny;
for(int dir = 0; dir < 4; dir++) {
nx = cx + dx[dir];
ny = cy + dy[dir];
if(oob(nx, ny)) continue;
if(vis[nx][ny]) continue;
sum += b[nx][ny];
vis[nx][ny] = 1;
solve(nx, ny, cnt + 1);
if(cnt == 2)
solve(cx, cy, cnt + 1);
sum -= b[nx][ny];
vis[nx][ny] = 0;
}
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> b[i][j];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
sum = b[i][j];
vis[i][j] = 1;
solve(i, j, 1);
vis[i][j] = 0;
}
cout << mx << '\n';
}
/*
ㅗㅜㅓㅏ를 제외한 모든 테트로미노는
길이가 4인 루트를 찾는 백트래킹으로 얻을 수 있다.
ㅗㅜㅓㅏ는 cnt == 2에서 3번째 정사각형을 뽑은 뒤
현위치인 (cx, cy)를 인자로 다시 보내 찾을 수 있다.
해당 코드는 34-35번째 줄과 같이 구현된다.
*/