Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLASS 2: 체스판 다시 칠하기 - [1018] #3

Closed
Tracked by #2
fkdl0048 opened this issue Jul 27, 2023 · 0 comments · Fixed by #8
Closed
Tracked by #2

CLASS 2: 체스판 다시 칠하기 - [1018] #3

fkdl0048 opened this issue Jul 27, 2023 · 0 comments · Fixed by #8
Assignees
Labels

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Jul 27, 2023

CLASS 2: 체스판 다시 칠하기 - [1018]

C#으로 알고리즘을 풀려고 하니.. 입력 방법이나 스트링을 다루는 부분에서 많이 고민하게 된다.

억지로 처리하는게 아닌지.. 예외를 어디까지 잡아야 하는지 잘 감이 안잡히는 기분

일단 객체지향적으로 풀려고 노력했고, 입력이나 알고리즘도 오랜만이라 어색해서 참고를 했다.

다른 언어로도 풀기전 손풀이 용 문제를 풀었다고 생각

using static System.Console;

namespace Beakjoon1018
{
    public class Pair<T1, T2>
    {
        public T1 First { get; private set; }
        public T2 Second { get; private set; }
        public Pair(T1 first, T2 second)
        {
            First = first;
            Second = second;
        }
    }

    public class ChessBoard
    {
        public int File {get; private set;}
        public int Rank {get; private set;}

        private char[,] ChessBoardArray;

        public ChessBoard(int file, int rank)
        {
            File = file;
            Rank = rank;

            ChessBoardArray = new char[file, rank];
        }

        public void AddRank(int file, string rank)
        {
            if (rank.Length != Rank)
            {
                throw new System.Exception("Rank length is not match");
            }

            for (int i = 0; i < rank.Length; i++)
            {
                ChessBoardArray[file, i] = rank[i];
            }
        }

        public int CheckMinFixedRealBoard()
        {
            int result = int.MaxValue;

            for (int i = 0; i <= File - 8; i++)
            {
                for (int j = 0; j <= Rank - 8; j++)
                {
                    int num = 0;
                    for (int x = i; x < i + 8; x++)
                    {
                        for (int y = j; y < j + 8; y++)
                        {
                            var c = (x + y) % 2 == 0 ? 'W' : 'B';
                            if (c != ChessBoardArray[x, y])
                            {
                                num++;
                            }
                        }
                    }
                    num = System.Math.Min(num, 64 - num);
                    result = System.Math.Min(result, num);
                }
            }

            return result;
        }

        public void Print()
        {
            for (int i = 0; i < File; i++)
            {
                for (int j = 0; j < Rank; j++)
                {
                    Write(ChessBoardArray[i, j]);
                }
                WriteLine();
            }
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var input = Console.ReadLine()!.Split(' ');

            Pair<int, int> pair = new (int.Parse(input[0]), int.Parse(input[1]));

            ChessBoard chessBoard = new(pair.First, pair.Second);

            for (int i = 0; i < pair.First; i++)
            {
                chessBoard.AddRank(i, Console.ReadLine()!);
            }

            WriteLine(chessBoard.CheckMinFixedRealBoard());
        }
    }
}
@fkdl0048 fkdl0048 mentioned this issue Jul 27, 2023
6 tasks
@fkdl0048 fkdl0048 self-assigned this Jul 27, 2023
@fkdl0048 fkdl0048 added this to Todo Jul 27, 2023
@github-project-automation github-project-automation bot moved this to Todo in Todo Jul 27, 2023
@fkdl0048 fkdl0048 added this to the Solved.ac Class milestone Jul 27, 2023
@fkdl0048 fkdl0048 moved this from Todo to 🔍Algorithm in Todo Jul 27, 2023
@fkdl0048 fkdl0048 moved this from 🔍Algorithm to Two-Week Plan in Todo Aug 7, 2023
@fkdl0048 fkdl0048 moved this from Two-Week Plan to In Progress in Todo Aug 7, 2023
@fkdl0048 fkdl0048 linked a pull request Aug 7, 2023 that will close this issue
@github-project-automation github-project-automation bot moved this from In Progress to Done in Todo Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant