File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments