#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//int solve(int x1,int y1,int x2,int y2,vvvvi&dp)
//{
// if (dp[x1][y1][x2][y2] != -1)return dp[x1][y1][x2][y2];
//
//}
int main()
{
int n,m;
cin>>n>>m;
vvi g(n + 1,vi(m + 1)),p(n + 1,vi(m + 1));
vvvvi dp(n + 1,vvvi(m + 1,vvi(n + 1,vi(m + 1,1e9))));
for (int i = 1;i<=n;i++){
for (int j = 1;j<=m;j++){
cin>>g[i][j];
p[i][j] = g[i][j] + p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1];
}
}
for (int x1 = n; x1 >= 1 ; x1--){
for (int y1 = m; y1 >= 1 ; y1--){
for (int x2 = x1; x2 <= n ;x2++){
for (int y2 = y1; y2 <= m ;y2++){
dp[x1][y1][x2][y2] = 1e9;
if (x1 == x2 && y1 == y2)dp[x1][y1][x2][y2] = 0;
int val = p[x2][y2] + p[x1 - 1][y1 - 1] - p[x2][y1 - 1] - p[x1 - 1][y2];
for (int x = x1; x < x2 ;x++)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2],val + dp[x1][y1][x][y2] + dp[x + 1][y1][x2][y2]);
for (int y = y1; y < y2 ;y++)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2],val + dp[x1][y1][x2][y] + dp[x1][y + 1][x2][y2]);
}
}
}
}
cout<<dp[1][1][n][m];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
4 ms |
1444 KB |
Output is correct |
9 |
Correct |
6 ms |
2252 KB |
Output is correct |
10 |
Correct |
8 ms |
2764 KB |
Output is correct |
11 |
Correct |
7 ms |
2380 KB |
Output is correct |
12 |
Correct |
22 ms |
6240 KB |
Output is correct |
13 |
Correct |
40 ms |
9028 KB |
Output is correct |
14 |
Correct |
11 ms |
3404 KB |
Output is correct |
15 |
Correct |
47 ms |
10160 KB |
Output is correct |
16 |
Correct |
6 ms |
1996 KB |
Output is correct |
17 |
Correct |
24 ms |
5232 KB |
Output is correct |
18 |
Correct |
127 ms |
19716 KB |
Output is correct |
19 |
Correct |
221 ms |
28204 KB |
Output is correct |
20 |
Correct |
254 ms |
33100 KB |
Output is correct |