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

ABC092-D Grid Components


問題へのリンク


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

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

        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int A = wkArr[0];
        int B = wkArr[1];

        int UB = 99;
        char[,] BanArr = new char[UB + 1, UB + 1];

        for (int X = 0; X <= UB; X++) {
            for (int Y = 0; Y <= UB; Y++) {
                if (X <= 49) {
                    BanArr[X, Y] = '.';
                }
                else {
                    BanArr[X, Y] = '#';
                }
            }
        }

        // 右の黒マスに白を設定
        int RestACnt = A - 1;
        for (int X = 51; X <= UB; X += 2) {
            for (int Y = 0; Y <= UB; Y += 2) {
                if (RestACnt >= 1) {
                    BanArr[X, Y] = '.';
                }
                RestACnt--;
                if (RestACnt == 0) break;
            }
            if (RestACnt == 0) break;
        }

        // 左の白マスに黒を設定
        int RestBCnt = B - 1;
        for (int X = 0; X <= 49; X += 2) {
            for (int Y = 0; Y <= UB; Y += 2) {
                if (RestBCnt >= 1) {
                    BanArr[X, Y] = '#';
                }
                RestBCnt--;
                if (RestBCnt == 0) break;
            }
            if (RestBCnt == 0) break;
        }

        Console.WriteLine("{0} {1}", UB + 1, UB + 1);
        for (int Y = 0; Y <= UB; Y++) {
            for (int X = 0; X <= UB; X++) {
                Console.Write(BanArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

最初に下記のように、半分に分割します。
......#####
......#####
......#####
......#####
......#####
......#####

次に、下記のように、半分に分割した中で、
必要な島の分だけ、1*1の島を作成してます。
#.#.#.#.#.#
......#####
#.#.#.#.###
......#####
......#####
......#####