๐Ÿ’ญ Problem Solving

[SWEA][C++] 1209. [S/W ๋ฌธ์ œํ•ด๊ฒฐ ๊ธฐ๋ณธ] 2์ผ์ฐจ - Sum

0=2. 2024. 5. 6. 06:43

๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13_BWKACUCFAYh

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

ํ’€์ด

๊ฐ ํ–‰์˜ ํ•ฉ, ๊ฐ ์—ด์˜ ํ•ฉ, ๊ฐ ๋Œ€๊ฐ์„ ์˜ ํ•ฉ์„ ๊ฐ๊ฐ ๊ตฌํ•˜๊ณ  ๋น„๊ตํ•œ๋‹ค.

์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

const int N = 100;

int calculate(vector<vector<int>> &v) {
    int ans = 0, row_sum = 0, col_sum = 0, r_sum = 0, l_sum = 0;    // ํ–‰, ์—ด, ์šฐํ•˜ํ–ฅ, ์ขŒํ•˜ํ–ฅ ๋Œ€๊ฐ์„ 

    for (int i = 0; i < N; i++) {
        r_sum += v[i][i];   // ์šฐํ•˜ํ–ฅ ๋Œ€๊ฐ์„ 
        l_sum += v[i][N - 1 - i];   // ์ขŒํ•˜ํ–ฅ ๋Œ€๊ฐ์„ 
        for (int j = 0; j < N; j++) {
            row_sum += v[i][j]; // ํ–‰
            col_sum += v[j][i]; // ์—ด
        }
        ans = max(ans, max(row_sum, col_sum));
        row_sum = 0;
        col_sum = 0;
    }
    ans = max(ans, max(l_sum, r_sum));
    return ans;
}

int main() {
    int T = 10;

    for (int test_case = 1; test_case <= T; ++test_case) {
        int num;
        vector<vector<int>> v(N, vector<int>(N, 0));

        // ์ž…๋ ฅ
        cin >> num;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                cin >> v[i][j];
            }
        }

        // ์ถœ๋ ฅ
        cout << '#' << test_case << ' ' << calculate(v) << '\n';
    }

    return 0;
}