본문 바로가기

Algorithm/Brute Force

(C++) - 백준(BOJ) 2659번 : 십자카드 문제

반응형

www.acmicpc.net/problem/2659

 

2659번: 십자카드 문제

입력은 한 줄로 이루어지며, 이 한 줄은 카드의 네 모서리에 씌여있는 1 이상 9 이하의 숫자 4개가 시계 방향으로 입력된다. 각 숫자 사이에는 빈칸이 하나 있다.

www.acmicpc.net

모든 경우의 수를 확인하는 brute force문제였습니다.

 

풀이방법

 1. 1111부터 9999까지의 모든 숫자카드를 확인합니다.

 2. 한 숫자카드의 시계수를 구합니다.

   dqeue을 이용해 4개의 경우로 시계수를 만든 후 가장 작은 시계수를 반환합니다.

 3. 한 시계수가 이전 시계수들보타 크다면 ans++ 후 값을 그 시계수로 갱신해줍니다.

 4. 만약 현재 시계수가 입력받은 십자카드로부터 나온 시계수와 같다면 답을 출력하고 loop를 탈출합니다.

 

Code

#include <bits/stdc++.h>
using namespace std;
int ans, maxi;

int getClockNum(string s){
    deque <char> dq;
    int smallInt = 0x3f3f3f3f;
    for(int i = 0; i < s.size(); i++){
        dq.push_back(s[i]);
    }

    for(int i = 0; i < s.size(); i++){
        string tmp = "";
        for(int j = 0; j < s.size(); j++)
            tmp += dq.at(j);
        smallInt = min(smallInt,stoi(tmp));
        dq.push_back(dq.front());
        dq.pop_front();
    }
    return smallInt;
}

int main(){
    string num="";
    for(int i = 0; i < 4; i++){
        string s; 
        cin >> s;
        num += s;
    }
    for(int card = 1111; card <= 9999; card++){
        string s = to_string(card);
        int clockNum = getClockNum(s);
        if(maxi < clockNum){
            maxi = clockNum;
            ans++;
        }
        if(clockNum == getClockNum(num)){
            cout << ans;
            break;
        }
    }
}