[프로그래머스] N으로 표현
#include <string>
#include <queue>
#include<map>
#include <algorithm>
using namespace std;
int solution(int N, int number) {
map<int, int> cntMap;
queue<pair<int, int>> q;
q.push({0, 0});
q.push({1, N});
while(!q.empty()){
int curCnt = q.front().first;
int curNum = q.front().second;
q.pop();
if(curCnt > 8) continue;
if(cntMap.find(curNum) != cntMap.end())
cntMap[curNum] = min(cntMap[curNum], curCnt);
else cntMap[curNum] = curCnt;
int operand = N;
for(int digit = 1; digit <= 8; ++digit){
q.push({curCnt + digit, curNum + operand});
q.push({curCnt + digit, curNum - operand});
q.push({curCnt + digit, curNum * operand});
q.push({curCnt + digit, curNum / operand});
operand *= 10;
operand += N;
}
}
int answer = -1;
if(cntMap.find(number) != cntMap.end()) {
answer = cntMap[number];
if(answer > 8) answer = -1;
}
return answer;
}