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;
}