Problem:
Extraordinary Girl (I)

She is an extraordinary girl. She works for a library. Since she is young
and cute, she is forced to do a lot of laborious jobs. The most annoying job
for her is to put returned books into shelves, carrying them by a cart. The
cart is large enough to carry many books, but too heavy for her. Since she
is delicate, she wants to walk as short as possible when doing this job.
The library has 4N shelves (1 <= N <= 10000), three main passages, and
N + 1 sub passages. Each shelf is numbered between 1 to 4N as shown in
Figure 1. Horizontal dashed lines correspond to main passages, while vertical
dashed lines correspond to sub passages. She starts to return books from the
position with white circle in the figure and ends at the position with black
circle. For simplicity, assume she can only stop at either the middle of shelves
or the intersection of passages. At the middle of shelves, she put books with
the same ID as the shelves. For example, when she stops at the middle of
shelf 2 and 3, she can put books with ID 2 and 3.

Since she is so lazy that she doesn’t want to memorize a complicated
route, she only walks main passages in forward direction (see an arrow in
Figure 1). The walk from an intersection to its adjacent intersections takes 1
cost. It means the walk from the middle of shelves to its adjacent intersection,
and vice versa, takes 0.5 cost. You, as only a programmer amoung her friends,
are to help her compute the minimum possible cost she takes to put all books
in the shelves.

Input

The first line of input contains the number of test cases, T . Then T test
cases follow. Each test case consists of two lines. The first line contains the
number N , and the second line contains 4N characters, either Y or N . Y in
n-th position means there are some books with ID n, and N means no book
with ID n.

Output

The output should consists of T lines, each of which contains one integer,
the minimum possible cost, for each test case.

Sample Input

2
2
YNNNNYYY
4
NYNNYYNNNYNYYNNN

Output for the Sample Input

6
9

Solution in C# (judge verdict: Accepted)

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1002
{
	class Program
	{
		int m_N = 0;
		bool[] m_hasBooks = null;
		int[] m_works = new int[3];// 上段、中段、下段

		Program(int N, String chrs)
		{
			m_N = N;
			m_works[0] = 0;
			m_works[1] = int.MaxValue / 2;
			m_works[2] = int.MaxValue / 2;
			m_hasBooks = new bool[(N + 1) * 2];

			// 上段の初期化
			m_hasBooks[0] = chrs[0] == 'Y';
			for (int index = 1; index < N; ++index)
			{
				m_hasBooks[index] =
					chrs[index * 2 - 1] == 'Y' ||
					chrs[index * 2] == 'Y';
			}
			m_hasBooks[N] = chrs[2 * N - 1] == 'Y';

			// 下段の初期化
			m_hasBooks[N + 1] = chrs[2 * N] == 'Y';
			for (int index = 1; index < N; ++index)
			{
				m_hasBooks[index + N + 1] =
					chrs[2 * N + index * 2 - 1] == 'Y' ||
					chrs[2 * N + index * 2] == 'Y';
			}
			m_hasBooks[(N + 1) * 2 - 1] = chrs[4 * N - 1] == 'Y';
		}

		int Run()
		{
			for (int index = 0; index < m_N + 1; ++index)
			{
				int[] nextWorks = { int.MaxValue / 2, int.MaxValue / 2, int.MaxValue / 2};// 上段、中段、下段

				// 上段の候補について
				{
					int cost = m_works[0];
					if (m_hasBooks[index] && !m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 2);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 2);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 3);
					}
					else if (m_hasBooks[index] && m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 4);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 3);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 3);
					}
					else if (!m_hasBooks[index] && m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 4);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 3);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 3);
					}
					else
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 1);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 2);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 3);
					}
				}

				// 中段の候補について
				{
					int cost = m_works[1];
					if (m_hasBooks[index] && !m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 2);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 2);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 3);
					}
					else if (m_hasBooks[index] && m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 3);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 3);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 3);
					}
					else if (!m_hasBooks[index] && m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 3);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 2);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 2);
					}
					else
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 2);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 1);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 2);
					}
				}

				// 下段の候補について
				{
					int cost = m_works[2];
					if (m_hasBooks[index] && !m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 3);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 3);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 4);
					}
					else if (m_hasBooks[index] && m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 3);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 3);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 4);
					}
					else if (!m_hasBooks[index] && m_hasBooks[m_N + 1 + index])
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 3);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 2);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 2);
					}
					else
					{
						nextWorks[0] = Math.Min(nextWorks[0], cost + 3);
						nextWorks[1] = Math.Min(nextWorks[1], cost + 2);
						nextWorks[2] = Math.Min(nextWorks[2], cost + 1);
					}
				}

				m_works = nextWorks;
			}

			return m_works[0] - 1;
		}

		static void Main(string[] args)
		{
			TextReader input = Console.In;
			if (args.Length > 0)
			{
				input = File.OpenText(args[0]);
			}

			input.ReadLine();// 問題数は不要

			string line = null;
			while ((line = input.ReadLine()) != null)
			{
				int N = int.Parse(line);
				String chrs = input.ReadLine();
				Program p = new Program(N, chrs);
				int result = p.Run();

				Console.Out.WriteLine(result);
			}
		}
	}
}