#include <bits/stdc++.h>
using namespace std;
const int N = 55, INF = 1000000009;
int n, m;
int a[N][N];
int p[N][N];
void pre()
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j];
}
}
}
int sum(int x1, int y1, int x2, int y2)
{
return p[x2][y2] - p[x1 - 1][y2] - p[x2][y1 - 1] + p[x1 - 1][y1 - 1];
}
bool c[N][N][N][N];
int dp[N][N][N][N];
int rec(int x1, int y1, int x2, int y2)
{
if (c[x1][y1][x2][y2])
return dp[x1][y1][x2][y2];
c[x1][y1][x2][y2] = true;
if (x1 == x2 && y1 == y2)
return 0;
dp[x1][y1][x2][y2] = INF;
int s = sum(x1, y1, x2, y2);
for (int i = x1; i < x2; ++i)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2], s + rec(x1, y1, i, y2) + rec(i + 1, y1, x2, y2));
for (int j = y1; j < y2; ++j)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2], s + rec(x1, y1, x2, j) + rec(x1, j + 1, x2, y2));
return dp[x1][y1][x2][y2];
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
#endif // SOMETHING
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
scanf("%d", &a[i][j]);
}
pre();
printf("%d\n", rec(1, 1, n, m));
return 0;
}
Compilation message
raisins.cpp: In function 'int main()':
raisins.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
raisins.cpp:52:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
2 ms |
1408 KB |
Output is correct |
8 |
Correct |
10 ms |
4096 KB |
Output is correct |
9 |
Correct |
17 ms |
5376 KB |
Output is correct |
10 |
Correct |
24 ms |
6400 KB |
Output is correct |
11 |
Correct |
21 ms |
5632 KB |
Output is correct |
12 |
Correct |
71 ms |
10744 KB |
Output is correct |
13 |
Correct |
118 ms |
13560 KB |
Output is correct |
14 |
Correct |
33 ms |
6904 KB |
Output is correct |
15 |
Correct |
145 ms |
15008 KB |
Output is correct |
16 |
Correct |
14 ms |
5248 KB |
Output is correct |
17 |
Correct |
62 ms |
10488 KB |
Output is correct |
18 |
Correct |
386 ms |
23168 KB |
Output is correct |
19 |
Correct |
619 ms |
28280 KB |
Output is correct |
20 |
Correct |
712 ms |
30484 KB |
Output is correct |