Study hard

(c++)프로그래머스 코딩테스트 연습 - 카펫 본문

프로그래머스/완전탐색

(c++)프로그래머스 코딩테스트 연습 - 카펫

Nimgnoej 2020. 10. 20. 13:19

programmers.co.kr/learn/courses/30/lessons/42842

 

코딩테스트 연습 - 카펫

Leo는 카펫을 사러 갔다가 아래 그림과 같이 중앙에는 노란색으로 칠해져 있고 테두리 1줄은 갈색으로 칠해져 있는 격자 모양 카펫을 봤습니다. Leo는 집으로 돌아와서 아까 본 카펫의 노란색과 ��

programmers.co.kr

[풀이]

노란 격자의 모든 가능한 경우에 대해 노란 격자를 감싸는 테두리의 개수가 brown과 같은지 비교하는 방법으로 풀었다.

#include <string>
#include <vector>

using namespace std;

int Brown,Yellow;
vector<int>Answer;

bool getRowCol(int row, int col){
    int tmp=row*2+col*2+4;
    if(Brown==tmp){
        Answer.push_back(row+2);
        Answer.push_back(col+2);
        return true;
    }
    return false;
}

vector<int> solution(int brown, int yellow) {
    Brown=brown;
    Yellow=yellow;
    for(int i=1;i<=Yellow;i++){
        if(Yellow/i<=i){
            if(getRowCol(i,Yellow/i))
                break;
        }
    }
    return Answer;
}