import java.io.*; import java.util.*; public class E_j { static DataInputStream in = new DataInputStream(System.in); static String line = ""; static char [] board = new char[33]; static int [] squares = new int[32]; // rMoves[i][0], rMoves[i][1] - legal move-to squares for Red from square i // rJumps[i][0], rJumps[i][1] - legal jump-to squares for Red from square i // // ditto for wMoves and wJumps // static int [][] rMoves = {{-1,-1}, { 5, 6},{ 6, 7},{ 7, 8},{ 8,-1},{-1, 9},{ 9,10},{10,11},{11,12}, {13,14},{14,15},{15,16},{16,-1},{-1,17},{17,18},{18,19},{19,20}, {21,22},{22,23},{23,24},{24,-1},{-1,25},{25,26},{26,27},{27,28}, {29,30},{30,31},{31,32},{32,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}; static int [][] wMoves = {{-1,-1}, {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1, 1},{ 1, 2},{ 2, 3},{ 3, 4}, { 5, 6},{ 6, 7},{ 7, 8},{ 8,-1},{-1, 9},{ 9,10},{10,11},{11,12}, {13,14},{14,15},{15,16},{16,-1},{-1,17},{17,18},{18,19},{19,20}, {21,22},{22,23},{23,24},{24,-1},{-1,25},{25,26},{26,27},{27,28}}; static int [][] rJumps = {{-1,-1}, {-1,10},{ 9,11},{10,12},{11,-1},{-1,14},{13,15},{14,16},{15,-1}, {-1,18},{17,19},{18,20},{19,-1},{-1,22},{21,23},{22,24},{23,-1}, {-1,26},{25,27},{26,28},{27,-1},{-1,30},{29,31},{30,32},{31,-1}, {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}; static int [][] wJumps = {{-1,-1}, {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, {-1, 2},{ 1, 3},{ 2, 4},{ 3,-1},{-1, 6},{ 5, 7},{ 6, 8},{ 7,-1}, {-1,10},{ 9,11},{10,12},{11,-1},{-1,14},{13,15},{14,16},{15,-1}, {-1,18},{17,19},{18,20},{19,-1},{-1,22},{21,23},{22,24},{23,-1}}; static void readBoard(int r, int w) // // read in read positions and white positions // { try { line = in.readLine(); } catch (IOException ioe) {} StringTokenizer str = new StringTokenizer(line); for(int i=1; i<=32; i++) board[i] = ' '; for(int i=1; i<=r; i++) { int val = Integer.parseInt(str.nextToken()); if (val > 0) board[val] = 'r'; else board[-val] = 'R'; // promoted piece } try { line = in.readLine(); } catch (IOException ioe) {} str = new StringTokenizer(line); for(int i=1; i<=w; i++) { int val = Integer.parseInt(str.nextToken()); if (val > 0) board[val] = 'w'; else board[-val] = 'W'; } } static int getMove(int [] squares) // // read next move sequence, and return number of moves // { int sq, nmove = 0; char ch; try { line = in.readLine(); } catch (IOException ioe) {} StringTokenizer str = new StringTokenizer(line,"-\n"); do { sq = Integer.parseInt(str.nextToken()); squares[nmove++] = sq; } while (str.hasMoreTokens()); return nmove-1; } static boolean incorrectPlayer(int sq, char pl) // // check is player at square is // { if (pl == 'R') return !(board[sq] == 'R' || board[sq] == 'r'); else return !(board[sq] == 'W' || board[sq] == 'w'); } static boolean notAJump(int sqs[], int nm) // // check if move sequence is a regular move, not a jump // { if (nm > 1) return false; int sq0 = sqs[0]; int sq1 = sqs[1]; return (rMoves[sq0][0] == sq1 || rMoves[sq0][1] == sq1 || wMoves[sq0][0] == sq1 || wMoves[sq0][1] == sq1); } static boolean legalMove(int sq0, int sq1, char pl) // // check if player can move from square to square // { if (board[sq1] != ' ') return false; if (pl == 'R' && board[sq0] == 'r') return (rMoves[sq0][0] == sq1 || rMoves[sq0][1] == sq1); else if (pl == 'W' && board[sq0] == 'w') return (wMoves[sq0][0] == sq1 || wMoves[sq0][1] == sq1); else // kings can move in either direction return (rMoves[sq0][0] == sq1 || rMoves[sq0][1] == sq1 || wMoves[sq0][0] == sq1 || wMoves[sq0][1] == sq1); } static boolean legalJump(int sq0, int sq1, char pl, char piece) // // check if player can jump from square to square // { if (board[sq1] != ' ') return false; if (pl == 'R' && piece == 'r') return (rJumps[sq0][0] == sq1 || rJumps[sq0][1] == sq1); else if (pl == 'W' && piece == 'w') return (wJumps[sq0][0] == sq1 || wJumps[sq0][1] == sq1); else // kings can move in either direction return (rJumps[sq0][0] == sq1 || rJumps[sq0][1] == sq1 || wJumps[sq0][0] == sq1 || wJumps[sq0][1] == sq1); } static int getInbetweenSquare(int sq1, int sq2) // // get square in between jump from to // { if (sq1 > sq2) { int temp = sq1; sq1 = sq2; sq2 = temp; } int sq = (sq1+sq2)/2; if ((sq1-1)%8 < 4) sq++; return sq; } static boolean noPiece(int sq0, int sq1, char pl) // // check if a valid piece to jump lies between square and // square // { int inb = getInbetweenSquare(sq0, sq1); if (pl == 'R') return !(board[inb] == 'w' || board[inb] == 'W'); else return !(board[inb] == 'r' || board[inb] == 'R'); } static void makeMove(int sq0, int sq1) // // modify the board to reflect a move // { board[sq1] = board[sq0]; board[sq0] = ' '; } static void makeJump(int sq0, int sq1) // // modify the board to reflect a jump // { makeMove(sq0, sq1); board[getInbetweenSquare(sq0, sq1)] = ' '; } static void convertToKings() // // promote anything on a promotion row // { for(int sq=29; sq <=32; sq++) if (board[sq] == 'r') board[sq] = 'R'; for(int sq=1; sq <=4; sq++) if (board[sq] == 'w') board[sq] = 'W'; } static boolean canJump(int sq, char pl) // // determine if player can jump from square // { if (pl == 'r' || pl == 'R') { int sq2 = rJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); try{ if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; } catch (Exception e) { System.out.println("sq2 = " + sq2 + ", inb = " + inb); System.exit(-1);} sq2 = rJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; } if (pl == 'w' || pl == 'W') { int sq2 = wJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = wJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; } if (pl == 'R') { int sq2 = rJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = rJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = wJumps[sq][0]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = wJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; } if (pl == 'W') { int sq2 = rJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = rJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = wJumps[sq][0]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = wJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; } return false; } static char toLower(char ch) // // don't know why I wrote this // { return (char)(ch - 'A' + 'a'); } static boolean jumpExists(char pl) // // check if a jump exists for player // { for(int sq=1; sq<=32; sq++) { if ((pl == board[sq] || toLower(pl) == board[sq]) && canJump(sq, board[sq])) return true; } return false; } public static void main(String [] args) { int r, w; String move; try { line = in.readLine(); } catch (IOException ioe) {} StringTokenizer str = new StringTokenizer(line); r = Integer.parseInt(str.nextToken()); while (r != 0) { w = Integer.parseInt(str.nextToken()); readBoard(r, w); try { line = in.readLine(); } catch (IOException ioe) {} str = new StringTokenizer(line); int m = Integer.parseInt(str.nextToken()); char player = str.nextToken().charAt(0); int valid = 0; int nMove; for(nMove=1; nMove<=m; nMove++) { int numMove = getMove(squares); if (incorrectPlayer(squares[0], player)) { valid = 1; // give a specific value for debugging purposes break; } if (notAJump(squares, numMove)) { if (jumpExists(player)) { valid = 2; break; } else if (!legalMove(squares[0], squares[1], player)) { valid = 3; break; } makeMove(squares[0], squares[1]); } else { char piece = board[squares[0]]; for(int i=1; i<=numMove; i++) { if (!legalJump(squares[i-1], squares[i], player, piece)) { valid = 4; break; } else if (noPiece(squares[i-1], squares[i], player)) { valid = 5; break; } makeJump(squares[i-1], squares[i]); } if (valid != 0) break; if (canJump(squares[numMove], piece)) { // check if you can still jump valid = 6; break; } } convertToKings(); player = (player == 'R') ? 'W' : 'R'; } if (valid != 0) { System.out.println("Move " + nMove + " is invalid"); nMove++; for(;nMove <= m; nMove++) { try { line = in.readLine(); } catch (IOException ioe) {} } } else System.out.println("All moves valid"); try { line = in.readLine(); } catch (IOException ioe) {} str = new StringTokenizer(line); r = Integer.parseInt(str.nextToken()); } } }