Sicily 1204. Kindergarten Graduat

1204. Kindergarten Graduat

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

The WeeOnes Kindergarten has a strange ceremony as part of its graduation: The children line up with the girls on the left and the boys on the right with a single space between the boys and the girls. By making a sequence of the following four moves, the children are to end up with all the boys on the left and all the girls on the right with a single space between the boys and the girls.

 


Move Operation
Slide left (s) The child to the right of the empty space moves into the empty
  space.
Slide right (S) The child to the left of the empty space moves into the empty space.
Hop left (h) The child two spaces to the right of the open space leapfrogs over
  the intervening child to the open space.
Hop right (H) The child two spaces to the left of the open space leapfrogs over
  the intervening child to the open space.

 


In each case, the previous position of the child who moved becomes the new open space.

For example, with two girls and two boys we begin with:

 

GG_BB

the following moves give the desired result:

 

s: GGB_B
H: G_BGB
S: _GBGB
h: BG_GB
h: BGBG_
S: BGB_G
H: B_BGG
s: BB_GG

The teacher would like this process to end in a reasonable amount of time so the parents can go home (the children are probably willing to do this all day). Write a program which takes as input the numbers of girls and boys (nGirls and nBoysrespectively) and finds a sequence of at most (nGirls * nBoys + nGirls + nBoys)moves which takes you from the starting position to the ending position. [Each girl must leapfrog over (or be leapfrogged over by) each boy and, on average, each child must move past the empty space.]

Input

The input begins with the number of problems N , (1$ \le$N$ \le$1000) , on a line by itself followed by N problem instances each on its own line. A problem instance has the form:

 


probNumber nGirls nBoys

 


where

 

probNumber increases sequentially from 1 to  N .
nGirls is the number of girls.
nBoys is the number of boys.

There is at least 1 child and at most 24 children in a class.

Output

For each problem instance, output the problem number at the beginning of the line then a single space, then the number of moves on a line. On each following line, output the codes for the required moves in order. Each line except the last should have 50 move characters with the remainder, if any, on the final line. The last line of a problem instance result should be a single blank line.

Sample Input

3 
1 2 2 
2 4 0 
3 5 10

Sample Output

1 8 
sHShhSHs 

2 2 
HH 

3 65 
sHShhsHHHShhhhsHHHHHshhhhhsHHHHHshhhhhsHHHHHshhhhh 
SHHHHshhhSHHshS


Note: Other solutions are possible; for instance: 

1. ShsHHshS is also a solution to problem 1 

2. SSSS, HSS, etc. are also acceptable answers to problem 2.

// Problem#: 1204
// Submission#: 3467963
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
#include <list>
#include <set>
using namespace std;

const int MAX_POS = 27;
const int MAX_C = 24;
const int CHAR_PER_LINE = 50;

char children[MAX_POS];
int totalPos, rightGoodBoy, leftGoodGirl, nBadGirls, nBadBoys, spaceLoc;
int altLeft, altRight;
int maxMoves;

char moves[625];
int nMoves;

void initPos(int nGirls, int nBoys) {
    int i;
    children[0] = 'B';
    rightGoodBoy = 0;
    for(i = 1; i <= nGirls ; i++) {
        children[i] = 'G';
    }
    nBadGirls = nGirls;
    children[nGirls+1] = ' ';
    spaceLoc = nGirls+1;
    for(i = spaceLoc+1 ; i <= spaceLoc+nBoys ; i++) {
        children[i] = 'B';
    }
    nBadBoys = nBoys;
    children[spaceLoc + nBoys + 1] = 'G';
    leftGoodGirl = spaceLoc + nBoys + 1;
    totalPos = nGirls + nBoys + 1;
    maxMoves = nGirls * nBoys + nGirls + nBoys;
    nMoves = 0;
}

