백준 1343 - 폴리오미노 [Rust]

[Silver V] 폴리오미노 - 1343

Posted by Hebi on March 31, 2023

백준 알고리즘

[Silver V] 폴리오미노 - 1343

문제 링크

성능 요약

메모리: 13148 KB, 시간: 4 ms

분류

그리디 알고리즘(greedy), 구현(implementation)

문제 설명

민식이는 다음과 같은 폴리오미노 2개를 무한개만큼 가지고 있다. AAAA와 BB

이제 '.'와 'X'로 이루어진 보드판이 주어졌을 때, 민식이는 겹침없이 'X'를 모두 폴리오미노로 덮으려고 한다. 이때, '.'는 폴리오미노로 덮으면 안 된다.

폴리오미노로 모두 덮은 보드판을 출력하는 프로그램을 작성하시오.

입력

첫째 줄에 보드판이 주어진다. 보드판의 크기는 최대 50이다.

출력

첫째 줄에 사전순으로 가장 앞서는 답을 출력한다. 만약 덮을 수 없으면 -1을 출력한다.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::io;

fn main() {
    let mut input_a = String::new();
    io::stdin().read_line(&mut input_a).unwrap();
    let mut cnt = 0;
    // let mut result: Vec<&str> = Vec::new();
    let mut result = "".to_string();
    for i in 0..input_a.len() {
        if &input_a[i..i + 1] == "X" {
            cnt = cnt + 1;
        }
        if &input_a[i..i + 1] == "." {
            // result.push(".");
            result = result + ".";
            if (cnt % 2) != 0 {
                break;
            } else {
                cnt = 0;
            }
        }
        if (cnt == 2) && (&input_a[i + 1..i + 2]) != "X" {
            // result.push("BB");
            result = result + "BB";
            cnt = 0;
        } else if cnt == 4 {
            result = result + "AAAA";
            // result.push("AAAA");
            cnt = 0;
        }
    }
    if (cnt % 2) == 1 {
        println!("{}", -1);
    } else {
        println!("{}", result.as_str());
    }
}