AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC275-C Counting Squares


問題へのリンク


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("##.......");
            WillReturn.Add("##.......");
            WillReturn.Add(".........");
            WillReturn.Add(".......#.");
            WillReturn.Add(".....#...");
            WillReturn.Add("........#");
            WillReturn.Add("......#..");
            WillReturn.Add(".........");
            WillReturn.Add(".........");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add(".#.......");
            WillReturn.Add("#.#......");
            WillReturn.Add(".#.......");
            WillReturn.Add(".........");
            WillReturn.Add("....#.#.#");
            WillReturn.Add(".........");
            WillReturn.Add("....#.#.#");
            WillReturn.Add("........#");
            WillReturn.Add(".........");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct PointDef
    {
        internal int X;
        internal int Y;
    }

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

        char[,] BanArr = CreateBanArr(InputList);
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        var PointList = new List<PointDef>();
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] == '#') {
                    PointDef WillAdd;
                    WillAdd.X = LoopX;
                    WillAdd.Y = LoopY;
                    PointList.Add(WillAdd);
                }
            }
        }

        int Answer = 0;
        for (int I = 0; I <= PointList.Count - 1; I++) {
            for (int J = I + 1; J <= PointList.Count - 1; J++) {
                for (int K = 0; K <= PointList.Count - 1; K++) {
                    if (K == I) continue;
                    if (K == J) continue;

                    PointDef PointA = PointList[I];
                    PointDef PointB = PointList[J];
                    PointDef PointC = PointList[K];

                    PointDef VectAB = SetVector(PointA, PointB);
                    PointDef VectBA = SetVector(PointB, PointA);
                    PointDef VectBC = SetVector(PointB, PointC);
                    PointDef VectCB = SetVector(PointC, PointB);

                    if (DeriveDot(VectBA, VectBC) != 0) continue;

                    int NormAB = DeriveNorm(VectAB);
                    int NormBC = DeriveNorm(VectBC);
                    if (NormAB != NormBC) continue;

                    for (int L = 0; L <= PointList.Count - 1; L++) {
                        if (L == I) continue;
                        if (L == J) continue;
                        if (L == K) continue;

                        PointDef PointD = PointList[L];

                        PointDef VectAD = SetVector(PointA, PointD);
                        PointDef VectCD = SetVector(PointC, PointD);

                        PointDef VectDC = SetVector(PointD, PointC);
                        PointDef VectDA = SetVector(PointD, PointA);

                        if (DeriveDot(VectAB, VectAD) != 0) continue;
                        if (DeriveDot(VectCB, VectCD) != 0) continue;
                        if (DeriveDot(VectDC, VectDA) != 0) continue;

                        int NormCD = DeriveNorm(VectCD);
                        int NormDA = DeriveNorm(VectDA);

                        if (NormAB != NormCD) continue;
                        if (NormAB != NormDA) continue;

                        Answer++;
                    }
                }
            }
        }
        // 正方形ABCD、正方形BCDA、正方形CDAB、正方形DABCで4回カウントしているので
        // 4で割る
        Console.WriteLine(Answer / 4);
    }

    // 内積を求める
    static int DeriveDot(PointDef pVector1, PointDef pVector2)
    {
        return pVector1.X * pVector2.X + pVector1.Y * pVector2.Y;
    }

    // ベクトルのNormを求める
    static int DeriveNorm(PointDef pVector)
    {
        return pVector.X * pVector.X + pVector.Y * pVector.Y;
    }

    // 始点と終点の座標を引数として、始点から終点へのベクトルを返す
    static PointDef SetVector(PointDef pStaPoint, PointDef pEndPoint)
    {
        PointDef WillReturn;
        WillReturn.X = pEndPoint.X - pStaPoint.X;
        WillReturn.Y = pEndPoint.Y - pStaPoint.Y;
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = StrList[0].Length - 1;
        int UB_Y = StrList.Count - 1;

        char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = StrList[Y][X];
            }
        }
        return WillReturn;
    }
}


解説

4辺が正方形をなす必要十分条件は、
4辺の長さが等しい、かつ、なす角が全て90度なので、
ベクトルの長さが等しい、かつ、内積が0で判定できます。

81*80*79*78だとTLEするので、
3点が決まった時点でNormと内積をチェックしてます。