Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Linux
- 셰그먼트트리
- 2단계로킹
- 오손데이터읽기
- hackerank
- 트랜잭션 관리
- oracle
- 언리얼 커스텀 플러그인
- 5639
- 데이터베이스 배움터
- command not found
- objtofbx
- UnrealMP
- 비재귀셰그먼트
- 백준
- OS
- 의미와 무의미의 경계에서
- FBX
- Unreal
- 민겸수
- 백준 1253번
- 실습
- UActor
- Security
- 1967번
- C++
- 언리얼 플러그인
- 1759번
- SQL
- 1253번
Archives
- Today
- Total
fatalite
프로그래머스 단어 변환 본문
다익스트라
#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int MAX_NUM = 100000000;
vector<vector<int>> GraphEdges;
vector<int> Dijkstra(int start)
{
vector<int> Dist;
for(int i = 0 ; i < GraphEdges.size(); i++) Dist.push_back(MAX_NUM);
Dist[start] = 0;
queue<int> q;
q.push(start);
while(!q.empty())
{
int cur = q.front();
q.pop();
for(int i = 0 ; i < GraphEdges[cur].size(); i++)
{
int originalDest = Dist[GraphEdges[cur][i]];
// cout << originalDest << endl;
if(Dist[cur] + 1 < originalDest)
{
// cout << cur << " " << GraphEdges[cur][i];
Dist[GraphEdges[cur][i]] = Dist[cur] + 1;
q.push(GraphEdges[cur][i]);
}
}
}
return Dist;
}
int solution(string begin, string target, vector<string> words) {
// Make Edges
words.push_back(begin);
GraphEdges = vector<vector<int>>(words.size());
for(int i = 0; i < words.size(); i++)
{
for(int j = 0; j < words.size(); j++)
{
if(i==j) continue;
int diff = 0;
for(int p = 0; p < words[i].size(); p++)
{
if(words[i][p] != words[j][p]) diff++;
}
if(diff == 1)
{
GraphEdges[i].push_back(j);
GraphEdges[j].push_back(i);
}
}
}
int start = -1;
int end = -1;
// Find
for(int i = 0 ; i < words.size(); i++)
{
if(words[i] == begin) start = i;
if(words[i] == target) end = i;
}
vector<int> minDistArr = Dijkstra(start);
if(minDistArr[end] == MAX_NUM) return 0;
return minDistArr[end];
}
'코딩 인터뷰 > C++' 카테고리의 다른 글
프로그래머스 이모티콘 할인행사 (3) | 2024.10.13 |
---|---|
프로그래머스 우박 수열 정적분 (0) | 2024.10.09 |
프로그래머스 n진수 게임 (0) | 2024.10.01 |
프로그래머스 - 괄호 변환 (0) | 2024.09.29 |
프로그래머스 C++ 삼각 달팽이 (0) | 2024.09.28 |