알고리즘
[C++] 프로그래머스 - 비밀지도
J3SUNG
2021. 4. 30. 18:55
728x90
2018 KAKAO BLIND RECRUITMENT (2018 카카오 블라인드 채용 문제)
비트 연산자를 활용하여 문제를 해결하였습니다.
주어지는 2개의 배열을 &연산으로 합칩니다.
그리고 &연산을 통해서 벽이 있는지 확인합니다.
ex) 10 (n = 5)
"01010"
01010 & 10000 => 0
01010 & 01000 => 1
01010 & 00100 => 0
01010 & 00010 => 1
01010 & 00001 => 0
0인 경우에 string에 " "을 삽입
1인 경우에 string에 "#"을 삽입
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> solution(int n, vector<int> arr1, vector<int> arr2)
{
int bit;
string s;
vector<string> answer;
for (int i = 0; i < n; ++i)
{
s = "";
bit = arr1[i] | arr2[i];
for (int j = 1; j <= n; ++j)
{
if (bit & 1 << n - j)
{
s += "#";
}
else
{
s += " ";
}
}
answer.push_back(s);
}
return answer;
}