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 JAVA (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static final int UPPER=1;
    static final int MIDDLE=2;
    static final int NEXTBOOKS_UPPER_AND_LOWER=11;
    static final int NEXTBOOKS_UPPER=12;
    static final int NEXTBOOKS_LOWER=13;
    static final int NEXTBOOKS_NONE=14;
    static char[] upperBooks;
    static char[] lowerBooks;
    static int[] nums;
    static int positon;

    public static void main(String[] args){
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int testCaseNum = Integer.parseInt(br.readLine());
            nums=new int[testCaseNum];

            for(int i = 0;i<testCaseNum;i++){
                int n = Integer.parseInt(br.readLine());
                String books=br.readLine();

                upperBooks = new char[2*n];
                lowerBooks = new char[2*n];

                for(int j =0; j<upperBooks.length;j++){
                    upperBooks[j]=books.charAt(j);
                   
                }

             

                for(int j =0; j<lowerBooks.length;j++){
                    lowerBooks[j]=books.charAt(j+(2*n));
                   
                }


                positon=UPPER;

                for(int j=0;j<=n;j++){
                    positon = calculateCost(j,n,positon,i);
                }
            }

          

           for(int i =0;i<nums.length;i++){
               System.out.println(nums[i]);
           }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    static boolean areBooksInUpperShelf(int num){
        return upperBooks[num*2]=='Y'||upperBooks[num*2-1]=='Y';
    }
    static boolean areBooksInLowerShelf(int num){
        return lowerBooks[num*2]=='Y'||lowerBooks[num*2-1]=='Y';
    }


    static int whereNextBooks(int num,int maxNum){
        if(num==maxNum){
            if(lowerBooks[num*2-1]=='Y'&&upperBooks[num*2-1]=='Y'){
                return NEXTBOOKS_UPPER_AND_LOWER;
            }else if(upperBooks[num*2-1]=='Y'){
                return NEXTBOOKS_UPPER;
            }else if(lowerBooks[num*2-1]=='Y'){
                return NEXTBOOKS_LOWER;
            }else{
                return NEXTBOOKS_NONE;
            }
        }else{
            if(areBooksInLowerShelf(num)&&areBooksInUpperShelf(num)){
                return NEXTBOOKS_UPPER_AND_LOWER;
            }else if(areBooksInUpperShelf(num)){
                return NEXTBOOKS_UPPER;
            }else if(areBooksInLowerShelf(num)){
                return NEXTBOOKS_LOWER;
            }else{
                return whereNextBooks(num+1,maxNum);
            }
        }
    }

    static int calculateCost(int num,int maxNum,int position,int testCaseNum){

        if(num==0){
            if(lowerBooks[num]=='Y'){
                nums[testCaseNum]=3;
                position=MIDDLE;
                return position;
            }else if(upperBooks[num]=='Y'){
                int nextBooksState=whereNextBooks(num+1,maxNum);

                if(nextBooksState==NEXTBOOKS_LOWER){
                    position=MIDDLE;
                }else{
                    position=UPPER;
                }

                nums[testCaseNum]=2;
                return position;
            }else{
                nums[testCaseNum]=1;
                return position;
            }

        }else if(num==maxNum){

            if(position==UPPER){
                if(lowerBooks[num*2-1]=='Y'){
                    nums[testCaseNum]+= 3;
                }else if(upperBooks[num*2-1]=='Y'){
                    nums[testCaseNum]+= 1;
                }else{
                    nums[testCaseNum]+=0;
                }
            }else{
                if(lowerBooks[num*2-1]=='Y') {
                    nums[testCaseNum]+=2;
                }else{
                    nums[testCaseNum]+=1;
                }
            }
            return 0;

        }else{
            if(position == UPPER){
                if(areBooksInLowerShelf(num)){
                    position = MIDDLE;
                    nums[testCaseNum]+= 3;

                    return position;
                }else if(areBooksInUpperShelf(num)){

                    int nextBooksState=whereNextBooks(num+1,maxNum);
                    if(nextBooksState==NEXTBOOKS_LOWER){
                        position=MIDDLE;
                    }else{
                        position=UPPER;
                    }
                    nums[testCaseNum]+=2;
                    return position;
                }else{
                    nums[testCaseNum]+=1;
                    return position;
                }
            }else{
                if(areBooksInUpperShelf(num)){

                    int nextBooksState=whereNextBooks(num+1,maxNum);
                    if(nextBooksState==NEXTBOOKS_LOWER ||nextBooksState==NEXTBOOKS_UPPER_AND_LOWER){
                        position=MIDDLE;
                    }else{
                        position=UPPER;
                    }

                    if(areBooksInLowerShelf(num)){
                        nums[testCaseNum]+=3;
                    }else{
                        nums[testCaseNum]+=2;
                    }

                    return position;


                }else if(areBooksInLowerShelf(num)){
                    nums[testCaseNum]+=2;
                    return position;
                }else{
                    nums[testCaseNum]+=1;
                    return position;
                }
            }
        }



    }
}