Skip to content

Commit 47bdee5

Browse files
authored
Merge pull request #82 from sh0723/solve-boj14620
[BOJ 14620] 꽃길
2 parents 0038631 + ac9f30c commit 47bdee5

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

‎src/week3/BOJ14620/BOJ14620_V1.cpp‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int N;
4+
int isplanted[10][10];
5+
int inp_map[10][10];
6+
int dy[4] = {0, 1, 0, -1};
7+
int dx[4] = {1, 0, -1, 0};
8+
int ret = INT_MAX;
9+
bool canPlant(int y, int x) {
10+
for (int k = 0; k < 4; k++) {
11+
int vy = y + dy[k];
12+
int vx = x + dx[k];
13+
if (0 > vy || vy >= N || 0 > vx || vx >= N || isplanted[vy][vx]) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
int plant(int y, int x) {
20+
int cost = inp_map[y][x];
21+
for (int k = 0; k < 4; k++) {
22+
int vy = y + dy[k];
23+
int vx = x + dx[k];
24+
isplanted[vy][vx] = 1;
25+
cost += inp_map[vy][vx];
26+
}
27+
28+
return cost;
29+
}
30+
void unplant(int y, int x) {
31+
for (int k = 0; k < 4; k++) {
32+
int vy = y + dy[k];
33+
int vx = x + dx[k];
34+
isplanted[vy][vx] = 0;
35+
}
36+
}
37+
void solve(int cost, int cnt) {
38+
if (cnt == 3) {
39+
ret = min(ret, cost);
40+
}
41+
42+
for (int i=0; i<N; i++) {
43+
for (int j=0; j<N; j++) {
44+
if (canPlant(i, j)) {
45+
int next_cost = cost + plant(i, j);
46+
solve(next_cost, cnt + 1);
47+
unplant(i, j);
48+
}
49+
}
50+
}
51+
}
52+
int main() {
53+
54+
ios_base::sync_with_stdio(false);
55+
cin.tie(NULL);
56+
cout.tie(NULL);
57+
58+
cin >> N;
59+
for (int i = 0; i < N; i++) {
60+
for (int j = 0; j < N; j++) {
61+
cin >> inp_map[i][j];
62+
}
63+
}
64+
65+
solve(0,0);
66+
67+
cout << ret << '\n';
68+
69+
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)