diff --git a/kangrae-jo/README.md b/kangrae-jo/README.md index 34ad789..5cd48e5 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -15,5 +15,5 @@ | 11차시 | 2024.12.21 | Greedy | [ATM](https://www.acmicpc.net/problem/11399)|[#41](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/41)| | 12차시 | 2024.12.24 | HEAP | [크리스마스 선물](https://www.acmicpc.net/problem/14235)|[#44](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/44)| | 13차시 | 2024.12.28 | Graph | [줄 세우기](https://www.acmicpc.net/problem/2252)|[#46](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/46)| - +| 17차시 | 2024.01.18 | Simulation | [충돌 위험 찾기](https://school.programmers.co.kr/learn/courses/30/lessons/340211)|[#61](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/61)| --- diff --git a/kangrae-jo/Simulation/17-kangrae-jo.cpp b/kangrae-jo/Simulation/17-kangrae-jo.cpp new file mode 100644 index 0000000..f3534ad --- /dev/null +++ b/kangrae-jo/Simulation/17-kangrae-jo.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +int solution(vector> points, vector> routes) { + int answer = 0; + + // 각 시점에서 로봇들의 위치를 기록할 맵 (시간, 좌표) -> 로봇 수 + vector, int>> history(100005); + for (auto& route : routes) { + int time = 0; + int y1 = points[route[0] - 1][0]; // 첫 번째 포인트의 y 좌표 + int x1 = points[route[0] - 1][1]; // 첫 번째 포인트의 x 좌표 + + history[time][{y1, x1}]++; + time++; + + for (int i = 0; i < route.size() - 1; i++) { + y1 = points[route[i] - 1][0]; // 현재 위치 y 좌표 + x1 = points[route[i] - 1][1]; // 현재 위치 x 좌표 + const int y2 = points[route[i + 1] - 1][0]; // 목표점 y 좌표 + const int x2 = points[route[i + 1] - 1][1]; // 목표점 x 좌표 + + // y먼저 이동, 그 뒤에 x이동 + while (y1 != y2 || x1 != x2) { + if (y1 != y2) y1 += (y2 > y1 ? 1 : -1); + else x1 += (x2 > x1 ? 1 : -1); + + history[time][{y1, x1}]++; + time++; + } + } + } + + // 위험한 상황이 발생한 횟수 계산 + for (const auto& historyMap : history) { + for (const auto& h : historyMap) { + if (h.second >= 2) answer++; + } + } + + return answer; +} \ No newline at end of file