본문으로 바로가기

[SIM] 2290번 LCD Test

category Algorithm/BOJ 문제풀이 2019. 1. 17. 21:40
2290_LCD Test

2290번 LCD Test

 

https://www.acmicpc.net/problem/2290


 

문제

지민이는 새로운 컴퓨터를 샀다. 하지만 새로운 컴퓨터에 사은품으로 온 LC-디스플레이 모니터가 잘 안나오는 것이다. 지민이의 친한 친구인 지환이는 지민이의 새로운 모니터를 위해 테스트 할 수 있는 프로그램을 만들기로 하였다.

 

입력

첫째 줄에 종이의 세로 크기 N과 가로 크기 M이 주어진다. (4 ≤ N, M ≤ 500)

둘째 줄부터 N개의 줄에 종이에 쓰여 있는 수가 주어진다. i번째 줄의 j번째 수는 위에서부터 i번째 칸, 왼쪽에서부터 j번째 칸에 쓰여 있는 수이다. 입력으로 주어지는 수는 1,000을 넘지 않는 자연수이다.

 

출력

길이가 s인 '-'와 '|'를 이용해서 출력해야 한다. 각 숫자는 모두 s+2의 가로와 2s+3의 세로로 이루어 진다. 나머지는 공백으로 채워야 한다. 각 숫자의 사이에는 공백이 한 칸 있어야 한다.

 

예제 입력

2 1234567890

 

예제 출력

      --   --        --   --   --   --   --   --  
   |    |    | |  | |    |       | |  | |  | |  | 
   |    |    | |  | |    |       | |  | |  | |  | 
      --   --   --   --   --        --   --       
   | |       |    |    | |  |    | |  |    | |  | 
   | |       |    |    | |  |    | |  |    | |  | 
      --   --        --   --        --   --   --  

 

해결방법

주어진 문제를 분석하여 구현하는 시뮬레이션 문제이다

 

 

⭐️ 솔루션 1 ⭐️

약간 하드코딩(?) 으로 문제를 구현하였다

 

아래의 그림은 s=2 일 때, 5와 8의 경우를 나타낸 것이다



  • 첫행, 중간행, 마지막행은 - 가 와야한다
  • 나머지행은 | 가 오는 행인데 처음 열과 마지막 열에만 배치할 수 있다

 

이 조건을 이용해서 조건문을 잘 사용하면 결과를 출력할 수 있다!!

 

 

⭐️ 솔루션 2 ⭐️

다른 블로그를 참고하여 코드를 간단하게 다시 짜봤다

참고블로그 : https://luvery93.github.io/articles/2018-11/boj-2290

 

진짜 다른 블로그의 소스를 보고 말도안된다고 생각했다....

어떻게 이런 생각을 할 수 있을까....?? 난 아직 멀었구나....

그냥 따라하는 식으로 코드를 카피했다...

 

 

 

소스코드

[ 1차 소스코드 ]

문제 해결 시간 : 1h 50m

메모리 : 1988 KB

시간 : 0 ms

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

int s,row,col,len,mid;
string n;

bool isPossibleCol(int ind,int num){
    switch (num) {
        case 0:{
            if(ind == 1) return true;
            else if(ind == 2) return false;
            else return true;
        }
        case 1:{
            if(ind == 1) return false;
            else if(ind == 2) return false;
            else return false;
        }
        case 2:{
            if(ind == 1) return true;
            else if(ind == 2) return true;
            else return true;
        }
        case 3:{
            if(ind == 1) return true;
            else if(ind == 2) return true;
            else return true;
        }
        case 4:{
            if(ind == 1) return false;
            else if(ind == 2) return true;
            else return false;
        }
        case 5:{
            if(ind == 1) return true;
            else if(ind == 2) return true;
            else return true;
        }
        case 6:{
            if(ind == 1) return true;
            else if(ind == 2) return true;
            else return true;
        }
        case 7:{
            if(ind == 1) return true;
            else if(ind == 2) return false;
            else return false;
        }
        case 8:{
            if(ind == 1) return true;
            else if(ind == 2) return true;
            else return true;
        }
        case 9:{
            if(ind == 1) return true;
            else if(ind == 2) return true;
            else return true;
        }
        default:
            return true;
    }
}

bool isPossibleRow(int ind,int dir,int num){
    switch (num) {
        case 0:{
            if(ind == 1 && dir == 1) return true;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return true;
            else return true;
        }
        case 1:{
            if(ind == 1 && dir == 1) return false;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return false;
            else return true;
        }
        case 2:{
            if(ind == 1 && dir == 1) return false;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return true;
            else return false;
        }
        case 3:{
            if(ind == 1 && dir == 1) return false;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return false;
            else return true;
        }
        case 4:{
            if(ind == 1 && dir == 1) return true;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return false;
            else return true;
        }
        case 5:{
            if(ind == 1 && dir == 1) return true;
            else if(ind == 1 && dir == 2) return false;
            else if(ind == 2 && dir == 1) return false;
            else return true;
        }
        case 6:{
            if(ind == 1 && dir == 1) return true;
            else if(ind == 1 && dir == 2) return false;
            else if(ind == 2 && dir == 1) return true;
            else return true;
        }
        case 7:{
            if(ind == 1 && dir == 1) return false;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return false;
            else return true;
        }
        case 8:{
            if(ind == 1 && dir == 1) return true;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return true;
            else return true;
        }
        case 9:{
            if(ind == 1 && dir == 1) return true;
            else if(ind == 1 && dir == 2) return true;
            else if(ind == 2 && dir == 1) return false;
            else return true;
        }
        default:
            return true;
    }
}

