[BOJ][C#] 14500 테트로미노

LimJaeJun·2024년 1월 1일
0

PS/BOJ

목록 보기
81/108

📕 문제

📌 링크

📗 접근 방식

모든 모양 정의:

  • 가능한 모든 모양을 2차원 배열로 정의하고, shapeList에 저장한다.

보드 탐색:

  • 주아진 보드에서 가능한 모든 모양을 확인하면서, 각 모양에 대해 값이 1일때의 값을 더해본다.
    이때, 모양의 크기만큼 보드에서 해당 값을 더한다.

최댓값 갱신:

  • 각 모양에 대해 구한 세포 값의 합을 answer와 비교하여 최댓값을 갱신한다.

📘 코드

using System.Text;

namespace BOJ
{    
    class No_14500
    {
        static void Main()
        {
            List<int[,]> shapeList = new List<int[,]>()
            {
                new int[2, 2] { { 1, 1 }, { 1, 1 } },
                new int[1, 4] { { 1, 1 , 1, 1 } },
                new int[4, 1] { { 1 }, { 1 } ,{ 1 },{ 1 } },

                new int[2,3]{ { 1, 1, 1 }, { 1, 0, 0 } },
                new int[2,3]{ { 1, 0, 0 }, { 1, 1, 1 } },
                new int[2,3]{ { 0, 0, 1 }, { 1, 1, 1 } },
                new int[2,3]{ { 1, 1, 1 }, { 0, 0, 1 } },

                new int[3,2]{ { 1, 1 }, { 1, 0 }, { 1, 0 } },
                new int[3,2]{ { 1, 0 }, { 1, 0 }, { 1, 1 } },
                new int[3,2]{ { 0, 1 }, { 0, 1 }, { 1, 1 } },
                new int[3,2]{ { 1, 1 }, { 0, 1 }, { 0, 1 } },

                new int[2,3]{ { 1, 1, 0 }, { 0, 1, 1 } },
                new int[2,3]{ { 0, 1, 1 }, { 1, 1, 0 } },
                new int[3,2]{ { 1, 0 }, { 1, 1 }, { 0, 1 } },
                new int[3,2]{ { 0, 1 }, { 1, 1 }, { 1, 0 } },

                new int[2,3]{ { 1, 1, 1 }, { 0, 1, 0 } },
                new int[2,3]{ { 0, 1, 0 }, { 1, 1, 1 } },
                new int[3,2]{ { 0, 1 }, { 1, 1 }, { 0, 1 } },
                new int[3,2]{ { 1, 0 }, { 1, 1 }, { 1, 0 } }
            };

            int[] inputs = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int n = inputs[0];
            int m = inputs[1];

            int[,] board = new int[n, m];

            for (int i = 0; i < n; i++)
            {
                inputs = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
                for (int j = 0; j < m; j++)
                {
                    board[i, j] = inputs[j];
                }
            }

            int answer = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    foreach (int[,] t in shapeList)
                    {
                        if (i + t.GetLength(0) > n) continue;
                        if (j + t.GetLength(1) > m) continue;

                        int sum = 0;
                        for (int x = 0; x < t.GetLength(0); x++)
                        {
                            for (int y = 0; y < t.GetLength(1); y++)
                            {
                                if (t[x, y] == 0) continue;

                                sum += board[i + x, j + y];
                            }
                        }
                        
                        answer = Math.Max(answer, sum);
                    }
                }
            }

            Console.WriteLine(answer);
        }
    }
}

📙 오답노트

📒 알고리즘 분류

  • 구현
  • 브루트포스 알고리즘
profile
Dreams Come True

0개의 댓글

관련 채용 정보