LoginSignup
0
0

More than 1 year has passed since last update.

Paiza Aランクレベルアップメニュー 陣取りゲーム C#で書いてみた

Posted at

C#での回答がなかったため共有します。
あまりメモリ面?での知識がないので非効率なコードかもしれないです。
もっといいコードがあったらコメントお願いします。

EncampmentGame.cs
using System;
using System.Linq;
using System.Collections.Generic;

class EncampmentGame
{
    static void Main()
    {

        string[] line = Console.ReadLine().Split(' ');

        // 先行
        string precedence = Console.ReadLine();

        int rows = int.Parse(line[0]);
        int cols = int.Parse(line[1]);

        string[,] map = new string[rows, cols];

        Queue<int> YA = new Queue<int>();
        Queue<int> XA = new Queue<int>();

        Queue<int> YB = new Queue<int>();
        Queue<int> XB = new Queue<int>();

        int aCount = 1;
        int bCount = 1;

        bool a = false;

        if (precedence == "A")
            a = true;


        for (int i = 0; i < rows; i ++) {

            string oneRow = Console.ReadLine();

            for(int l = 0; l < cols; l++) {

                map[i, l] = oneRow[l].ToString();

                if (map[i, l] == "A") {
                    YA.Enqueue(i);
                    XA.Enqueue(l);
                }
                if (map[i, l] == "B") {
                    YB.Enqueue(i);
                    XB.Enqueue(l);
                }
            }
        }
        
        // 先行から始めてABどちらも置けなくなったら終了
        while (true) {

            int[] direX = {1, -1, 0, 0};
            int[] direY = {0, 0, 1, -1};
            
            if (a) {

                int roopCount = YA.Count;

                for (int r = 0; r < roopCount; r++) {

                    int row = YA.Dequeue();
                    int col = XA.Dequeue();

                    for (int i = 0; i < 4; i++) {

                        int newRow = row + direY[i];
                        int newCol = col + direX[i];

                        if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols)
                            continue;

                        if (map[newRow, newCol] == ".") {

                            map[newRow, newCol] = "A";
                            YA.Enqueue(newRow);
                            XA.Enqueue(newCol);
                            aCount++;
                        }
                    }
                }
                if (YB.Count != 0)
                    a = false;
            }

            if (!a) {

                int roopCount = YB.Count;
                
                for (int r = 0; r < roopCount; r++) {

                    int row = YB.Dequeue();
                    int col = XB.Dequeue();

                    for (int i = 0; i < 4; i++) {

                        int newRow = row + direY[i];
                        int newCol = col + direX[i];

                        if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols)
                            continue;

                        if (map[newRow, newCol] == ".") {

                            map[newRow, newCol] = "B";
                            YB.Enqueue(newRow);
                            XB.Enqueue(newCol);
                            bCount++;
                        }
                    }
                }
                if (YA.Count != 0)
                    a = true;
            }

            if (YA.Count == 0 && YB.Count == 0)
                break;
        }


        Console.WriteLine(aCount + " " + bCount);

        if (aCount > bCount)
            Console.WriteLine("A");
        else
            Console.WriteLine("B");
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0