Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions 13_최단경로/필수/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 파티
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int INF = 1e9; // 큰 값

vector<pair<int, int>> graph[1001]; // 인접 리스트 그래프

// X에서 다른 마을로 가는 최단 거리 구하기(다익스트라 알고리즘 이용)
vector<int> dijkstra(int start, int n) {
vector<int> dist(n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

dist[start] = 0;
pq.push({0, start});

while (!pq.empty()) {
int cur_dist = pq.top().first;
int cur_node = pq.top().second;
pq.pop();

if (cur_dist > dist[cur_node]) continue;

for (auto &edge : graph[cur_node]) {
int next_node = edge.first;
int weight = edge.second;

if (dist[cur_node] + weight < dist[next_node]) {
dist[next_node] = dist[cur_node] + weight;
pq.push({dist[next_node], next_node});
}
}
}

return dist;
}

// 각 마을에서 X로 가는 최단 거리 계산
int findMax(int x, int n, vector<int> &dist_from_x){
int max_time = 0;

for (int i = 1; i <= n; i++) {
if (i == x) continue;

vector<int> dist_to_x = dijkstra(i, n);
max_time = max(max_time, dist_to_x[x] + dist_from_x[i]);
}

return max_time;
}

int main() {

// 입력
int n, m, x;
cin >> n >> m >> x;

for (int i = 0; i < m; i++) {
int u, v, t;
cin >> u >> v >> t;
graph[u].push_back({v, t});
}

// 연산
vector<int> dist_from_x = dijkstra(x, n);
int max_time = findMax(x, n, dist_from_x);

// 출력
cout << max_time << endl;

return 0;
}
79 changes: 79 additions & 0 deletions 13_최단경로/필수/15685.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 드래곤 커브
#include <iostream>
#include <vector>
#include <set>

using namespace std;

// 방향 이동 배열 (우, 상, 좌, 하)
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

// 드래곤 커브 생성
void generateDragonCurve(vector<pair<int, int>> &curve, int g) {
for (int generation = 0; generation < g; ++generation) {
int size = curve.size();
pair<int, int> end = curve.back();
for (int i = size - 2; i >= 0; --i) {
int x = curve[i].first;
int y = curve[i].second;
// 회전 변환
int newX = end.first - (y - end.second);
int newY = end.second + (x - end.first);
curve.push_back({newX, newY});
}
}
}

// 1x1 정사각형의 개수 계산
int calSquare(vector<vector<bool>> &grid){

int squareCount = 0;
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) {
if (grid[i][j] && grid[i + 1][j] && grid[i][j + 1] && grid[i + 1][j + 1]) {
++squareCount;
}
}
}
return squareCount;

}

int main() {
int n;
cin >> n;

// 101x101 격자를 초기화 (격자는 0~100까지 사용)
vector<vector<bool>> grid(101, vector<bool>(101, false));
Comment on lines +47 to +48
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 격자의 크기를 상수 변수로 선언해서 사용하면 가독성도 좋아지고 실수할 일이 줄어들겠네요 🥰


// 입력 및 드래곤 커브 생성
for (int i = 0; i < n; ++i) {
int x, y, d, g;
cin >> x >> y >> d >> g;

// 초기 드래곤 커브 설정
vector<pair<int, int>> curve;
curve.push_back({x, y});
curve.push_back({x + dx[d], y + dy[d]});

// 드래곤 커브 생성
generateDragonCurve(curve, g);

// 격자에 드래곤 커브 표시
for (auto c : curve) {
int cx = c.first;
int cy = c.second;
if (cx >= 0 && cx <= 100 && cy >= 0 && cy <= 100) {
grid[cx][cy] = true;
}
}
Comment on lines +64 to +70
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 문제에서 드래곤 커브는 격자의 크기를 벗어나지 않는다고 했으니 x, y의 범위는 체크하지 않아도 될 것 같아요🤗🤗

}

int squareCount = calSquare(grid);

// 출력
cout << squareCount << endl;

return 0;
}
63 changes: 63 additions & 0 deletions 13_최단경로/필수/2458.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 키 순서
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1e9;

// 플로이드-워셜 알고리즘으로 모든 쌍의 관계 계산한다.
void calGraph(vector<vector<bool>> &graph, int n){

for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (graph[i][k] && graph[k][j]) {
graph[i][j] = true;
Comment on lines +11 to +15
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배운 개념을 활용해서 잘 구현해주셨네요 👍👍👍

}
}
}
}

}

// 자신의 키 순위를 알 수 있는 학생 찾아서 반환한다.
int findAnswer(vector<vector<bool>> &graph, int n){
int count = 0;

for (int i = 1; i <= n; ++i) {
int smaller = 0, larger = 0;

for (int j = 1; j <= n; ++j) {
if (graph[i][j]) smaller++; // i보다 큰 학생 수
if (graph[j][i]) larger++; // i보다 작은 학생 수
}

if (smaller + larger == n - 1) {
count++; // 자신보다 크거나 작은 학생 수가 정확히 N-1이면 순위를 알 수 있음
}
}

return count;
}

int main() {
int n, m; // 학생들의 수 n (2 ≤ n ≤ 500), 두 학생 키를 비교한 횟수 m (0 ≤ m ≤ n(n-1)/2)

// 입력
cin >> n >> m;
vector<vector<bool>> graph(n + 1, vector<bool>(n + 1, false));
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
graph[a][b] = true; // a < b
}

// 연산
calGraph(graph, n);
int answ = findAnswer(graph, n);

// 출력
cout << answ << endl;

return 0;
}
Comment on lines +43 to +63
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메인함수 깔끔하고 좋습니다👍👍👍