[백준] 노드사이의 거리(C++)
·
개발/알고리즘
문제 링크https://www.acmicpc.net/problem/1240 코드 구현#include #include #include using namespace std;vector visited;int answer = 0;void dfs(int _node, int _end, int _dist, const vector>& tree){ if (_node == _end) { answer += _dist; return; } for (int i = 0; i > N >> M; vector> tree(N, vector(N)); visited.resize(N); for (int i = 0; i > node1 >> node2 >> dist; tree[node1 - 1][node2 - 1] = dist; tree..
[백준] RGB거리(C++)
·
개발/알고리즘
문제 링크https://www.acmicpc.net/problem/1149 코드 구현#include #include #include #include using namespace std;int main(){ int n; cin >> n; vector> rgbCost(n, vector(3)); vector> curCost(n, vector(3)); for (int i = 0; i > rgbCost[i][j]; } } curCost[0][0] = rgbCost[0][0]; curCost[0][1] = rgbCost[0][1]; curCost[0][2] = rgbCost[0][2]; for (int i = 1; i 해결 방법현재까지의 총 비..
[백준] 스타트와 링크(C++)
·
개발/알고리즘
문제 링크https://www.acmicpc.net/problem/14889 코드 구현#include #include using namespace std;vector> Stats;int minValue = 100;bool selected[20];int Diff(){ int diff; int S = 0, L = 0; // selected 에서 true 면 스타트 팀, false 면 링크 팀 for (int i = 0; i 0) ? diff : -diff; return diff;}// idx : 현재 팀원, count : 팀원 수void Backtrack(int idx, int count){ // 팀원 수는 전체 인원의 절반 if (count == Stats.size() / 2) { // 팀이 만들어졌..
[프로그래머스] 하노이의 탑(C++)
·
개발/알고리즘
문제 링크https://school.programmers.co.kr/learn/courses/30/lessons/12946 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 코드 풀이#include #include #include using namespace std;vector> answer;// 원판 개수, 시작 기둥, 보조 기둥, 목적지 기둥void Hanoi(int n, int start, int via, int dest){ if (n == 1) { answer.push_back({ start, dest }); return; } Hanoi(n -..
[프로그래머스] 타겟 넘버(C++)
·
개발/알고리즘
문제 링크https://school.programmers.co.kr/learn/courses/30/lessons/43165 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 코드 구현#include #include #include using namespace std;int answer = 0;void dfs(const vector& numbers, int _Index, int _sum, const int& _target){ if (_Index == numbers.size()) { if (_sum == _target) answer++; return; } ..
[백준] 2606번: 바이러스(C++)
·
개발/알고리즘
문제 링크https://www.acmicpc.net/problem/2606 코드 구현#include #include #include #include using namespace std;unordered_map> adjList;unordered_set visited;void dfs(int node){ // 방문 체크 visited.insert(node); for (const int& neighbor : adjList[node]) { // 방문하지 않은 컴퓨터인 경우 dfs() 재귀 호출 if (visited.find(neighbor) == visited.end()) { dfs(neighbor); } }}int solution(int _computerCount, int _pairCount)..
[프로그래머스] 미로 탈출(C++)
·
개발/알고리즘
문제 링크https://school.programmers.co.kr/learn/courses/30/lessons/159993 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 코드 구현#include #include #include #include using namespace std;struct Point{ int y, x; int score; bool operator==(const Point& other) const { return y == other.y && x == other.x; } Point operator+(const Point& other) con..
[프로그래머스] 게임 맵 최단거리(C++)
·
개발/알고리즘
문제 링크https://school.programmers.co.kr/learn/courses/30/lessons/1844 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 코드 구현#include #include #include using namespace std;struct Point{ int y, x; Point operator+(const Point& other) const { return Point(y + other.y, x + other.x); } bool operator==(const Point& other) const { if..
BFS 구현(C++)
·
개발/알고리즘
#include #include #include #include #include using namespace std;/** 너비 우선 탐색으로 모든 노드를 순회하는 함수 solution()을 작성하세요.* 시작 노드는 정수형 start로 주어집니다. graph 배열은 [출발 노드, 도착 노드] 쌍이 들어 있는 배열입니다.* 반환값은 그래프의 시작 노드부터 모든 노드를 너비 우선 탐색한 경로가 순서대로 저장된 배열입니다.*/unordered_map> adjList;vector result;void bfs(int start){ unordered_set visited; queue q; // 시작 노드 방문 q.push(start); visited.insert(start); result.push_back(sta..
DFS 구현 (C++)
·
개발/알고리즘
#include #include #include #include using namespace std;/** 깊이 우선 탐색으로 모든 그래프의 노드를 순회하는 함수 solution()을 작성하시오* 시작노드는 문자형 start로 주어집니다. graph 배열은 [출발 노드, 도착 노드] 쌍들이 들어있는 배열입니다.* 반환값은 그래프의 시작 노드부터 모든 노드를 깊이 우선 탐색으로 탐색한 경로가 순서대로 저장된 배열입니다.*/unordered_map> adjList;vector result;unordered_set visited;void dfs(char node){ // 현재 노드를 방문 목록과 방문 경로에 추가 visited.insert(node); result.push_back(node); // 현재 노드..