[프로그래머스 C++] 단어 변환
[프로그래머스 C++] 단어 변환
https://school.programmers.co.kr/learn/courses/30/lessons/43163
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해결전략
깊이우선탐색 DFS
정답코드
#include <iostream> #include <string> #include <vector> #include <cmath> using namespace std; int answer = 987654321; int n; // 단어의 개수 int len; // 단어의 길이 vector<int> ch; // 각 단어의 방문 여부를 저장할 벡터 bool canChange(const string& str1, const string& str2) // 두 문자열이 한 글자만 다른지 확인하는 함수 { int diffCount = 0; // 다른 글자 수를 카운팅하는 변수 for (int i = 0; i < str1.size(); i++) { if (str1[i] != str2[i]) { diffCount++; // 글자가 다르다면 ++ } if (diffCount > 1) { return false; // 두 글자 이상 다른 경우 false 반환 } } return diffCount == 1; // 한 글자만 다른 경우 true 반환 } void DFS(string str, const string& target, const vector<string>& words, int cnt) { if (str == target){ // 현재 문자열이 목표 문자열과 같은 경우 answer = min(answer, cnt); return; } for (int i = 0; i < n; i++) // 모든 단어를 탐색 { if (ch[i] == 0 && canChange(str, words[i])) // 방문X && 현재 단어와 한 글자만 다른 경우 { ch[i] = 1; DFS(words[i], target, words, cnt + 1); ch[i] = 0; } } } int solution(string begin, string target, vector<string> words) { len = begin.size(); n = words.size(); ch.resize(n); DFS(begin, target, words, 0); answer = answer == 987654321 ? 0 : answer; return answer; } int main() { string begin = "hit"; string target = "cog"; vector<string> words = {"hot", "dot", "dog", "lot", "log", "cog"}; int result = solution(begin, target, words); cout << result << endl; return 0; }
'⭐ 코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] 징검다리 건너기 (0) | 2024.06.16 |
---|---|
[프로그래머스 C++] 네트워크 (1) | 2024.06.15 |
[프로그래머스 C++] 이중우선순위큐 (0) | 2024.06.12 |
[프로그래머스 C++] [PCCP 기출문제] 2번 / 석유 시추 (0) | 2024.06.11 |
[프로그래머스 C++] 택배 배달과 수거하기 (0) | 2024.06.10 |
댓글
이 글 공유하기
다른 글
-
[프로그래머스 C++] 징검다리 건너기
[프로그래머스 C++] 징검다리 건너기
2024.06.16[프로그래머스 C++] 징검다리 건너기 https://school.programmers.co.kr/learn/courses/30/lessons/64062 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 해결전략 이분 탐색슬라이딩 윈도우 알고리즘 Sliding Window Algorithm 처음 시도한 코드- 효율성 7 ~ 14 통과X #include #include #include #include using namespace std;// 연속된 k개의 징검다리의 각각의 수가 낮은거 찾아야 함// 처음부터 k개씩 묶은 후 가장 높은 수를 기록하며, k개씩… -
[프로그래머스 C++] 네트워크
[프로그래머스 C++] 네트워크
2024.06.15[프로그래머스 C++] 네트워크 https://school.programmers.co.kr/learn/courses/30/lessons/43162 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 해결전략 너비우선탐색 BFS 유니온 앤 파인드 Union & Find 아래 풀이는 BFS 처음 시도한 코드 #include #include #include using namespace std;int answer; // 네트워크 개수int cnt; // 연결된 컴퓨터 수vector> ch; // 방문 여부를 체크void BFS(int start, int n, vect… -
[프로그래머스 C++] 이중우선순위큐
[프로그래머스 C++] 이중우선순위큐
2024.06.12 -
[프로그래머스 C++] [PCCP 기출문제] 2번 / 석유 시추
[프로그래머스 C++] [PCCP 기출문제] 2번 / 석유 시추
2024.06.11[프로그래머스 C++] [PCCP 기출문제] 2번 / 석유 시추 https://school.programmers.co.kr/learn/courses/30/lessons/250136 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 해결전략 너비우선탐색 BFS맵 Map, 셋 Set BFS를 돌려 석유 덩어리의 석유 매장량(= oilAmount)를 구한다. BFS 시행 후 해당 석유 덩어리가 걸쳐있는 x를 모두 set xLocation 에 담아준다. set xLocation 에 담긴 x 위치에 석유 매장량을 더해서 담는다.매장된 석유 덩어리 끼리는 맡닿아 …
댓글을 사용할 수 없습니다.