#include <stdio.h>
#include <malloc.h>

// input format:
// length, H or V, row, col

#define MAXCARS 13

int ncars;
int length[MAXCARS];
char hv[MAXCARS];
int row[MAXCARS], col[MAXCARS];

int nstates;
char *move;			// code for the move to get here
char *oldmoves;			// old state of this

void getinput() {
  ncars = 0;
  for(;;){
    scanf("%d", &length[ncars]);
    if (length[ncars] == 0) return;
    scanf(" %c%d%d",&hv[ncars],&row[ncars],&col[ncars]);
    //printf("Got %d %c %d %d\n",length[ncars],hv[ncars],row[ncars],col[ncars]);
    ncars++;
  }
}

void alloc_move() {
  int i,n=1;
  for(i=0;i<ncars;i++){
    n *= (7 - length[i]); // # positions of vehicle i
  }
  move = (char*)malloc(n);
  oldmoves = (char*)malloc(n);
  if (!move || !oldmoves) {
    printf("Not enough memory.\n");
    exit(1);
  }
  for(i=0;i<n;i++){
    move[i] = -1;		// special code means "not visited"
  }
  printf("There are %d states in my search space.\n",n);
  nstates = n;
}

int index() {
  // return index of current board position
  int ind = 0;
  for(int i=0;i<ncars;i++){
    if (hv[i] == 'H') {
      ind += col[i];
    } else {
      ind += row[i];
    }
    if (i<ncars-1) {
      ind *= (7 - length[i+1]);	// shift index over w enough room for next datum
    }
  }
  return ind;
}

void parse_index(int i) {
  // convert an index back to a board state
  for(int v=ncars-1; v>=0; v--) {
    int p = i%(7 - length[v]);
    i = i/(7 - length[v]);
    if (hv[v] == 'H') {
      col[v] = p;
    } else {
      row[v] = p;
    }
  }
}

int nvisited=0;

void set_move(int d) {
  //printf("Pos %d -> move %d.\n",index(),d);
  move[index()] = d;
  nvisited++;
}

int get_move() {
  return move[index()];
}

int winner () {
  return (col[0] + length[0] >= 6);
}

// does the current position involve a collision?
int collide() {
  char occ[6][6];
  for(int r=0;r<6;r++) for(int c=0;c<6;c++) occ[r][c] = 0;
  for(int v=0;v<ncars;v++){
    int y = row[v], x = col[v];
    if (x<0 || y<0) {
      //printf("Too far up or left!\n");
      return 1;
    }
    for(int i=0;i<length[v];i++){
      if (x>5 || y>5) {
	//printf("Too far down or right!\n");
	return 1;
      }
      if (occ[y][x]) return 1;
      occ[y][x] = 1;
      if (hv[v] == 'H') {
	x++;
      } else {
	y++;
      }
    }
  }
  return 0;
}

void undo_move(int m){
  int v = m>>1;
  int d = (m&1)?1:-1;
  if (hv[v] == 'H') col[v]+=d; else row[v]+=d;
}

int nmoves = 0;

void print_position() {
  printf("\tPosition ID %d:\n",index());
  for(int i=0;i<ncars;i++){
    printf("\tCar %d (%d %c) at row %d, col %d.\n",
	   i, length[i], hv[i], row[i], col[i]);
  }
}

void print_path() {
  //printf("OK, at the moment I'm at position:\n");
  //print_position();
  int m = get_move();
  //printf("To which I got using move (%d, %d).\n", m>>1, m&1);
  if (m == -2) {
    printf("There are %d moves:\n",nmoves);
    return;
  }
  undo_move(m);
  nmoves++;
  print_path();
  printf("%d %c\n", m>>1, (m&1)?'B':'F');
}

void trymove(int movecode) {
  //printf("Trying move (%d, %d) to pos %d - ", movecode>>1, movecode&1, index());
  if (collide()) {
    //printf(" Bang!\n");
    return;
  }
  if (get_move() != -1) {
    //printf(" Been there, done that.\n");
    return;
  }
  //printf(" Good one. ");
  set_move(movecode);
  if (winner()) {
    //printf("We have a winnah!\n");
    printf("I visited %d states total.\n",nvisited);
    print_path();
    exit(0);
  }
}

main() {
  getinput();
  alloc_move();
  set_move(-2);			// code meaning "initial"
  // repeat following until done
  for(int mv=1;mv<100;mv++){
    printf("Searching move %d...\n",mv);
    memcpy(oldmoves,move,nstates);
    for(int s=0;s<nstates;s++){
      if (oldmoves[s] != -1) {
	// generate all moves from this state
	parse_index(s);
	//printf("Considering moves from position %d:\n",s);
	//print_position();
	for(int v=0;v<ncars;v++){
	  // first, trymove forward.
	  if (hv[v] == 'H') col[v]++; else row[v]++;
	  trymove(v<<1);
	  // now trymove back.
	  if (hv[v] == 'H') col[v]-=2; else row[v]-=2;
	  trymove((v<<1)+1);
	  if (hv[v] == 'H') col[v]++; else row[v]++;
	}
      } else {
	//printf("Ignoring state %d which is marked as %d.\n",s,oldmoves[s]);
      }
    }
  }
  printf("Search terminated\n");
}

