Algorithm/BOJ

[BOJ] 1343번 폴리오미노 - C# 사용 - 미해결

Bueong_E 2023. 2. 4. 17:19
반응형
SMALL

문제

풀이

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace BOJ1343
{
    internal class Program
    {
        

        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            List<string> completeWords = new List<string>();
            List<string> finalWords = new List<string>();
            int wrong = 0;
            string input = sr.ReadLine();
            char[] chars = input.ToCharArray();
            string tem = null;
            string tem2 = null;

            foreach (char words in chars)
            {
                if (words == 'X')
                {
                    if (tem2 != null)
                    {
                        completeWords.Add(tem2);
                        tem2 = null;
                    }

                    tem += words.ToString();
                }
                if (words == '.')
                {
                    if (tem != null)
                    {
                        completeWords.Add(tem);
                        tem = null;
                    }

                    tem2 += words.ToString();
                }
            }
            completeWords.Add(tem);

            foreach (string i in completeWords)
            {
                if (i.Contains('X') && i.Length % 2 != 0)
                {
                    wrong = -1;
                    sw.WriteLine(wrong);
                    break;
                }
            }

            if (wrong != -1)
            {
                foreach (string x in completeWords)
                {
                    if (x.Length == 1 && Convert.ToChar(x) == '.')
                    {
                        finalWords.Add(x);
                    }
                    else if (x.Contains('.'))
                    {
                        finalWords.Add(x);
                    }
                    else if (x.Contains('X') && x.Length % 2 != 0)
                    {
                        wrong = -1;
                        sw.WriteLine(wrong);
                        break;
                    }
                    else
                    {
                        if (x.Length % 4 == 2)
                        {
                            string newX = x.Replace('X', 'A');
                            newX = newX.Substring(0, x.Length - 2);
                            finalWords.Add(newX + "BB");
                        }
                        else if (x.Length % 4 == 0)
                        {
                            string newX = x.Replace('X', 'A');
                            finalWords.Add(newX);

                        }
                        else if (x.Length == 2)
                        {
                            string newX = x.Replace('X', 'B');
                            finalWords.Add(newX);
                        }

                    }
                }
            }
            if (wrong != -1)
                foreach (string final in finalWords) { sw.Write(final); }

            sw.Flush();
            sw.Close();
            sr.Close();
        }
    }
}

결과창

 

스크린샷 처럼 예제들에 대한 답은 잘 출력되지만 계속해서 어떤 이유에서인지 런타임 에러가 뜬다 ㅠㅠ

어제 밤부터 계속 런타임 에러를 해결해보려고 시도 했지만 도저히 방법을 찾을수가 없었다....

 

검색도 해보았지만 위의 케이스는 없는듯하여 일단 풀은 걸로 생각하고 넘어가기로....

(엄청 찜찜하다)

반응형
LIST