#include <iostream>
using namespace std;
int n, m;
int** R;
int** P;
int gridsum(int i1, int j1, int i2, int j2)
{
return P[i2][j2] - P[i2][j1-1] - P[i1-1][j2] + P[i1-1][j1-1];
}
int**** r;
int res(int i1, int j1, int i2, int j2)
{
int q;
if(r[i1][j1][i2][j2] == -1)
{
if(i1 == i2 && j1 == j2) q = 0;
else
{
q = 2000000000;
for(int I = i1; I < i2; I++)
{
q = min(q, res(i1, j1, I, j2) + res(I+1, j1, i2, j2) + gridsum(i1, j1, i2, j2));
}
for(int J = j1; J < j2; J++)
{
q = min(q, res(i1, j1, i2, J) + res(i1, J+1, i2, j2) + gridsum(i1, j1, i2, j2));
}
}
r[i1][j1][i2][j2] = q;
}
return r[i1][j1][i2][j2];
}
int main()
{
cin >> n >> m;
R = new int*[n+1];
P = new int*[n+1];
r = new int***[n+1];
for(int i = 1; i <= n; i++)
{
r[i] = new int**[m+1];
for(int j = 1; j <= m; j++)
{
r[i][j] = new int*[n+1];
for(int k = 1; k <= n; k++)
{
r[i][j][k] = new int[m+1];
for(int l = 1; l <= m; l++) r[i][j][k][l] = -1;
}
}
}
for(int i = 0; i <= n; i++)
{
R[i] = new int[m+1];
P[i] = new int[m+1];
R[i][0] = P[i][0] = 0;
}
for(int j = 0; j <= m; j++) R[0][j] = P[0][j] = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
cin >> R[i][j];
P[i][j] = P[i-1][j] + P[i][j-1] - P[i-1][j-1] + R[i][j];
}
cout << res(1, 1, n, m) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
0 ms |
220 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
220 KB |
Output is correct |
5 |
Correct |
1 ms |
220 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
8 ms |
1200 KB |
Output is correct |
9 |
Correct |
15 ms |
1720 KB |
Output is correct |
10 |
Correct |
22 ms |
2140 KB |
Output is correct |
11 |
Correct |
22 ms |
1868 KB |
Output is correct |
12 |
Correct |
88 ms |
5060 KB |
Output is correct |
13 |
Correct |
119 ms |
7404 KB |
Output is correct |
14 |
Correct |
35 ms |
2780 KB |
Output is correct |
15 |
Correct |
167 ms |
8468 KB |
Output is correct |
16 |
Correct |
16 ms |
1500 KB |
Output is correct |
17 |
Correct |
59 ms |
4016 KB |
Output is correct |
18 |
Correct |
398 ms |
16680 KB |
Output is correct |
19 |
Correct |
720 ms |
24236 KB |
Output is correct |
20 |
Correct |
873 ms |
28756 KB |
Output is correct |