算法1
(模拟) $O(n)$
向右转加90 向左减90,(不能掉头,掉头就走回头路就有重复点了)
最后达到和原来方向一致时,即转了一圈,为360就是顺时针,-360就是逆时针;
[HTML_REMOVED]
[HTML_REMOVED]
参考文献
https://www.acwing.com/activity/content/code/content/2854489/
C++ 代码
#include<bits/stdc++.h>
using namespace std ;
const int N = 1e3 + 1;
int n , m;
string s ;
int main()
{
map<char,int> mp ;
mp['N'] = 0 , mp['E'] = 90 , mp['S'] = 180 , mp['W'] = 270 ;
int t ; cin >> t ;
while( t -- )
{
//这里加上初始是为了和原来方向一至,因为回到原点可能是其他三个方向回来的,
//当然也可以不加,这样的话,回到原点方向上就不是转一圈,
//把判断条件调整为大于零即可
cin >> s ; s += s[0] ; n = s.size() ;
int T = 0 ;
for( int i = 1 ; i < n ; i ++ )
{
int turn = mp[s[i]] - mp[s[i - 1]] ;
if( turn == 270 ) turn = -90 ;
if( turn == -270 ) turn = 90 ;
T += turn ;
}
if( T == 360 ) cout << "CW\n" ;
else cout << "CCW\n" ;
}
return 0;
}
nb