【題解】ZeroJudge c143: 105北二1爺爺種樹

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c143

#include <iostream>
using namespace std;

int n, m, t, g[505][505], a, b, c, d, x, y, ans;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    cin >> t;
    while (t--){
        cin >> a >> b >> c >> d;
        if (c > a) x = 1;
        else if (c == a) x = 0;
        else x = -1;
        if (d > b) y = 1;
        else if (d == b) y = 0;
        else y = -1;
        for (int i = max(abs(c-a), abs(d-b)); i >= 0; i--){
            if (g[a][b] == 0){
                g[a][b] = 1;
                ans++;
            }
            b += y;
            a += x;
        }
    }
    cout << ans << "\n";
}
分享本文 Share with friends