Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* http://gosucoder.com/problem/P2 */
- /* The plan is as follows:
- * 1. Track the (x,y) coordinates on each step (the starting point is (0,0))
- * 2. Calculate and remember the diagonal d where the coordinates are (the diagonal that goes through the current point and cuts the Y axis in (0,d))
- * 3. Two points found on the same diagonal indicate a starting and ending points of a line segment (increment count)
- * 3a. After finding a line segment, we need to remove the diagonal from our set, because another line segment can be found on the same diagonal (number of line segments = number of points / 2)
- * 3b. Some points have to be ignored: there's no line segment ending/starting in corners like '┘' (EN / SW) or '┌' (NE, WS), so we must keep track of what the next unit shift is
- */
- using System;
- using System.Collections.Generic;
- public class Test
- {
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- string a = Console.ReadLine();
- a = a + a[0];
- int x = 0, y = 0, count = 0;
- HashSet<int> diagonals = new HashSet<int>();
- for (int i = 0; i < n; i++)
- {
- switch (a[i])
- {
- case 'N': y++; if (a[i + 1] == 'E') continue; break;
- case 'S': y--; if (a[i + 1] == 'W') continue; break;
- case 'E': x++; if (a[i + 1] == 'N') continue; break;
- case 'W': x--; if (a[i + 1] == 'S') continue; break;
- }
- int d = y - x;
- if (diagonals.Contains(d))
- diagonals.Remove(d);
- else
- {
- diagonals.Add(d);
- count++;
- }
- }
- Console.WriteLine(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement