Advertisement
Miklak

Hatched Drawings

Oct 21st, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. /* http://gosucoder.com/problem/P2 */
  2. /* The plan is as follows:
  3.  * 1. Track the (x,y) coordinates on each step (the starting point is (0,0))
  4.  * 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))
  5.  * 3. Two points found on the same diagonal indicate a starting and ending points of a line segment (increment count)
  6.  * 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)
  7.  * 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
  8. */
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12.  
  13. public class Test
  14. {
  15.     public static void Main()
  16.     {
  17.     int n = int.Parse(Console.ReadLine());
  18.         string a = Console.ReadLine();
  19.         a = a + a[0];
  20.         int x = 0, y = 0, count = 0;
  21.         HashSet<int> diagonals = new HashSet<int>();
  22.  
  23.         for (int i = 0; i < n; i++)
  24.         {
  25.             switch (a[i])
  26.             {
  27.                 case 'N': y++; if (a[i + 1] == 'E') continue; break;
  28.                 case 'S': y--; if (a[i + 1] == 'W') continue; break;
  29.                 case 'E': x++; if (a[i + 1] == 'N') continue; break;
  30.                 case 'W': x--; if (a[i + 1] == 'S') continue; break;
  31.             }
  32.             int d = y - x;
  33.             if (diagonals.Contains(d))
  34.                 diagonals.Remove(d);
  35.             else
  36.             {
  37.                 diagonals.Add(d);
  38.                 count++;
  39.             }
  40.         }
  41.     Console.WriteLine(count);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement