/**============================================================================= * ACSL Intermediate Division Contest#3 2007-2008 Programming Problem: Petteia * Programmed by: Anna Yu, Sir Winston Churchill High School [no documentation] * ============================================================================= */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class petteia { /** * @param args */ public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("petteia.in")); StringTokenizer st1 = null; st1 = new StringTokenizer(in.readLine()); String temp = st1.nextToken().substring(0, 1); int num = Integer.parseInt(temp); String[][] board = new String[9][9]; for (int i = 0; i < num; i++) { String posX = st1.nextToken().substring(0, 1); int x = Integer.parseInt(posX); String posY = st1.nextToken().substring(0, 1); int y = Integer.parseInt(posY); board[x][y] = "O"; } StringTokenizer st2 = null; st2 = new StringTokenizer(in.readLine()); String temp2 = st2.nextToken().substring(0, 1); int num2 = Integer.parseInt(temp2); for (int i = 0; i < num2; i++) { int x = Integer.parseInt(st2.nextToken().substring(0, 1)); int y = Integer.parseInt(st2.nextToken().substring(0, 1)); board[x][y] = "X"; } for (int i =0;i<5;i++) { StringTokenizer st = null; st = new StringTokenizer(in.readLine()); int row = Integer.parseInt(st.nextToken().substring(0,1)); int col = Integer.parseInt(st.nextToken()); System.out.println(printCapped(board, row,col)); } System.exit(0); } static boolean checkLeft(String[][] board, int row, int col) { if (col==1) return false; else if (board[row][col-1]==null)return false; else if (board[row][col - 1] == "O") return checkLeft(board, row, col - 1); else { if ((board[row][col - 1] == "X") && (board[row][col] == "O")) return true; else return false; } } static boolean checkRight(String[][] board, int row, int col) { if (col==8) return false; else if (board[row][col+1]==null)return false; else { if (board[row][col + 1] == "O") return checkRight(board, row, col + 1); else { if ((board[row][col + 1] == "X") && (board[row][col] == "O")) return true; else return false; } } } static boolean checkUp(String[][] board, int row, int col) { if (row==8) return false; else if (board[row+1][col]==null)return false; else { if (board[row + 1][col] == "O") return checkUp(board, row + 1, col); else { if ((board[row + 1][col] == "X") && (board[row][col] == "O")) return true; else return false; } } } static boolean checkDown(String[][] board, int row, int col) { if (row==1) return false; else if (board[row-1][col]==null)return false; else { if (board[row - 1][col] == "O") return checkDown(board, row - 1, col); else { if ((board[row - 1][col] == "X") && (board[row][col] == "O")) return true; else return false; } } } static String printCapped(String[][] board, int row, int col) { String output = ""; if (checkLeft(board, row, col) == true) { for (int k = col - 1; k > 0; k--) if (board[row][k] != "X") output = output + row + ", " + k + " and "; else k = 0; } if (checkRight(board, row, col) == true) { for (int k = col + 1; k <= 8; k++) if (board[row][k] != "X") output = output + row + ", " + k + " and "; else k = 9; } if (checkUp(board, row, col) == true) { for (int k = row + 1; k <= 8; k++) if (board[k][col] != "X") output = output + k + ", " + col + " and "; else k = 9; } if (checkDown(board, row, col) == true) { for (int k = row - 1; k > 0; k--) if (board[k][col] != "X") output = output + k + ", " + col + " and "; else k = 0; } if (output == "") return "NONE"; else return output.substring(0, output.length() - 5); } }