藉由存儲上節點做標與狀態可以判斷現在的狀態改變方向和方位
#include <iostream> #define endl '\n' using namespace std; struct Point { int x, y; }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, d = 0, right_turn = 0, left_turn = 0, re_turn = 0; Point cur, last = {0, 0}; cin >> n; for (int i = 1; i <= n; i++) { cin >> cur.x >> cur.y; if (d == 0) { if (cur.x - last.x > 0) { d = 1; } else if (cur.x - last.x < 0) { d = -1; } else if (cur.y - last.y > 0) { d = 2; } else { d = -2; } } else if (cur.x - last.x > 0) { if (d == -1) { re_turn++; } else if (d == 2) { right_turn++; } else if (d == -2) { left_turn++; } d = 1; } else if (cur.x - last.x < 0) { if (d == 1) re_turn++; else if (d == 2) left_turn++; else if (d == -2) right_turn++; d = -1; } else if (cur.y - last.y > 0) { if (d == -2) re_turn++; else if (d == 1) left_turn++; else if (d == -1) right_turn++; d = 2; } else { if (d == 2) re_turn++; else if (d == 1) right_turn++; else if (d == -1) left_turn++; d = -2; } last = cur; } cout << left_turn << " " << right_turn << " " << re_turn << endl; return 0; }
怎麼直接貼答案。
我很抱歉,這邊趕緊寫了一份詳細解析作為對社群的補償。
https://github.com/banahaker/ZeroJudge/blob/main/APCS_2023/202306.md