하루 한 접시

[백준] 1018 체스판 다시 칠하기 [C#]

NaZZU 2024. 3. 24. 21:51

첫줄에 Y축과 X축의 길이가 한줄로 주어진다.

이후 고쳐야 할 체스판이 주어진다.

고쳐야 할 수가 제일 적은 체스판의 고칠 칸의 숫자를 출력해주면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
namespace 연습장
{
    internal class Program
    {
        static char[,] board;
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            int[] lines = Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
            int x = lines[1];
            int y = lines[0];
 
            board = new char[y, x];
 
            for (int i = 0; i < y; i++)
            {
                string line = reader.ReadLine();
                for (int j = 0; j < x; j++)
                {
                    board[i, j] = line[j];
                }
            }
 
            int cnt = int.MaxValue;
 
            for (int a = 0; a+7 < y; a++)
            {
                for (int b = 0; b+7 < x; b++)
                {
                    cnt = Math.Min(cnt, checkBoard(a, b));
                }
            }
 
            print.WriteLine(cnt);
 
        }
 
        static int checkBoard(int x, int y)
        {
            int top_is_white = 0;
            int top_is_Black = 0;
 
            for(int i = 0; i < 8; i++)
            {
                for(int j = 0; j < 8; j++)
                {
                    if((i + j) % 2 == 0 )
                    {
                        if (board[x+i, y+j] == 'W')
                            top_is_Black++;
                        if (board[x+i, y+j] == 'B')
                            top_is_white++;
                    }
                    else
                    {
                        if (board[x+i, y+j] == 'W')
                            top_is_white++;
                        if (board[x+i, y+j] == 'B')
                            top_is_Black++;
                    }
                }
            }
 
            return Math.Min(top_is_Black, top_is_white);
 
        }
 
    }
}
cs

 

가독성을 위해 checkBoard 함수를 따로 빼두었기 때문에, 체스판을 담을 배열을 static으로 전역화 해주었다.

이후 좌표의 크기가 Y축, X축으로 주어졌는데, 이는 이차원 배열이 [y축, x축] 으로 되어있기 때문이다.

사람의 관점에서 당연히 x, y로 읽다 보니 자주 에러가 났었다.

부르트 포스 문제인 만큼 알고리즘 따위 생각하지 않고 무식하게 모든 경우의 수를 체크해주면 해결 할 수 있다.

알고리즘을 짜는것 보다 좌표가 너무 헷갈려서 힘들었다.