๊ธฐ๋ก๋ฐฉ

BOJ_2714 : ๋ฌธ์ž๋ฅผ ๋ฐ›์€ ์Šนํ™˜์ด ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2714 : ๋ฌธ์ž๋ฅผ ๋ฐ›์€ ์Šนํ™˜์ด

Soom_1n 2022. 10. 10. 16:46

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2714๋ฒˆ: ๋ฌธ์ž๋ฅผ ๋ฐ›์€ ์Šนํ™˜์ด

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ T ≤ 1,000) ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , R, ๊ณต๋ฐฑ, C, ๊ณต๋ฐฑ, ์Šนํ™˜์ด๊ฐ€ ๋ฐ›์€ ๋ฉ”์‹œ์ง€๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. (1 ≤ R, C ≤ 21) ๋ฉ”์‹œ์ง€๋Š” 0๊ณผ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋ฐ›์€ ํ–‰๋ ฌ ํฌ๊ธฐ๋Œ€๋กœ ์ด์ง„์ˆ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋‚˜์—ดํ•œ๋‹ค.
  • ๋‹ฌํŒฝ์ด ํ˜•ํƒœ๋กœ ์ฝ์–ด๊ฐ€๋ฉฐ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
  • ์ˆซ์ž์•  ๋งค์น˜๋˜๋Š” ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());

        for (int i = 0; i < t; i++){
            String answer = "";
            StringTokenizer st = new StringTokenizer(br.readLine());
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            String msg = st.nextToken();

            String arr[] = new String[r];
            for (int j = 0; j < r; j++) {
                arr[j] = msg.substring(c * j, c * j + c);
            }

            int len = r*c/5;
            String matrix[] = new String[len];
            for (int j = 0; j < len; j++)
                matrix[j] = "";

            int x=0, y=0, flag = 1, count = 0, idx = 0, mc = c, mr = r;
            while (idx < len){
                for (int cc = 0; cc < mc; cc++){
                    matrix[idx] += arr[x].charAt(y);
                    y += flag;
                    if (++count == 5){
                        idx++;
                        count = 0;
                        if (idx >= len) break;
                    }
                }
                if (idx >= len) break;
                y += flag*-1;
                x += flag;
                mc--;
                for (int rr = 0; rr < mr-1; rr++){
                    matrix[idx] += arr[x].charAt(y);
                    x += flag;
                    if (++count == 5){
                        idx++;
                        count = 0;
                        if (idx >= len) break;
                    }
                }
                mr--;
                x += flag*-1;
                flag *= -1;
                y += flag;
            }
            for (int j = 0; j < len; j++) {
                int temp = 0;
                int num = 0;
                for (int k = 4; k >= 0; k--){
                    temp += (matrix[j].charAt(k) - '0') * Math.pow(2, num++);
                }
                if (temp == 0) answer += " ";
                else answer += (char) ('A' + temp - 1);
            }
            System.out.println(answer.stripTrailing());
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋ฐ›์€ ๋ฉ”์‹œ์ง€๋ฅผ ํ–‰๋ ฌ ํฌ๊ธฐ๋กœ ์„ ์–ธํ•œ String๋ฐฐ์—ด arr์— ๋‚˜๋ˆ„์–ด ์ €์žฅํ•œ๋‹ค.
  • String๋ฐฐ์—ด matrix๋Š” ๋‹ฌํŒฝ์ด ํ˜•ํƒœ๋กœ arr๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ 5๊ธ€์ž์”ฉ ์ž˜๋ผ ๋„ฃ๋Š”๋‹ค.
  • matrix์— ์ €์žฅ๋œ 2์ง„์ˆ˜ ๋ฌธ์ž์—ด์„ 10์ง„์ˆ˜๋กœ ๋ฐ”๊พธ์–ด ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์ถœ๋ ฅํ• ๋•Œ ๋’ค์— ๋ถ™๋Š” ๊ณต๋ฐฑ์€ ์ œ๊ฑฐํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ์— ์ž˜ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ž…๋ ฅ๋ฐ›์€ ๋ฉ”์‹œ์ง€๋ฅผ 5๊ฐœ๋กœ๋งŒ ๋Š์œผ๋ฉด ๋˜๋Š” ์ค„ ์•Œ์•˜๋‹ค๊ฐ€ ๋‹ฌํŒฝ์ด ํ˜•ํƒœ ์ˆœํšŒ๋ฅผ ๋’ค๋Šฆ๊ฒŒ ์ถ”๊ฐ€ํ–ˆ๋‹ค.
  • ํ•˜๋‚˜ํ•˜๋‚˜ ๊ตฌ๋ถ„ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•œ ๋ฒˆ ๊ผฌ์ด๋ฉด์„œ ์ถ”๊ฐ€ํ•˜๊ณ  ๋˜ ์ถ”๊ฐ€ํ•˜๋‹ˆ๊นŒ ๋งŽ์ด ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ง€๊ณ  ์ž˜ ํ’€๋ฆฌ์ง€ ์•Š์•˜๋‹ค.
    • ๋งˆ์ง€๋ง‰ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š์œผ๋ฉด ์ถœ๋ ฅํ˜•์‹ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_25205 : ๊ฒฝ๋กœ๋‹นํŽ‘ํฌ 2077  (0) 2022.10.19
BOJ_2217 : ๋กœํ”„  (0) 2022.10.11
BOJ_1065 : ํ•œ์ˆ˜  (0) 2022.10.10
BOJ_2635 : ์ˆ˜ ์ด์–ด๊ฐ€๊ธฐ  (0) 2022.10.08
BOJ_2628 : ์ข…์ด์ž๋ฅด๊ธฐ  (0) 2022.10.06