Skip to content

Instantly share code, notes, and snippets.

@getsadzeg
Last active April 7, 2017 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getsadzeg/64c7ba22b399a1cc9c518aea751b7fb8 to your computer and use it in GitHub Desktop.
Save getsadzeg/64c7ba22b399a1cc9c518aea751b7fb8 to your computer and use it in GitHub Desktop.
GEOI2017, #2 ( needs to be fixed )
package main;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Grid {
static int n = 0;
static int m = 0;
static Square[][] squares;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
squares = new Square[n][m];
TreeMap whiteEmpties = new TreeMap<>();
int[][] colors = new int[n][m];
System.out.println(algorithm(whiteEmpties, colors, scanner));
}
public static int algorithm(TreeMap whiteEmpties, int[][] colors, Scanner scanner) {
String[][] newArray = new String[n][m];
int minX = 0;
int minY = 0;
int value = 1;
if (!whiteEmpties.isEmpty()) {
minX = whiteEmpties.firstKey().toString().charAt(0);
minY = whiteEmpties.firstKey().toString().charAt(2);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
colors[i][j] = scanner.nextInt();
squares[i][j] = new Square();
if (i == 0 & j == 0) {
squares[i][j].value = 1;
}
squares[i][j].color = colors[i][j];
//System.out.println("color: " + squares[i][j].color);
if (!process(i, j, minX, minY, value)) {
/* suspicious stuff here*/
if(!whiteEmpties.isEmpty()) {
whiteEmpties.remove(whiteEmpties.lastKey());
minX = i;
minY = j;
}
//System.out.println("square with coordinates " + i + " and " + j + " has value of " + value);
whiteEmpties.put(encode(i, j), squares[i][j]);
value++;
}
// if(squares[i][j].value == 3) System.out.println(i + " and " + j);
}
}
Entry<String, Square> ent = whiteEmpties.lastEntry();
return ent.getValue().value;
}
public static boolean isWhite(int x, int y) {
return squares[x][y].color == 0;
}
/*public static int[] decode(String joined) {
String[] decodedArray = joined.split("#");
int[] intArray = new int[2];
for (int i = 0; i < 2; i++) {
intArray[i] = Integer.parseInt(decodedArray[i]);
}
return intArray;
} */
public static String encode(int x, int y) {
return x + "#" + y;
}
public static String isNeighbor(int x1, int y1, int x0, int y0) {
if (x1 == x0 - 1) {
return "is up";
}
if (x1 == x0 + 1) {
return "is down";
} else if (y1 == y0 - 1) {
return "is left";
} else if (y1 == y0 + 1) {
return "is right";
}
return "";
}
public static boolean process(int i, int j, int originalX, int originalY, int value) {
if (squares[i][j].color == 0 && squares[i][j].value == 0) {
if (!isNeighbor(i, j, originalX, originalY).equals("")) {
squares[i][j].value = value;
}
if (isNeighbor(i, j, originalX, originalY).equals("is up")) {
originalX--;
}
if (isNeighbor(i, j, originalX, originalY).equals("is down")) {
originalX++;
}
if (isNeighbor(i, j, originalX, originalY).equals("is left")) {
originalY--;
}
if (isNeighbor(i, j, originalX, originalY).equals("is right")) {
originalY++;
} else {
return false;
}
}
return true;
}
static final class Square {
int value;
int color;
public Square() {
this.value = 0;
}
public Square(int value, String color) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment