// by Dan Joyce
// solves Problem 3: Hi-Q 
// of 1996 Mid Atlantic programming contest

// input describes Hi-Q puzzle boards 
// output describes result of playing puzzle 

import java.io.*;
import common.doinput; 

class problem3 {

  static int first(int n)   {
  // returns first coordinate of the board location N
  if ((n > 0) && (n <4)) return 2;
  if ((n > 3) && (n <7)) return 3;
  if ((n > 6) && (n <14)) return 4;
  if ((n > 13) && (n <21)) return 5;
  if ((n > 20) && (n <28)) return 6;
  if ((n > 27) && (n <31)) return 7;
  if ((n > 30) && (n <34)) return 8;
  return 0;
  };

  static int second(int n)  {
  // returns second coordinate of the board location N
  switch (n) {
    case  1: return  4;   
    case  2: return  5; 
    case  3: return  6;  
    case  4: return  4;  
    case  5: return  5;  
    case  6: return  6;  
    case  7: return  2;  
    case  8: return  3;
    case  9: return  4;   
    case 10: return  5;   
    case 11: return  6;   
    case 12: return  7;   
    case 13: return  8;   
    case 14: return  2;   
    case 15: return  3;   
    case 16: return  4;   
    case 17: return  5;   
    case 18: return  6;   
    case 19: return  7;   
    case 20: return  8;   
    case 21: return  2;   
    case 22: return  3;   
    case 23: return  4;   
    case 24: return  5;   
    case 25: return  6;   
    case 26: return  7;   
    case 27: return  8;   
    case 28: return  4;   
    case 29: return  5;   
    case 30: return  6;   
    case 31: return  4;   
    case 32: return  5;   
    case 33: return  6;   
    };
  return 0;
  };

  public static void main (String [] args) throws IOException {

    int count;                           // number of puzzles
    int puzzle [][] = new int [11][11];  // holds one puzzle
                                         // 0 = unavailable, 1 = hole, 2 = peg
    int pos, testpos;                    // puzzle position 1 .. 33
    int first, second;                   // puzzle coordinates
    boolean mademove;
    int sum;

    doinput myinput = new doinput();

    System.out.println ("HI Q OUTPUT");

    count = myinput.nextint();

    for (int i = 1; i <= count; i++) 
      {
      // initialize board
      for (int j = 0; j <= 10; j++)    // first set all to holes
        for (int k = 0; k <= 10; k++)
          puzzle[j][k] = 1;
      for (int j = 0; j <= 10; j++)    // next make border unavailable
        {puzzle[0][j] = 0;
         puzzle[1][j] = 0;
         puzzle[9][j] = 0;
         puzzle[10][j] = 0;
         puzzle[j][0] = 0;
         puzzle[j][1] = 0;
         puzzle[j][9] = 0;
         puzzle[j][10] = 0;
        };
      for (int j = 2; j <= 3; j++)    // do rest of the unavailable
        {puzzle[j][2] = 0;
         puzzle[j][3] = 0;
         puzzle[j][7] = 0;
         puzzle[j][8] = 0;
        };
      for (int j = 7; j <= 8; j++)
        {puzzle[j][2] = 0;
         puzzle[j][3] = 0;
         puzzle[j][7] = 0;
         puzzle[j][8] = 0;
        };
      pos = myinput.nextint();       // read and assign pegs to holes
      while (pos != 0)
        {first = first(pos);
         second = second(pos);
         puzzle[first][second] = 2;
         pos = myinput.nextint();
        };

      // solve puzzle
      mademove = true;
      while (mademove) 
        {mademove = false;
         testpos = 33;
         while ((testpos != 0) && !mademove)
           {first = first(testpos);
            second = second(testpos);
            if (puzzle[first][second] == 1)
              {if ((puzzle[first+1][second] ==  2) 
                    && (puzzle[first+2][second] == 2))
                   {puzzle[first+1][second] = 1;
                    puzzle[first+2][second] = 1;
                    puzzle[first][second] = 2;
                    mademove = true;}
               else
               if ((puzzle[first][second+1] ==  2) 
                    && (puzzle[first][second+2] == 2))
                   {puzzle[first][second+1] = 1;
                    puzzle[first][second+2] = 1;
                    puzzle[first][second] = 2;
                    mademove = true;}
               else              
               if ((puzzle[first][second-1] ==  2) 
                    && (puzzle[first][second-2] == 2))
                   {puzzle[first][second-1] = 1;
                    puzzle[first][second-2] = 1;
                    puzzle[first][second] = 2;
                    mademove = true;}
               else              
               if ((puzzle[first-1][second] ==  2) 
                    && (puzzle[first-2][second] == 2))
                   {puzzle[first-1][second] = 1;
                    puzzle[first-2][second] = 1;
                    puzzle[first][second] = 2;
                    mademove = true;}
              };
            testpos = testpos - 1;
            };  // while  testpos != 0 ...
         }; // while mademove

      // get and print final count
      sum = 0;
      for (int j = 33; j > 0; j = j -1)
        {first = first(j);
         second = second(j);
         if (puzzle[first][second] == 2)
            sum = sum + j;
        };
      System.out.println(sum);

      }; // end for count

    System.out.println ("END OF OUTPUT");
   }
}

