Skip to content

Instantly share code, notes, and snippets.

@balamark
Created June 4, 2015 08:13
Show Gist options
  • Save balamark/563a6288af19aecd96ed to your computer and use it in GitHub Desktop.
Save balamark/563a6288af19aecd96ed to your computer and use it in GitHub Desktop.
SRM525 dropcoin editorial
public int getMinimum(String[] board, int K)
{
int w = board.length;
int h = board[0].length();
final int INF = 1000000000; //A large number
int res = INF;
// For each subrectangle of the board:
for (int x0 = 0; x0 < w; x0 ++) {
for (int x1 = x0+1; x1 <= w; x1++) {
for (int y0 = 0; y0 < h; y0 ++) {
for (int y1 = y0+1; y1 <= h; y1++) {
// Count the coins inside the subrectangle:
int coins = 0;
for (int i=x0; i<x1; i++) {
for (int j=y0; j<y1; j++) {
if (board[i].charAt(j)=='o') {
coins ++;
}
}
}
// If there are K coins, find the minimum number of moves to
// end with that subrectangle. Remember the minimum overall.
if (coins == K) {
int a = x0; //number of left columns to remove.
int b = w - x1; //number of right columns to remove
int c = y0; //number of top rows to remove
int d = h - y1; //number of bottom rows to remove.
res = Math.min(res, a+b+c+d + Math.min(a,b) + Math.min(c,d) );
}
}
}
}
}
return ( (res==INF) ? -1 : res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment