1. 문제

1706번: 크로스워드



2. 개요

R x C 크기의 완성된 크로스워드 퍼즐이 있다.

퍼즐 내에서 가로나 세로로 연속되어 있고 더이상 확장할 수 없는 낱말이 단어가 된다.

이 때 퍼즐 내에 존재하는 단어들 중 사전순으로 가장 앞서는 단어를 출력하는 문제.



3. 풀이 및 코드

3-1. 풀이

각각의 행과 열의 모든 문자에 대해서 막힌 칸을 기준으로 단어들로 나누어 단어 리스트에 저장한다.

이때 길이가 2미만인 경우는 단어로 취급할 수 없으므로 제외한다.

모든 행과 열에 대해 작업을 마친 뒤, 단어 리스트를 정렬하여 첫번째 값을 출력한다.

3-2. Python

import sys

R, C = map(int, sys.stdin.readline().split())

board = []

for i in range(R):
    board.append(sys.stdin.readline().rstrip())

words = []

for i in range(R):
    for w in list(board[i].split('#')):
        if len(w) < 2:
            continue
        words.append(w)

for i in range(C):
    tmp = ""
    for j in range(R):
        if board[j][i] == '#':
            if len(tmp) < 2:
                tmp = ""
                continue
            words.append(tmp)
            tmp = ""
            continue

        elif j == R-1:
            tmp += board[j][i]
            if len(tmp) < 2:
                tmp = ""
                continue
            words.append(tmp)
            tmp = ""
            continue

        tmp += board[j][i]

print(sorted(words)[0])

3-3. C#

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

            string[] input = sr.ReadLine().Split();
            int R = int.Parse(input[0]), C = int.Parse(input[1]);

            string[] board = new string[R];

            for (int i = 0; i < R; i++)
                board[i] = sr.ReadLine().TrimEnd();

            List<string> words = new();

            for (int i = 0; i < R; i++)
            {
                foreach (string w in board[i].Split("#"))
                {
                    if (w.Length < 2)
                        continue;

                    words.Add(w);
                }
            }

            for (int i = 0; i < C; i++)
            {
                string tmp = "";

                for (int j = 0; j < R; j++)
                {
                    if (board[j][i] == '#')
                    {
                        if (tmp.Length < 2)
                        {
                            tmp = "";
                            continue;
                        }
                        words.Add(tmp);
                        tmp = "";
                        continue;
                    }

                    else if (j == R - 1)
                    {
                        tmp += board[j][i];

                        if (tmp.Length < 2)
                        {
                            tmp = "";
                            continue;
                        }
                        words.Add(tmp);
                        tmp = "";
                        continue;
                    }

                    tmp += board[j][i];
                }
            }

            words.Sort();
            sw.WriteLine(words[0]);
            sw.Close();
        }
    }
}

Comments