This is an easy problem as long as you have done UVA 10196 – Check The Check. I added another “deathByKing” method and just basically used 99% of the old code to solve this problem. The time limit is 3 seconds so a lazy O(N^2) works great.
import java.awt.Point; import java.io.PrintWriter; import java.util.Scanner; import java.util.StringTokenizer; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P10284 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNext()) { char[][] grid = new char[8][8]; StringTokenizer st = new StringTokenizer(sc.next(), "/"); for (int i = 0; i < 8; i++) { char[] row = st.nextToken().trim().toCharArray(); int idx = 0; for (Character c : row) { if (Character.isDigit(c)) { int tmp = c - '0'; for (int j = idx; j < idx + tmp; j++) { grid[i][j] = '.'; } idx += tmp; } else { grid[i][idx] = c; idx++; } } } int ans = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (grid[i][j] == '.') { Point tmp = new Point(i, j); if (!deathByKing(tmp, grid)) { int count = 0; grid[i][j] = 'X'; if (!deathByBishop(tmp, grid) && !deathByKnight(tmp, grid) && !deathByPawn(tmp, grid) && !deathByRook(tmp, grid)) { count++; } grid[i][j] = 'x'; if (!deathByBishop(tmp, grid) && !deathByKnight(tmp, grid) && !deathByPawn(tmp, grid) && !deathByRook(tmp, grid)) { count++; } grid[i][j] = '.'; if (count == 2) ans++; } } } } out.println(ans); } out.close(); sc.close(); } private static boolean deathByKing(Point king, char[][] grid) { int i = king.x; int j = king.y; for (int n = i - 1; n <= i + 1; n++) for (int m = j - 1; m <= j + 1; m++) { if (n >= 0 && n < 8 && m >= 0 && m < 8 && (grid[n][m] == 'k' || grid[n][m] == 'K')) return true; } return false; } private static boolean deathByBishop(Point king, char[][] grid) { int i = king.x; int j = king.y; char thing = '\u000F'; char thing2 = '\u000F'; if (Character.isLowerCase(grid[i][j])) { // Black King thing = 'B'; thing2 = 'Q'; } else { // White King thing = 'b'; thing2 = 'q'; } // NW int idx = i - 1; int idx2 = j - 1; while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') { idx--; idx2--; } if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2)) return true; // NE idx = i - 1; idx2 = j + 1; while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') { idx--; idx2++; } if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2)) return true; // SW idx = i + 1; idx2 = j - 1; while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') { idx++; idx2--; } if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2)) return true; // RIGHT idx = i + 1; idx2 = j + 1; while (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && grid[idx][idx2] == '.') { idx++; idx2++; } if (idx >= 0 && idx < 8 && idx2 >= 0 && idx2 < 8 && (grid[idx][idx2] == thing || grid[idx][idx2] == thing2)) return true; return false; } private static boolean deathByRook(Point king, char[][] grid) { int i = king.x; int j = king.y; char thing = '\u000F'; char thing2 = '\u000F'; if (Character.isLowerCase(grid[i][j])) { // Black King thing = 'R'; thing2 = 'Q'; } else { // White King thing = 'r'; thing2 = 'q'; } // UP int idx = i - 1; while (idx >= 0 && grid[idx][j] == '.') { idx--; } if (idx >= 0 && idx < 8 && (grid[idx][j] == thing || grid[idx][j] == thing2)) return true; // DOWN idx = i + 1; while (idx < 8 && grid[idx][j] == '.') { idx++; } if (idx >= 0 && idx < 8 && (grid[idx][j] == thing || grid[idx][j] == thing2)) return true; // LEFT idx = j - 1; while (idx >= 0 && grid[i][idx] == '.') { idx--; } if (idx >= 0 && idx < 8 && (grid[i][idx] == thing || grid[i][idx] == thing2)) return true; // RIGHT idx = j + 1; while (idx < 8 && grid[i][idx] == '.') { idx++; } if (idx >= 0 && idx < 8 && (grid[i][idx] == thing || grid[i][idx] == thing2)) return true; return false; } private static boolean deathByKnight(Point king, char[][] grid) { int i = king.x; int j = king.y; char thing = '\u000F'; if (Character.isLowerCase(grid[i][j])) { // Black King thing = 'N'; } else { // White King thing = 'n'; } if (i + 2 < 8) { if (j - 1 >= 0 && grid[i + 2][j - 1] == thing) return true; if (j + 1 < 8 && grid[i + 2][j + 1] == thing) return true; } if (i - 2 >= 0) { if (j - 1 >= 0 && grid[i - 2][j - 1] == thing) return true; if (j + 1 < 8 && grid[i - 2][j + 1] == thing) return true; } if (j + 2 < 8) { if (i - 1 >= 0 && grid[i - 1][j + 2] == thing) return true; if (i + 1 < 8 && grid[i + 1][j + 2] == thing) return true; } if (j - 2 >= 0) { if (i - 1 >= 0 && grid[i - 1][j - 2] == thing) return true; if (i + 1 < 8 && grid[i + 1][j - 2] == thing) return true; } return false; } private static boolean deathByPawn(Point king, char[][] grid) { int i = king.x; int j = king.y; char thing = '\u000F'; if (Character.isLowerCase(grid[i][j])) { // Black King i++; thing = 'P'; } else { // White King i--; thing = 'p'; } if (i >= 0 && i < 8) { if (j - 1 >= 0 && grid[i][j - 1] == thing) return true; if (j + 1 < 8 && grid[i][j + 1] == thing) return true; } return false; } }