inline int SlideLeft() {
    if(spaceLoc >= totalPos) {
        return -1;
    }
    children[spaceLoc] = children[spaceLoc+1];
    spaceLoc++;
    children[spaceLoc] = ' ';
    moves[nMoves++] = 's';
    if(nMoves > maxMoves) {
        return -2;
    }
    return 0;
}

inline int SlideRight() {
    if(spaceLoc <= 1) {
        return -1;
    }
    children[spaceLoc] = children[spaceLoc-1];
    spaceLoc--;
    children[spaceLoc] = ' ';
    moves[nMoves++] = 'S';
    if(nMoves > maxMoves) {
        return -2;
    }
    return 0;
}

inline int HopLeft() {
    if(spaceLoc >= (totalPos - 1)) {
        return -1;
    }
    children[spaceLoc] = children[spaceLoc+2];
    spaceLoc += 2;
    children[spaceLoc] = ' ';
    moves[nMoves++] = 'h';
    if(nMoves > maxMoves) {
        return -2;
    }
    return 0;
}

inline int HopRight() {
    if(spaceLoc <= 2) {
        return -1;
    }
    children[spaceLoc] = children[spaceLoc-2];
    spaceLoc -= 2;
    children[spaceLoc] = ' ';
    moves[nMoves++] = 'H';
    if(nMoves > maxMoves) {
        return -2;
    }
    return 0;
}

inline int HopGirls() {
    while (spaceLoc >= (altLeft + 2)) HopRight();
    altLeft = spaceLoc + 1;
    return 0;
}

inline int HopBoys() {
    while (spaceLoc <= (altRight - 2)) HopLeft();
    altRight = spaceLoc - 1;
    return 0;
}

void output(int probNum) {
    cout << probNum << " " << nMoves << endl;
    for (int i = 0, j = 0; i < nMoves; i++, j++) {
        if (j == CHAR_PER_LINE) {
            j = 0;
            cout << endl;
        }
        cout << moves[i];
    }
    cout << endl << endl;
}

int main() {

    std::ios::sync_with_stdio(false);

    int caseNum;
    cin >> caseNum;

    for (int probNum = 1; probNum <= caseNum; probNum++) {
        int nGirls, nBoys;
        cin >> nGirls >> nGirls >> nBoys;
        initPos(nGirls, nBoys);
        if (nGirls == 0) {
            int i = 0;
            while (i < nBoys) {
                if (i < (nBoys - 1)) {
                    HopLeft();
                    i += 2;
                } else {
                    SlideLeft();
                    i++;
                }
            }
        } else if (nBoys == 0) {
            int i = 0;
            while (i < nGirls) {
                if (i < (nGirls - 1)) {
                    HopRight();
                    i += 2;
                } else {
                    SlideRight();
                    i++;
                }
            }
        } else {
            char tChild;
            SlideLeft();
            altRight = spaceLoc - 1;
            altLeft = spaceLoc - 2;
            nBadBoys--;
            nBadGirls--;
            while (altRight >= altLeft) {
                if (spaceLoc == (altRight + 1)) {
                    if (children[altRight] == 'B') {
                        tChild = children[spaceLoc + 1];
                        HopGirls();
                        if (tChild == 'B') altRight += 2;
                        else leftGoodGirl--;
                    } else {
                        if (children[spaceLoc + 1] == 'B') {
                            SlideLeft();
                            altRight++;
                        } else {
                            SlideRight();
                            altRight--;
                            leftGoodGirl--;
                        }
                    }
                } else if (spaceLoc == (altLeft - 1)) {
                    if (children[altLeft] == 'G') {
                        tChild = children[spaceLoc - 1];
                        HopBoys();
                        if (tChild == 'G') altLeft -= 2;
                        else rightGoodBoy++;
                    } else {
                        if (children[spaceLoc - 1] == 'G') {
                            SlideRight();
                            altLeft--;
                        } else {
                            SlideLeft();
                            altLeft++;
                            rightGoodBoy++;
                        }
                    }
                }
            }
        }
        output(probNum);
    }

    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值