import java.util.*; import java.io.*; /* * * When we place a stone, check to see if anything adjacent to it on the other team is captured. * * */ public class Go { static int NONE=-1,BLACK=0,WHITE=1; static int MAX_BOARD_SIZE=19; static int N_OFFSET = 9; Scanner in = new Scanner(System.in); int n=0; int m=0; int[] score = {0,0}; Square[][] board = new Square[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; public Go() { reset(); } public void reset() { score[BLACK] = score[WHITE] = 0; for(int i=0; i filled) { // if already filled, continue if(filled.contains(c)) return; int border = (MAX_BOARD_SIZE-n)/2; // if out of bounds, skip if(c.x < border || c.x >= MAX_BOARD_SIZE-border || c.y < border || c.y >= MAX_BOARD_SIZE-border) { return; } // if not the right tile, return if(board[c.x][c.y].stone != tile_type) { return; } // add c, then floodfill filled.add(c); floodFill(new Coords(c.x-1,c.y), tile_type, filled); floodFill(new Coords(c.x+1,c.y), tile_type, filled); floodFill(new Coords(c.x,c.y-1), tile_type, filled); floodFill(new Coords(c.x,c.y+1), tile_type, filled); } // we want to see if an area is full of inner type surrounded by outertype // so pretty much return false only if we hit the third type public boolean checkArea(Coords c, int inner_type, int outer_type, ArrayList filled) { // if already filled, continue if(filled.contains(c)) return true; int border = (MAX_BOARD_SIZE-n)/2; // if out of bounds, skip if(c.x < border || c.x >= MAX_BOARD_SIZE-border || c.y < border || c.y >= MAX_BOARD_SIZE-border) { return true; } // add c, then check others filled.add(c); // if this is a border, we're ok so far, but don't check contiguous squares if(board[c.x][c.y].stone == outer_type) { return true; } // if it's neither the inner nor outer type, return false! if(board[c.x][c.y].stone != inner_type) { return false; } if(!checkArea(new Coords(c.x-1,c.y), inner_type, outer_type, filled)) return false; if(!checkArea(new Coords(c.x+1,c.y), inner_type, outer_type, filled)) return false; if(!checkArea(new Coords(c.x,c.y-1), inner_type, outer_type, filled)) return false; if(!checkArea(new Coords(c.x,c.y+1), inner_type, outer_type, filled)) return false; return true; } public static void main(String[] args) { new Go().solve(); } public void solve() { // in = new Scanner("7 6\nB(-2,-2)\nW(2,2)\nB(-2,-3)\nW(2,3)\nB(-3,-2)\nW(3,2)\n7 6\nB(-2,-3)\nW(-3,-3)\nB(-2,-2)\nW(3,2)\nB(-3,-2)\nW(2,3)\n0 0\n"); while(true) { String[] temp = in.nextLine().split(" "); n = Integer.parseInt(temp[0]); m = Integer.parseInt(temp[1]); if(n == 0) {break;} doCase(); } } public void doCase() { // reset score reset(); // read in m moves and process each one for(int move=0; move done = new ArrayList(); int border = (MAX_BOARD_SIZE-n)/2; // System.out.println("BORDER: " + border + ", " + (MAX_BOARD_SIZE-border)); ArrayList inner_area = new ArrayList(); for(int i=border; i< MAX_BOARD_SIZE-border; i++) { for(int j=border; j< MAX_BOARD_SIZE-border; j++) { if(board[i][j].stone == NONE) continue; inner_area.clear(); int[] dx = {-1,1,0,0}; int[] dy = {0,0,-1,1}; for(int k=0; k<4; k++) { inner_area.clear(); if(checkArea(new Coords(i+dx[k],j+dy[k]),NONE,board[i][j].stone,inner_area)) { // inner_area now has a bunch of coords, many of which are the inner ones we must remove and add to score for(Coords c : inner_area) { // if it's the inner, get it and score if(board[c.x][c.y].stone == NONE && !done.contains(c)) { score[board[i][j].stone]++; done.add(c); } } } } } } System.out.println(score[BLACK] + " " + score[WHITE]); } public void processMove(String line) { int player = getPlayer(line.charAt(0)); int comma_pos = 2; while(line.charAt(comma_pos) != ',') comma_pos++; int x = Integer.parseInt(line.substring(2,comma_pos)); int y = Integer.parseInt(line.substring(comma_pos+1,line.length()-1)); // put the stone on the square for the player board[x+N_OFFSET][y+N_OFFSET].set(player); int sc = checkScore(x+N_OFFSET,y+N_OFFSET,getOpposite(player),player); // System.out.println("Giving " + player + " " + sc + " for play at " + (x+N_OFFSET) + "," + (y+N_OFFSET)); score[player] += sc; } // checks to see if outer surrounds inner; if so, clears inner to NONE and returns number of squares surrouned // else returns zero public int checkScore(int x, int y, int inner, int outer) { // try all squares around us for a score ArrayList inner_area = new ArrayList(); int answer = 0; int[] dx = {-1,1,0,0}; int[] dy = {0,0,-1,1}; for(int i=0; i<4; i++) { inner_area.clear(); if(checkArea(new Coords(x+dx[i],y+dy[i]),inner,outer,inner_area)) { // inner_area now has a bunch of coords, many of which are the inner ones we must remove and add to score for(Coords c : inner_area) { // if it's the inner, get it and score if(board[c.x][c.y].stone == inner) { answer++; board[c.x][c.y].set(NONE); } } } } return answer; } }