int main(int argc, const char * argv[]) {
    // cin,cout 속도향상
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    cin >> s >> n;
    
    row = 2 * s + 3;
    col = s + 2;
    len = (int) n.size();
    mid = row/2 + 1;
    
    for(int i=1; i<=row; i++){
        for(int j=1; j<=col*len+len; j++){
            int pos = j % (col + 1);
            int n_pos = j / (col + 1);
            
            // 숫자를 출력한 뒤, 띄어쓰기를 해야한다면
            if(pos == 0){
                cout << " ";
                continue;
            }
            
            // 현재 숫자 구하기
            int num = n[n_pos] - '0';
            
            // '-' 출력
            if(i==1 || i== mid || i==row){
                // 양쪽 끝은 제외
                if(pos==1 || pos==col){
                    cout << " ";
                    continue;
                }
                
                // '상 중 하' 인덱스 구하기
                int ind;
                if(i==1) ind = 1;
                else if(i==row) ind = 3;
                else ind = 2;
                
                if(isPossibleCol(ind,num)){
                    cout << '-';
                }else{
                    cout << " ";
                }
            }
            // '|' 출력
            else{
                // 양쪽 끝이 아니라면 제외
                if(pos!=1 && pos!=col){
                    cout << " ";
                    continue;
                }
                
                // '중상 중하' 인덱스 구하기
                int ind;
                if(i <= row/2) ind = 1;
                else ind = 2;
                
                // 방향 구하기
                int dir;
                if(pos == 1) dir = 1;
                else dir = 2;
                
                if(isPossibleRow(ind,dir,num)){
                    cout << '|';
                }else{
                    cout << " ";
                }
            }
        }
        cout << "\n";
    }
    
    return 0;
}

 

[ 2차 소스코드 ]

문제 해결 시간 : 1h

메모리 : 1988 KB

시간 : 0 ms

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;

int num[11][5][3] = {
    {{0,1,0},{1,0,1},{0,0,0},{1,0,1},{0,1,0}},  // 0
    {{0,0,0},{0,0,1},{0,0,0},{0,0,1},{0,0,0}},  // 1
    {{0,1,0},{0,0,1},{0,1,0},{1,0,0},{0,1,0}},  // 2
    {{0,1,0},{0,0,1},{0,1,0},{0,0,1},{0,1,0}},  // 3
    {{0,0,0},{1,0,1},{0,1,0},{0,0,1},{0,0,0}},  // 4
    {{0,1,0},{1,0,0},{0,1,0},{0,0,1},{0,1,0}},  // 5
    {{0,1,0},{1,0,0},{0,1,0},{1,0,1},{0,1,0}},  // 6
    {{0,1,0},{0,0,1},{0,0,0},{0,0,1},{0,0,0}},  // 7
    {{0,1,0},{1,0,1},{0,1,0},{1,0,1},{0,1,0}},  // 8
    {{0,1,0},{1,0,1},{0,1,0},{0,0,1},{0,1,0}}   // 9
};

int s;
string digit;

int main(int argc, const char * argv[]) {
    // cin,cout 속도향상
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    cin >> s >> digit;
    
    for(int r=0; r<5; r++){
        // 'ㅡ' 출력
        if(r % 2 == 0){
            for(int i=0; i<digit.size(); i++){
                int d = digit[i] - '0';
                
                cout << " ";
                
                for(int j=0; j<s; j++)
                    num[d][r][1] == 1 ? cout << "-" : cout << " ";
                
                cout << " ";
                
                // 문자간 간격
                cout << " ";
            }
            cout << "\n";
        }
        // 'ㅣ' 출력
        else{
            for(int t=0; t<s; t++){
                for(int i=0; i<digit.size(); i++){
                    int d = digit[i] - '0';
                    
                    num[d][r][0] == 1 ? cout << "|" : cout << " ";
                    
                    for(int j=0; j<s; j++)
                        cout << " ";
                    
                    num[d][r][2] == 1 ? cout << "|" : cout << " ";
                    
                    // 문자간 간격
                    cout << " ";
                }
                cout << "\n";
            }
        }
    }
    
    return 0;
}

 

'Algorithm > BOJ 문제풀이' 카테고리의 다른 글

[BF] 1051번 숫자 정사각형  (0) 2019.01.17
[BF] 1065번 한수  (0) 2019.01.17
[SIM] 3568번 iSharp  (0) 2019.01.17
[DFS] 3019번 테트리스  (0) 2019.01.17
[DFS] 14500번 테트로미노  (0) 2019.01.16