Submission

Status:

-PP-------

Subtask/Task Score:

20/100

Score: 20

User: nate213121

Problemset: บริษัททำความสะอาด

Language: cpp

Time: 0.002 second

Submitted On: 2026-09-12 20:22:56

#include <bits/stdc++.h>

using namespace std;

int n;

bool inBounds(int i, int j)
{
    if(!(i >= 0 && i < n && j >= 0 && j < n))
    {
        return false;
    }
    return true;
}

int main()
{
    cin >> n;
    string input;
    cin >> input;

    for(auto& c:input)
    {
        if(!isdigit(c))
        {
            c = ' ';
        }
    }

    vector<vector<int>> buildings;

    stringstream ss(input);

    for(int i = 0; i < n; i++)
    {
        vector<int> currRow;
        for(int j = 0; j < n; j++)
        {
            int tmp;
            ss >> tmp;
            currRow.push_back(tmp);
        }
        buildings.push_back(currRow);
    }

    int ans = 0;
    vector<pair<int, int>> checkDirections = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            int currHeight = buildings[i][j];
            ans += 2 * (currHeight + 1 + currHeight);
            for(auto& d:checkDirections)
            {
                int newI = i + d.first;
                int newJ = j + d.second;
                if(inBounds(newI, newJ))
                {
                    if(buildings[newI][newJ] <= buildings[i][j])
                    {
                        ans -= buildings[newI][newJ];
                    }
                    else
                    {
                        ans -= buildings[i][j];
                    }
                }
            }
        }
    }

    cout << ans;
    
    return 0;
}