#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2005;
int n, m, a[N][N];
ll dp[N][N], suff[N][N], pref[N][N];
char mat[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int mx, sm;
bool vis[N][N];
bool valid(int x, int y){
return (x > 0 and y > 0 and x <= n and y <= m and mat[x][y] != '.');
}
void dfs(int x, int y){
vis[x][y] = 1;
mx = max(mx, y);
sm += mat[x][y] - '0';
for (int i = 0; i < 4; i ++){
int nx = x + dx[i];
int ny = y + dy[i];
if (!valid(nx, ny) or vis[nx][ny]) continue;
dfs(nx, ny);
}
}
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
cin >> mat[i][j];
for (int j = 1; j <= m; j ++){
for (int i = 1; i <= n; i ++){
if (vis[i][j] or mat[i][j] == '.') continue;
mx = j, sm = 0;
dfs(i, j);
a[j][mx] += sm;
}
}
for (int i = 1; i <= m; i ++)
for (int j = m; j >= i; j --)
suff[i][j] = suff[i][j + 1] + a[i][j];
for (int i = 1; i <= m; i ++){
for (int j = 1; j <= m; j ++){
dp[i][j] = dp[i - 1][j];
ll sm = 0;
for (int l = i - 1; l >= 0; l--){
sm += suff[l + 1][i];
dp[i][j] = max(dp[i][j], dp[l][j - 1] + sm);
}
}
}
for (int k = 1; k <= m; k ++)
cout << dp[m][k] << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |