Skip to content

Instantly share code, notes, and snippets.

@cjnghn
Last active September 10, 2023 04:46
Show Gist options
  • Save cjnghn/33931d27f8edefe6b70e6efa7395ea97 to your computer and use it in GitHub Desktop.
Save cjnghn/33931d27f8edefe6b70e6efa7395ea97 to your computer and use it in GitHub Desktop.
정수 사각형 최대 합.cpp
#include <iostream>
using namespace std;
int board[105][105], dp[105][105];
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> board[i][j];
// dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + board[i][j]
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int prev = 0;
if ((i - 1 >= 1 && i - 1 <= n) && (j >= 1 && j <= n))
prev = max(prev, dp[i - 1][j] ? dp[i - 1][j] : board[i - 1][j]);
if ((i >= 1 && i <= n) && (j - 1 >= 1 && j - 1 <= n))
prev = max(prev, dp[i][j - 1] ? dp[i][j - 1] : board[i][j - 1]);
dp[i][j] = prev + board[i][j];
}
}
int max_val = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (max_val < dp[i][j]) max_val = dp[i][j];
cout << max_val << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment