wanimaru47's diary

プログラミング等々

AOJ 1165

問題

この問題で、答えを出すにあたって必要な要素は縦横の長さです。
0枚目の正方形から4方向へ最大どのくらい図形が大きくなったかを保持して出力しす。

また問題では、N-1枚目の正方形がn枚目の正方形からd方向へ移動しているので
、n枚目の正方形が0枚目の座標からどんくらい離れているかが重要な要素になります。

以上をこれをコードにします。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair <int,int> P;

int main()
{
    int n;
    while (cin >> n, n) {
        vector<P> v;
        v.push_back(P(0, 0));
        int R = 0, L = 0, T = 0, B = 0;
        for (int i = 1; i < n; i++) {
            int t1, t2;
            cin >> t1 >> t2;
            int x = v[t1].first, y = v[t1].second;
            if (t2 == 0) y--;
            else if (t2 == 1) x--;
            else if (t2 == 2) y++;
            else if (t2 == 3) x++;
            v.push_back(P(x, y));
            if (x > T) T = x;
            else if (x < B) B = x;
            else if (y > R) R = y;
            else if (y < L) L = y;
        }
        cout << (R - L + 1) << " " << (T - B + 1) << endl;
    }

    return 0;
}