문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/1835
코딩테스트 연습 - 단체사진 찍기
단체사진 찍기 가을을 맞아 카카오프렌즈는 단체로 소풍을 떠났다. 즐거운 시간을 보내고 마지막에 단체사진을 찍기 위해 카메라 앞에 일렬로 나란히 섰다. 그런데 각자가 원하는 배치가 모두
programmers.co.kr
모든 경우의 수에 대해 시뮬레이션을 진행하면 되는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <iostream> | |
using namespace std; | |
const int MAX = 8; | |
const char names[MAX] = {'A', 'C', 'F', 'J', 'M', 'N', 'R', 'T'}; | |
int result; | |
bool isPossible(string friends, vector<string> &data) | |
{ | |
for (string d : data) | |
{ | |
int from = friends.find(d[0]); | |
int target = friends.find(d[2]); | |
char op = d[3]; | |
int distance = (d[4] - '0') + 1; | |
int realDistance = abs(from - target); | |
switch (op) | |
{ | |
case '=': | |
if (!(realDistance == distance)) | |
{ | |
return false; | |
} | |
break; | |
case '>': | |
if (!(realDistance > distance)) | |
{ | |
return false; | |
} | |
break; | |
case '<': | |
if (!(realDistance < distance)) | |
{ | |
return false; | |
} | |
break; | |
} | |
} | |
return true; | |
} | |
void func(string friends, bool visited[MAX], vector<string> &data) | |
{ | |
if (friends.length() == MAX - 1) | |
{ | |
result += isPossible(friends, data); | |
return; | |
} | |
for (int i = 0; i < MAX; i++) | |
{ | |
if (visited[i]) | |
{ | |
continue; | |
} | |
visited[i] = true; | |
func(friends + names[i], visited, data); | |
visited[i] = false; | |
} | |
} | |
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요. | |
int solution(int n, vector<string> data) { | |
result = 0; | |
bool visited[MAX] = {false, }; | |
func("", visited, data); | |
return result; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 오픈채팅방 (0) | 2022.01.26 |
---|---|
[Programmers] 카카오프렌즈 컬러링북 (0) | 2022.01.26 |
[Programmers] k진수에서 소수 개수 구하기 (0) | 2022.01.19 |
[Programmers] 신고 결과 받기 (3) | 2022.01.16 |
[Programmers] 소수 찾기 (0) | 2021.12.21 |