매일 C++ day 11 / N으로 표현

2021-06-09

N으로 표현

https://programmers.co.kr/learn/courses/30/lessons/42895?language=cpp


#include <vector>
#include <unordered_set>
using namespace std;

int solution(int N, int number) {
    if(N==number) return 1;
	vector<unordered_set<int>> cache(8);
	cache[0].insert(N);
	for(int i=1; i<8; i++)
		cache[i].insert(10*(*cache[i-1].begin()) + N);


	for(int count=1; count<8; count++)
	{
		for(int i=0; i<8; i++)
		{
			for(auto it:cache[i])
			{
                		if(count-i-1<0) continue;
				for(auto iter:cache[count-i-1])
				{
					cache[count].insert(it+iter);
					cache[count].insert(it*iter);
					if(it-iter>=1) cache[count].insert(it-iter);
					if(it/iter>=1) cache[count].insert(it/iter);
				}
			}
			if(cache[count].find(number) != cache[count].end())
			return count+1;
		}
	}
	
	return -1;
}