Skip to content

Instantly share code, notes, and snippets.

@narongdejsrn
Created July 29, 2015 21:01
Show Gist options
  • Save narongdejsrn/863094ba7c201c707035 to your computer and use it in GitHub Desktop.
Save narongdejsrn/863094ba7c201c707035 to your computer and use it in GitHub Desktop.
import java.util.*;
class MultiKey {
public int x;
public int y;
public int side; // 0 = x , 1 = y;
public int count;
public MultiKey(int x, int y, int side, int count) {
this.y = x;
this.x = y;
this.side = side;
this.count = count;
}
}
class dota {
static void printMatrix(int[][] matrix, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.print("\n");
}
}
static int checkMatrix(int[][] matrix, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 1) {
return 1;
}
}
}
return 0;
}
static MultiKey checkAndSet(MultiKey key, int y, int x, int side, int count){
if(count > key.count){
key.count = count;
key.y = y;
key.x = x;
key.side = side;
}
return key;
}
static MultiKey calXYlist(int[][] matrix, int height, int width) {
MultiKey returnKey = new MultiKey(0, 0, 0, 0);
for (int y = 0; y < height; y++) {
int curVAL = 0;
int count = 0;
int x;
for (x = 0; x < width; x++) {
int tempVAL = matrix[y][x];
if (x > 0) {
if (x == (width - 1)) {
if (tempVAL == 1 && curVAL == 1) {
checkAndSet(returnKey, y, x - count, 0, count + 1);
} else if (tempVAL == 0 && curVAL == 1) {
checkAndSet(returnKey, y, x - count, 0, count);
} else if (tempVAL == 1 && curVAL == 0) {
checkAndSet(returnKey, y, x - count, 0, 1);
}
} else {
if (tempVAL == 1 && curVAL == 1) {
count++;
} else if (tempVAL == 0 && curVAL == 1) {
checkAndSet(returnKey, y, x - count, 0, count);
count = 0;
} else if (tempVAL == 1 && curVAL == 0) {
count = 1;
}
curVAL = tempVAL;
}
} else if(width == 1 || height == 1) {
checkAndSet(returnKey, y, x, 0, tempVAL);
} else {
curVAL = tempVAL;
count = tempVAL;
}
}
}
for (int x = 0; x < width; x++) {
int curVAL = 0;
int count = 0;
int y;
for (y = 0; y < height; y++) {
int tempVAL = matrix[y][x];
if (y > 0) {
if (y == (height - 1)) {
if (tempVAL == 1 && curVAL == 1) {
checkAndSet(returnKey, y - count, x, 1, count + 1);
} else if (tempVAL == 0 && curVAL == 1) {
checkAndSet(returnKey, y - count, x, 1, count);
} else if (tempVAL == 1 && curVAL == 0) {
checkAndSet(returnKey, y - count, x, 1, 1);
}
} else {
if (tempVAL == 1 && curVAL == 1) {
count++;
} else if (tempVAL == 0 && curVAL == 1) {
checkAndSet(returnKey, y - count, x, 1, count);
count = 0;
} else if (tempVAL == 1 && curVAL == 0) {
count = 1;
}
curVAL = tempVAL;
}
} else {
curVAL = tempVAL;
count = tempVAL;
}
}
}
return returnKey;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int testCase = cin.nextInt();
cin.nextLine();
/* inside a test case */
for (int i = 0; i < testCase; i++) {
int height = cin.nextInt();
int width = cin.nextInt();
int[][] matrix = new int[height][width];
cin.nextLine();
/* add into matrix */
for (int y = 0; y < height; y++) {
String temp = cin.nextLine();
for (int x = 0; x < width; x++) {
if (temp.charAt(x) == '*') {
matrix[y][x] = 1;
} else {
matrix[y][x] = 0;
}
}
}
int totalCount = 0;
while (checkMatrix(matrix, height, width) == 1) {
MultiKey key = calXYlist(matrix, height, width);
if (key.side == 0) {
for (int x = 0; x < key.count; x++) {
if (matrix[key.y][key.x + x] == 1) {
matrix[key.y][key.x + x] = 0;
}
}
} else {
for (int y = 0; y < key.count; y++) {
if (matrix[key.y + y][key.x] == 1) {
matrix[key.y + y][key.x] = 0;
}
}
}
totalCount++;
}
System.out.println(totalCount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment