登录后台

页面导航

本文编写于 1360 天前,最后修改于 1360 天前,其中某些信息可能已经过时。

USACO 2021 February Contest, Bronze Problem 3. Clockwise Fence

The fence surrounding Farmer John's largest pasture has fallen into disrepair, and he has finally decided to replace it with a new fence.

Unfortunately, as Farmer John is laying out the new fence, a large bee ends up chasing him around the pasture, and as a result, the fence ends up following a rather irregular path. The fence can be described by a string of characters, each either "N" (north), "E" (east), "S" (south), or "W" (west). Each character describes a 1-meter run of the fence. For example, if the string is NESW, this means the fence starts by moving north for 1 meter, then east for 1 meter, then south for 1 meter, then west for 1 meter, returning to its starting point.

The fence ends at the position where it started, and this is the only point visited more than once by the path of the fence (and the starting point is only re-visited once, at the end). As a result, the fence does indeed enclose a single connected region of the grassy pasture, even though this region could have a rather strange shape.

Farmer John is curious if the path in which he laid the fence traveled clockwise (with the enclosed region on the right side of the fence as one walks along the path of the fence in the order specified by the string) or counter-clockwise (with the enclosed region on the left side of the fence).

INPUT FORMAT (input arrives from the terminal / stdin):

The first line of input contains an integer NN (1≤N≤201≤N≤20). Each of the next NN lines contains a string of length at least 4 and at most 100, describing a single fence path.

OUTPUT FORMAT (print output to the terminal / stdout):

For each of the NN fence paths described in the input, output a line containing either "CW" (clockwise) or "CCW" (counterclockwise).

SAMPLE INPUT:

2
NESW
WSSSEENWNEESSENNNNWWWS

SAMPLE OUTPUT:

CW
CCW

The two fence paths with denoting the starting point:

*>*
^ v

<*

  *<*<*<*
  v     ^
*<
     *
v       ^
* *>*>* *
v ^   v ^
* *<* * *
v   ^ v ^
*>*>* *>*

Problem credits: Brian Dean

题解

参照于:空间多边形顺逆时针的判断

根据字符串模拟运动方向,根据向量判断顺逆时针即可。

代码

#include <bits/stdc++.h>
using namespace std;
char a[200]={0};
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",a+1);
        int x=0,y=0,sumz=0,sumf=0;
        for(int j=1;a[j];j++)
        {
            int xx,yy;
            if(a[j]=='N')
                yy=y-1;
            else if(a[j]=='S')
                yy=y+1;
            else if(a[j]=='W')
                xx=x-1;
            else if(a[j]=='E')
                xx=x+1;
            int val=x*yy-xx*y;
            if(val>0)
                sumz++;
            else if(val<0)
                sumf++;
            x=xx,y=yy;
        }    
        if(sumz<sumf)
            printf("CCW\n");
        else
            printf("CW\n");
    }
}