Skip to content

Instantly share code, notes, and snippets.

@dalcon10028
Created March 4, 2020 05:55
Show Gist options
  • Select an option

  • Save dalcon10028/106439c1d91667be9f75fda01fb94c33 to your computer and use it in GitHub Desktop.

Select an option

Save dalcon10028/106439c1d91667be9f75fda01fb94c33 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
static int[][] field;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
field = new int[n][n];
for(int i=0; i<n; i++){
String[] temp = sc.next().split("");
for(int j=0; j<n; j++)
field[i][j] = Integer.parseInt(temp[j]);
}
coleredPaper(n, 0, 0);
System.out.print(sb);
}
static void coleredPaper(int n, int x, int y){
if(isSame(n, x, y)){
sb.append(field[y][x]);
return;
}
sb.append("(");
coleredPaper(n/2, x, y);
coleredPaper(n/2, x+n/2, y);
coleredPaper(n/2, x, y+n/2);
coleredPaper(n/2, x+n/2, y+n/2);
sb.append(")");
}
static boolean isSame(int n, int x, int y){
int criteria = field[y][x];
for(int i=y; i<y+n; i++)
for(int j=x; j<x+n; j++)
if (criteria!=field[i][j]) return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment