AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

AOJ 0518 最古の遺跡


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("10");
            WillReturn.Add("9 4");
            WillReturn.Add("4 3");
            WillReturn.Add("1 1");
            WillReturn.Add("4 2");
            WillReturn.Add("2 4");
            WillReturn.Add("5 8");
            WillReturn.Add("4 0");
            WillReturn.Add("5 3");
            WillReturn.Add("0 5");
            WillReturn.Add("5 2");
            WillReturn.Add("10");
            WillReturn.Add("9 4");
            WillReturn.Add("4 3");
            WillReturn.Add("1 1");
            WillReturn.Add("4 2");
            WillReturn.Add("2 4");
            WillReturn.Add("5 8");
            WillReturn.Add("4 0");
            WillReturn.Add("5 3");
            WillReturn.Add("0 5");
            WillReturn.Add("5 2");
            WillReturn.Add("0");
            //10
            //10
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct PointDef
    {
        internal long X;
        internal long Y;
    }
    static List<PointDef> mPointList = new List<PointDef>();

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int CurrInd = 0;
        while (true) {
            int N = int.Parse(InputList[CurrInd]);
            if (N == 0) break;

            mPointList.Clear();
            for (int I = CurrInd + 1; I <= CurrInd + 1 + N - 1; I++) {
                SplitAct(InputList[I]);
                PointDef WillAdd;
                WillAdd.X = wkArr[0];
                WillAdd.Y = wkArr[1];
                mPointList.Add(WillAdd);
            }
            Solve();

            CurrInd += 1 + N;
        }
    }

    static void Solve()
    {
        var PosHashSet = new HashSet<long>();
        foreach (PointDef EachPoint in mPointList) {
            long Hash = GetHash(EachPoint.X, EachPoint.Y);
            PosHashSet.Add(Hash);
        }

        long Answer = 0;

        int UB = mPointList.Count - 1;
        for (int I = 0; I <= UB; I++) {
            for (int J = I + 1; J <= UB; J++) {
                // ベクトルを求める
                PointDef Vect1;
                Vect1.X = mPointList[J].X - mPointList[I].X;
                Vect1.Y = mPointList[J].Y - mPointList[I].Y;
                PointDef Vect2 = Exec1JiHenkan(Vect1, 1, 0);
                PointDef Vect3 = Exec1JiHenkan(Vect2, 1, 0);

                PointDef Pos1;
                Pos1.X = mPointList[J].X;
                Pos1.Y = mPointList[J].Y;

                PointDef Pos2;
                Pos2.X = Pos1.X + Vect2.X;
                Pos2.Y = Pos1.Y + Vect2.Y;
                long Hash2 = GetHash(Pos2.X, Pos2.Y);
                if (PosHashSet.Contains(Hash2) == false) continue;

                PointDef Pos3;
                Pos3.X = Pos2.X + Vect3.X;
                Pos3.Y = Pos2.Y + Vect3.Y;
                long Hash3 = GetHash(Pos3.X, Pos3.Y);
                if (PosHashSet.Contains(Hash3) == false) continue;

                long Norm = Vect1.X * Vect1.X + Vect1.Y * Vect1.Y;
                Answer = Math.Max(Answer, Norm);
            }
        }
        Console.WriteLine(Answer);
    }

    // 座標のハッシュ値を返す
    static long GetHash(long pX, long pY)
    {
        return pX * 100000000 + pY;
    }

    // ベクトルとSinとCosを引数として、回転したベクトルを返す
    static PointDef Exec1JiHenkan(PointDef pPos, long pSin, long pCos)
    {
        PointDef WillReturn;
        WillReturn.X = pCos * pPos.X + pPos.Y * -pSin;
        WillReturn.Y = pSin * pPos.X + pPos.Y * pCos;
        return WillReturn;
    }
}


解説

正方形の1つのベクトルを全探索し、
90度回転したベクトルの移動先に座標があるかを調べてます。