Skip to content

USACO 2021 February Contest Bronze Division - Clockwise Fence#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Use adjacent cross products to check if the path is clockwise or counter clockwise.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while(t--)
    {
        string s;
        cin >> s;
        int ans = 0;
        int x = 0, y = 0;
        for(int j = 0; j < s.size(); j++)
        {
            int x2 = x, y2 = y;
            if(s[j] == 'W')
                y2--;
            if(s[j] == 'E')
                y2++;
            if(s[j] == 'N')
                x2++;
            if(s[j] == 'S')
                x2--;
            ans += x * y2 - y * x2;
            x = x2, y = y2;
        }
        cout << (ans <= 0 ? "CCW" : "CW") << '\n';
    }

    return 0;
}
t = int(input())
for _ in range(t):
    s = input().strip()
    ans = 0
    x, y = 0, 0
    for ch in s:
        x2, y2 = x, y
        if ch == 'W':
            y2 -= 1
        elif ch == 'E':
            y2 += 1
        elif ch == 'N':
            x2 += 1
        elif ch == 'S':
            x2 -= 1
        ans += x * y2 - y * x2
        x, y = x2, y2
    print("CCW" if ans <= 0 else "CW")