굥뷰를 햡시댜

[2017_Blind_Kakao] 비밀지도 본문

알고리즘 문제 풀이/2018 Kakao Blind 문제 풀이

[2017_Blind_Kakao] 비밀지도

GodZ 2019. 7. 29. 14:33

 

https://programmers.co.kr/learn/courses/30/lessons/17681

 

코딩테스트 연습 - [1차] 비밀지도 | 프로그래머스

비밀지도 네오는 평소 프로도가 비상금을 숨겨놓는 장소를 알려줄 비밀지도를 손에 넣었다. 그런데 이 비밀지도는 숫자로 암호화되어 있어 위치를 확인하기 위해서는 암호를 해독해야 한다. 다행히 지도 암호를 해독할 방법을 적어놓은 메모도 함께 발견했다. 지도는 한 변의 길이가 n인 정사각형 배열 형태로, 각 칸은 공백(" ) 또는벽(#") 두 종류로 이루어져 있다. 전체 지도는 두 장의 지도를 겹쳐서 얻을 수 있다. 각각 지도 1과 지도 2라고 하자. 지도 1

programmers.co.kr

STL을 사용할 수 있을 경우 난이도는 낮다

 

단순히 문자열을 가지고 노는 문제로 빡구현하면 시간초과도 안나게 구현할 수 있다.

 

#include <string>
#include <vector>

using namespace std;

vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
    vector<string> answer;
    vector<string> map1, map2;
    
    for (int i = 0; i < arr1.size(); i++) {
		string str;

		while (arr1[i] != 0) {
			int ch = arr1[i] % 2;
			arr1[i] /= 2;
			
			str += to_string(ch);
		}
		string temp;
		for (int j = str.length() - 1; j >= 0; j--) {
			temp += str[j];
		}
		if (temp.length() != n) {
			int len = n - temp.length();

			string zero;
			for (int k = 0; k < len; k++) {
				zero += "0";
			}
			temp = zero + temp;
		}
		map1.push_back(temp);
	}

	for (int i = 0; i < arr2.size(); i++) {
		string str;

		while (arr2[i] != 0) {
			int ch = arr2[i] % 2;
			arr2[i] /= 2;

			str += to_string(ch);
		}
		string temp;
		for (int j = str.length() - 1; j >= 0; j--) {
			temp += str[j];
		}
		if (temp.length() != n) {
			int len = n - temp.length();

			string zero;
			for (int k = 0; k < len; k++) {
				zero += "0";
			}
			temp = zero + temp;
		}
		map2.push_back(temp);
	}

	for (int i = 0; i < n; i++) {
		string s1 = map1[i];
		string s2 = map2[i];

		string temp;

		for (int j = 0; j < n; j++) {
			if (s1[j] - 48 == 1 || s2[j] - 48 == 1) {
				temp += "#";
			}
			else temp += " ";
		}
		answer.push_back(temp);
	}
    return answer;
}

 

 

Comments