쾌락없는 책임 (공부)/알고리즘 문제풀이
[Algorithm] 프로그래머스 순위 - C++, 플로이드 와샬
허스크
2022. 3. 4. 14:07
반응형
코딩테스트 연습 - 순위
5 [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]] 2
programmers.co.kr
#include <vector>
using namespace std;
bool fight[110][110];
int solution(int n, vector<vector<int>> results) {
int answer = 0;
// make input
for(int i = 0; i < results.size(); i++)
fight[results[i][0]][results[i][1]] = true;
// floyid
for(int via = 1; via <= n; via++){
for(int from = 1; from <= n; from++){
if(!fight[from][via]) continue;
for(int to = 1; to <= n; to++){
if(fight[via][to])
fight[from][to] = true;
}
}
}
// for(int from = 1; from <= n; from++){
// for(int via = 1; via <= n; via++){
// if(fight[from][via] == false) continue;
// for(int to = 1; to <= n; to++){
// if(fight[from][via] && fight[via][to])
// fight[from][to] = true;
// }
// }
// }
// search
for(int i = 1; i <= n; i++){
int count = 0;
for(int j = 1; j <= n; j++){
if(i == j) continue;
if(fight[i][j] || fight[j][i])
count++;
}
if(count == n-1)
answer++;
}
return answer;
}
플로이드 와샬 아이디어로 왜 안되나 했는데 플로이드 와샬 순서가 via-from-to 라고 합니다... 지금까지 from-via-to 라고 알아서 계속 헛걸음을 했네요. 혹시나 테스트 2, 7, 8 정도를 계속 틀린다면 탐색 순서를 보면 좋을 듯 합니다.
반응형