---
# System prepended metadata

title: 題解 zerojudge k731. 1. 路徑偵測
tags: [zerojudge, APCS, c++, k371]

---

###### tags:`zerojudge` `k371` `APCS` `c++`
# 題解 zerojudge k731. 1. 路徑偵測
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;cin>>n;
    int x1,y1,x2,y2;
    x1=y1=0;
    int l=0,r=0,u=0;
    int d[150]={0};
    //NWSE->0123 先記錄方向
    for(int i=0;i<n;i++){
        cin>>x2>>y2;
        if(x1==x2){
            if(y2>y1)   d[i]=0;//N北
            else d[i]=2;//S南
        }
        else{
            if(x2>x1)   d[i]=3;//E東
            else d[i]=1;//W西
        }
        x1=x2;y1=y2;
    }
    for(int i=1;i<n;i++){
        if(d[i]-d[i-1]==1)  l++;
        else if(d[i]-d[i-1]==-1)    r++;
        else if((d[i]+2)%4==d[i-1]) u++;
        else if(d[i]-d[i-1]==3)    r++;
        else if(d[i]-d[i-1]==-3)    l++;
    }
    cout<<l<<' '<<r<<' '<<u;
}
```