Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Finding Biggest Square
//Big square problem. // input is 1D array of strings that produce a 2D aray each element of row as separated string // '#' separates the columns // final o/p printed as same way in a single string (not array) - some conditions giv in problem statement to produce format with {} and () . /* 0#1#1#1#0#1#0#1 1#0#1#0#0#0#0#1 0#0#0#1#0#1#0#0 1#1#1#1#1#0#0#1 1#1#1#1#0#1#1#1 1#1#1#1#0#1#1#1 1#1#1#1#1#1#1#1 1#1#0#1#0#0#1#1 0-white 1-black */ import java.util.*; import java.lang.*; class Rextester { public static void main(String[] args) { String input1[] = {"1#1#1#1#0#1#0#1","1#1#1#1#0#0#0#1","1#1#1#0#0#1#0#0","1#1#1#1#0#0#0#1","1#1#1#1#0#1#1#1","1#1#1#1#0#1#1#1","1#1#1#1#1#1#1#1","1#1#0#1#0#0#1#1"}; int counter=0,diagonal=0,counter1=0; int dim = input1.length; int[][] matrix = new int[dim][dim]; for(int i=0;i<dim;i++) { String[] splits = input1[i].split("#"); for(int j=0;j<splits.length;j++) { matrix[i][j]=Integer.parseInt(splits[j]); if(matrix[i][j]==1) counter=counter+1; } } int[][] count = new int[counter][3]; int x=0,y=0; for(int i=0;i<dim;i++) { for(int j=0;j<dim;j++) { boolean done = check1(i,j,matrix); counter1=0; if(done) { counter1=counter1+1; done = check1(i,j+1,matrix); if(done) { counter1=counter1+1; done = check1(i+1,j,matrix); if(done) { counter1=counter1+1; done = check1(i+1,j+1,matrix); if(done) { counter1=counter1+1; diagonal=solve(i,j,matrix,counter1); float cheker=(float)diagonal; float checker=(float)matrix.length/2; count[x][y]=i; count[x][y+1]=j; count[x][y+2]=diagonal; x++; if(cheker>=checker) { i=matrix.length; j=matrix.length;} else j++; } } } } } } int max=0,starti=0,startj=0; for(int i=0;i<count.length;i++) { for(int j=0;j<3;j++) { if(max<count[i][2]) max=count[i][2]; } } for(int i=0;i<count.length;i++) { if(max==count[i][2]) {starti=count[i][0]; startj=count[i][1]; } } String result="{"; for(int i=starti;i<starti+max;i++) { result=result+"("; for(int j=startj;j<max+startj;j++) { if(j==max+startj-1) { result=result+""+i+"#"+j;} else { result=result+""+i+"#"+j+",";} } result=result+")"; if(i!=max+starti-1) { result=result+",";} } result=result+"}"; System.out.println(result); } public static boolean check1(int row, int column,int[][] soln1) { boolean result = false; if (row >= 0 && row < soln1.length && column >= 0 && column < soln1[row].length) if (soln1[row][column] == 1) result = true; return result; } public static int solve(int row1,int column1,int[][] mat1,int diag) { int finals=0; int tempr=row1; int tempc=column1; int temp=(int)Math.sqrt(diag); int diag1=diag; boolean done=true,done1=true,done2=true; column1=column1+temp; done=check1(row1,column1,mat1); if(done) { int temp1=0,temp2=0; diag++; while(temp1<temp && done1) { row1++; temp1++; done1=check1(row1,column1,mat1); if(done1) diag++; } if(done1) { while(temp2<temp && done2) { column1--; temp2++; done2=check1(row1,column1,mat1); if(done2) diag++; } } } if(diag>diag1) { int cnt=(int)Math.sqrt(diag); if(diag==cnt*cnt) { finals=solve(tempr,tempc,mat1,diag); } else finals=temp; } else finals=temp; return finals; } }
run
|
edit
|
history
|
help
0
kkk
boolean1
1.5
Java
Words with same letters
생성자
Project Euler #2
classwork
Min Quadrato
fs