🌞Algorithm/🔥Baekjoon

[Baekjoon] 1706_크로스워드

뿌야._. 2024. 2. 20. 00:14

Silver II

문제(출처: https://www.acmicpc.net/problem/1706)

< 크로스워드 >

 

문제 풀이 

 

가로, 세로를 살펴보며 낱말을 만들어 정렬한다.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class _1706_ { // 크로스워드

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int r = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(st.nextToken());

		char arr[][] = new char[r][c];
		for (int i = 0; i < r; i++) {
			String str = bf.readLine();
			for (int j = 0; j < c; j++) {
				arr[i][j] = str.charAt(j);
			}
		}

		ArrayList<String> list = new ArrayList<>();
		for (int i = 0; i < r; i++) {
			String temp = "";
			for (int j = 0; j < c; j++) {
				if (arr[i][j] != '#') {
					temp += arr[i][j];
				} else {
					if (temp.length() > 1) {
						list.add(temp);
					}
					temp = "";
				}
			}
			if (temp.length() > 1) {
				list.add(temp);
			}
		}

		for (int i = 0; i < c; i++) {
			String temp = "";
			for (int j = 0; j < r; j++) {
				if (arr[j][i] != '#') {
					temp += arr[j][i];
				} else {
					if (temp.length() > 1) {
						list.add(temp);
					}
					temp = "";
				}
			}
			if (temp.length() > 1) {
				list.add(temp);
			}
		}

		Collections.sort(list);
		System.out.println(list.get(0));

	}
}
변수)
r, c : 퍼즐의 크기
arr : 퍼즐 정보
list : 만들 수 있는 낱말

 

퍼즐의 크기를 입력받는다. 퍼즐의 크기만큼 퍼즐의 정보를 입력받아 arr 배열에 저장한다. 먼저 가로로 연속되어 있는 낱말을 찾아 list에 저장한다. 그다음 세로로 연속되어 있는 낱말을 찾아 list에 저장한다. 그 후 list를 정렬한 후 사전식 순으로 가장 앞서 있는 낱말을 출력한다.



 

'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글

[Baekjoon] 2232_지뢰  (0) 2024.02.23
[Baekjoon] 3005_크로스워드 퍼즐 쳐다보기  (0) 2024.02.22
[Baekjoon] 11437_LCA  (0) 2024.02.19
[Baekjoon] 8911_거북이  (1) 2024.02.16
[Baekjoon] 1918_후위 표기식  (0) 2024.02.15