Sunday, June 26, 2011

ZOlO, intresting java problem with the solution !

Hi yaaa / this is an problem that i solved during the Data Structure course , 
was an assignment out of 20 , but it was interesting to solve .. ,


،,

A new race was identified to live on ZOLO, a newly discovered planet. The DNA of these people is much simpler than that of human beings. Each trace is coded by a square of size n x n where n is an even number and 2  n  50. Each cell of the square is either 0 (OFF) or 1 (ON). Scientists discovered that the  relationship between two persons is based on the similarity between the cells of the person with the smaller square trace and the cells of an equal size square centered in the middle of the trace of the person with larger square trace. (the two traces can be of equal size). The relationship is based on the following findings:


• If the number of ones of the corresponding rows in the compared traces are equal (e.g. row#0 of first trace with row#0 of the second trace, row#1 of the first trace with row#1 of the second trace, and so on), then they are related from their mothers’ sides.

• If the number of ones of the corresponding columns in the compared traces are equal (e.g. column#0 of first trace with column#0 of the second trace, column#1 of the first trace with column#1 of the second trace, and so on), then they are related from their fathers’ sides.

• If the number of ones of the corresponding rows and the corresponding columns in the compared traces are equal, then they are related from both their mothers sides and fathers sides.


For example, suppose, two persons X and Y have the following traces, respectively:


0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 0 1 1
1 1 1 1 1 1

1 1 0 1
1 1 1 1
1 0 0 0
0 1 0 0

We say X and Y are related from their fathers’ sides since the number of ones of the corresponding columns are equal.


Input

 
The input data is stored in file “digital.in”. The first line of the input file contains a single integer t (1  t  10), the number of test cases, subsequent lines correspond to the t test cases.
For each test case, the first line contains the number of rows in the small trace person. The subsequent line(s) contains data to fill the cells of that person row by row. Then, a line that contains the number of rows of the larger or equal size trace person. The subsequent line(s) contains data to fill the cells of the person with larger or equal size trace row by row.


Output
The output should be stored in file “digital.out”. The output should contain one line for each test case (as shown in the sample output below) indicating the type of relationship between the two persons as follows: Mothers’ sides, Father’s sides, Both sides, or Not related

Sample Input


The following input file contains 2 test cases. The first test case corresponds to 4x4 and 6x6 traces of two persons, and the second test case corresponds to 2x2 and 4x4 traces of another couple of persons.

2

4
0 0 0 0
1 1 1 1
1 1 0 0
1 1 1 1
6
0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
2
1 1
0 1
4
1 1 1 1
1 1 1 1
1 0 0 0
0 0 0 1

Sample Output


Case 1: Both sides
Case 2: Not related





،, 
there is another solution i will post it soon. 
if you have any question be free to ask .. ! 
ps : i love programming ! °\(^▿^)/°
،, 
------------------------------------------------------------------------------------------[CODE ] !
public class Main { public static void main(String[] args) { Scanner sc =null; PrintStream out = null; try{ sc= new Scanner (new File("digital.in")); out = new PrintStream(new File("digital.out")); int cases , count =1 ; cases= sc.nextInt(); while(cases>= count){ out.print("Case "+count); int size1 = sc.nextInt(); int A[][] = new int [size1][size1]; for(int i=0 ; i<size1 ; i++){ // reading 1st metrix for(int j=0 ; j<size1 ; j++){ A[i][j]= sc.nextInt(); } } int size2 = sc.nextInt(); int B[][] = new int [size2][size2]; for(int i=0 ; i<size2 ; i++){ // reading 2nd metrix for(int j=0 ; j<size2 ; j++){ B[i][j]= sc.nextInt(); } } if(MotherSide(A,B) == true && FatherSide(A,B)==true) out.print(": Both sides"); else if(MotherSide(A, B) == true) out.print(": Mother sides"); else if(FatherSide(A, B) == true) out.print(": Father sides"); else out.print(": Not related"); count++; out.println(); } }catch(Exception e){ System.out.println(" Exception : "+ e); } sc.close(); out.close(); } public static boolean MotherSide(int Small[][], int Big[][]){ boolean found = true; int ones[] = new int [Small.length]; for(int i=0 ; i<Small.length ; i++ ){ int onesCount = 0 ; for(int j =0 ; j<Small.length ; j++){ if (Small[i][j]==1) onesCount = onesCount +1; } ones[i]= onesCount; } int i=0; while(i<Small.length && found == true ){ int onesCount = 0 ; for(int j =0 ; j<Small.length ; j++){ if (Big[i+(Big.length - Small.length)/2][j+(Big.length - Small.length)/2]==1) onesCount = onesCount +1; } if(ones[i]== onesCount) i++; else found = false; } return found; } public static boolean FatherSide(int Small[][], int Big[][]){ boolean found = true; int ones[] = new int [Small.length]; for(int i =0 ; i<Small.length ; i++ ){ int onesCount = 0 ; for(int j =0 ; j<Small.length ; j++){ if (Small[j][i]==1) onesCount = onesCount +1; } ones[i]= onesCount; } int i=0; while(i<Small.length && found == true ){ int onesCount = 0 ; for(int j =0 ; j<Small.length ; j++){ if (Big[j+(Big.length - Small.length)/2][i+(Big.length - Small.length)/2]==1) onesCount = onesCount +1; } if(ones[i]== onesCount) i++; else found = false; } return found; } }


1 comment:

Unknown said...

Helllo i've found your love to programming is interesting
I too like to program but im facing this problem jn object oriented lab course
And i couldn't think of a good way to solve
Do you care to see ours?
Its the same problem but there is no need to do mother and father side just show related or not by the number of ones