#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>>chocolate, pref;
int table[51][51][51][51];
int rec(int x1, int y1, int x2, int y2){
int cnt = pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
int res = 1e9;
if(x1 == x2 && y1 == y2)return 0;
if(table[x1][y1][x2][y2] != -1)return table[x1][y1][x2][y2];
if(x1 != x2){
for(int i = x1;i < x2;++i){
res = min(res, rec(x1, y1, i, y2) + rec(i + 1, y1, x2, y2) + cnt);
}
}
if(y1 != y2){
for(int i = y1;i < y2;++i){
res = min(res, rec(x1, y1, x2, i) + rec(x1, i + 1, x2, y2) + cnt);
}
}
return table[x1][y1][x2][y2] = res;
}
int main()
{
cin >> n >> m;
chocolate.assign(n + 1, vector<int>(m + 1));
pref.assign(n + 1, vector<int>(m + 1));
for(int i = 1;i <= n;++i){
for(int j = 1;j <= m;++j){
cin >> chocolate[i][j];
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + chocolate[i][j];
}
}
for(int i = 0;i < 51;++i){
for(int j = 0;j < 51;++j){
for(int k = 0;k < 51;++k){
for(int l = 0;l < 51;++l){
table[i][j][k][l] = -1;
}
}
}
}
cout << rec(1, 1, n, m);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
26708 KB |
Output is correct |
2 |
Correct |
15 ms |
26676 KB |
Output is correct |
3 |
Correct |
15 ms |
26708 KB |
Output is correct |
4 |
Correct |
14 ms |
26676 KB |
Output is correct |
5 |
Correct |
14 ms |
26676 KB |
Output is correct |
6 |
Correct |
14 ms |
26708 KB |
Output is correct |
7 |
Correct |
15 ms |
26708 KB |
Output is correct |
8 |
Correct |
22 ms |
26768 KB |
Output is correct |
9 |
Correct |
26 ms |
26708 KB |
Output is correct |
10 |
Correct |
44 ms |
26836 KB |
Output is correct |
11 |
Correct |
34 ms |
26756 KB |
Output is correct |
12 |
Correct |
73 ms |
26732 KB |
Output is correct |
13 |
Correct |
125 ms |
26776 KB |
Output is correct |
14 |
Correct |
40 ms |
26828 KB |
Output is correct |
15 |
Correct |
135 ms |
26776 KB |
Output is correct |
16 |
Correct |
22 ms |
26708 KB |
Output is correct |
17 |
Correct |
56 ms |
26724 KB |
Output is correct |
18 |
Correct |
313 ms |
26784 KB |
Output is correct |
19 |
Correct |
528 ms |
26796 KB |
Output is correct |
20 |
Correct |
585 ms |
26800 KB |
Output is correct |