8 8 1
Case #1: POSSIBLE
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWWS
8 8 2
Case #2: POSSIBLE
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWSS
8 8 3
Case #3: POSSIBLE
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWEWEW
EWEWWSSS
1 1 1
Case #4: POSSIBLE
S
1 1 0
Case #5: IMPOSSIBLE
2 3 5
Case #6: IMPOSSIBLE
2 3 6
Case #7: POSSIBLE
SSS
SSS
2 3 4
Case #8: POSSIBLE
EWS
SSS
3 3 1
Case #9: POSSIBLE
EWS
EWN
EWS
3 3 2
Case #10: POSSIBLE
EWW
EWS
EWS
3 3 3
Case #11: POSSIBLE
EWS
EWS
EWS
3 3 4
Case #12: POSSIBLE
EWW
EWS
SSS
3 3 5
Case #13: POSSIBLE
EWS
EWS
SSS
3 3 6
Case #14: POSSIBLE
EWW
SSS
SSS
3 3 7
Case #15: POSSIBLE
EWS
SSS
SSS
3 3 8
Case #16: IMPOSSIBLE
3 3 9
Case #17: POSSIBLE
SSS
SSS
SSS







 import java.util.*;
    import java.io.*;
    public class GridEscape {
      public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new 
InputStreamReader(System.in)));
        int t = in.nextInt(); // Scanner has functions to read ints, longs, 
strings, chars, etc.
        for (int i = 1; i <= t; ++i) {
                determineIfCanEscapeNew(in, i);
         
          //System.out.println("Case #" + i + ": " + (n + m) + " " + (n * m));
        }
        
        in.close();
      }
      
      private static void determineIfCanEscapeNew(Scanner in, int testNum) {
          int rows = in.nextInt();
          int cols = in.nextInt();
          int kPlayersAbleToEscape = in.nextInt();
          int kPlayersUnableToEscape = rows*cols - kPlayersAbleToEscape;
          //divide the above into pairs that point towards each other.
          //if the number is odd -> if at least one such pair exists, put the 
last unable to escape player next to a captured pair and point towards that pair
          
          if(kPlayersUnableToEscape == 1) {
                  System.out.println("Case #" + testNum + ": " + "IMPOSSIBLE");
          } else {
                  System.out.println("Case #" + testNum + ": " + "POSSIBLE");
                  char grid[][] = new char[rows][cols];
                  int numPairsUnableToEscape = kPlayersUnableToEscape/2;
                  //fill the grid up with pairs.
                  boolean isOdd = kPlayersUnableToEscape % 2 == 1;
                  int numPairsLeft = fillGridWithHoriPairs(grid, 
numPairsUnableToEscape, isOdd);
                  if(numPairsLeft != 0) {
                          fillGridWithVertiPairs(grid, numPairsLeft, isOdd);
                  }
                  fillGridWithEscapes(grid);
                  
                  //print out grid
                  for(int r = 0; r < grid.length; r++) {
                          for(int c = 0; c < grid[0].length; c++) {
                                  System.out.print(grid[r][c]);
                          }
                          System.out.println();
                  }
          }
      }
      
      private static void fillGridWithEscapes(char grid[][]) {
          for(int r = 0; r < grid.length; r++) {
                  for(int c = 0; c < grid[0].length; c++) {
                          if(grid[r][c] != 'N' && grid[r][c] != 'S' && 
grid[r][c] != 'E' && grid[r][c] != 'W') {
                                  grid[r][c] = 'S';
                          }
                  }
          }
      }
      
      private static int fillGridWithHoriPairs(char grid[][], int 
numPairsUnableToEscape, boolean oddUnableToEscapes) {
          //1 <= R.
          //1 <= C.
          if(numPairsUnableToEscape == 0) {
                          return 0;
                  }
          
          for(int row = 0; row < grid.length; row++) {
                  for(int col = 0; col + 1 < grid[0].length; col += 2) {
                          grid[row][col] = 'E';
                          grid[row][col + 1] = 'W';
                          numPairsUnableToEscape--;
                          if(numPairsUnableToEscape == 0) {
                                  if(oddUnableToEscapes) {
                                          if(grid[0].length % 2 == 1) { //add 
to last col, first row
                                                  grid[0][grid[0].length - 1] = 
'W';
                                          } else if (col + 2 < grid[0].length) 
{ //try this row, very next col 
                                                  grid[row][col + 2] = 'W';
                                          }
                                          else { //else, that means the 
next/last col doesn't work for all rows. need to add in the next row.
                                                  grid[row + 1][0] = 'N';
                                          }
                                  }
                                  return 0;
                          }
                  }
          }
          
          return numPairsUnableToEscape;
      }
      
      //fill in the last col
      private static void fillGridWithVertiPairs(char grid[][], int 
numPairsUnableToEscape, boolean oddUnableToEscapes) {
          if(numPairsUnableToEscape == 0) {
                          return;
                  }
          
          for(int row = 0; row + 1< grid.length; row += 2) {
                  grid[row][grid[0].length - 1] = 'S';
                  grid[row + 1][grid[0].length - 1] = 'N';
                  numPairsUnableToEscape--;
                  if(numPairsUnableToEscape == 0) {
                                  if(oddUnableToEscapes) {
                                          grid[row+2][grid[0].length - 1] = 
'N'; //only option left is in this col. hori pairs should have filled up 
everything to the left
                                  }
                                  return;
                  }
          }
      }
      
     
          
            }

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To post to this group, send email to google-code@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/8f184114-f6d4-47a9-a260-b738b09d14c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to