Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What is wrong with my code ?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code ?

    I was solving an ICPC 2012 question.The question says -

    "You have a wall of the cave consisting of 'lighted diamonds' arranged in a N by M grid (basically, you have a light behind each diamond which can be turned on or off). Further, you have a switch corresponding to each row of this diamond-grid. When you operate a switch, it will toggle (flip) the lights corresponding to that row.

    You are given the current configuration of the lighted diamonds. Gimli challenges Legolas to turn on as many diamonds as possible using EXACTLY K on/off operations of the switches. Since Legolas is an Elf of the Wood and doesn't care much for things that glitter, he instead asks for your help. Note that the same switch (i.e. row) can be chosen multiple times.

    Input (STDIN):

    The first line contains the number of test cases T. Each test case contains N, M and K on the first line followed by N lines containing M characters each. The ith line denotes the state of the diamonds in the ith row, where '*' denotes a diamond which is on and '.' denotes a diamond which is off.

    Output (STDOUT):

    Output T lines containing the answer for the corresponding case.
    Between successive test cases, there should not be any blank lines in the output.

    Constraints:

    1 <= T <= 100
    1 <= N,M <= 50
    1 <= K <= 100

    Sample Input:

    2
    2 2 1
    ..
    **
    2 2 2
    ..
    **

    Sample Output:
    4
    2

    Notes/Explanation of Sample Input:
    In the first test case, row 1 can be toggled hence leaving all 4 lights to be in the ON state.
    In the second test case, row 1 (or row 2) can be toggled twice, hence maintaining the state of the initial configuration. "


    I first toggle the rows which result in positive or zero(toggling a row 2 times gives 0 change) change in *'s in decreasing order. At the end, if there is 1 toggle left, toggle the row with the least absolute value amongst the rows having negative changes.

    I tested my code on various test cases but still the code is not accepted by the online judges.I need some help to figure out what is possibly going wrong with my code.

    To have more test cases please go through -
    http://ideone.com/etDkSF

    My code -

    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
                    Scanner sc = new Scanner(System.in);
                    int t = sc.nextInt();
                    for(t=t;t>0;t--){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int k = sc.nextInt();
            char [][] array= new char[n][m] ;
            int []tracker=new int[n];
            int []trackerl=new int[n];
            int i,j;
            for(i = 0; i < n; i++)
                    {
     
                              String temp = sc.next();
                              for(j = 0; j < m; j++)
                              {             
                                      array[i][j] = (temp.charAt(j));  
                              }
     
                    }
     
            int light=0,diamond=0;
     
            for(i=0;i<n;i++){
                    for(j=0;j<m;j++){
                    if(array[i][j]=='*')
                            {diamond++;}
     
                    }
                    tracker[i]=diamond;
                    diamond=0;
                    }
     
     
           int a, b;
            int temp;
            int sortTheNumbers = n;
     
            for (a = 1; a < sortTheNumbers; a++) {
                for (b = 0; b < sortTheNumbers-a; b++) {
                    if (tracker[b] > tracker[b + 1]) {
                        temp = tracker[b];
                        tracker[b] = tracker[b + 1];
                        tracker[b + 1] = temp;
                    }
                }
            }
     
            for(i=0;i<n;i++)
                    trackerl[i]=m-tracker[i];
     
     
            int br=0;
            try{
            if(m%2==0)
            {
                    for(i=0;i<=n-1;i++)
                            if(tracker[i]>(m)/2)
                        br++;  
            }
            if(m%2!=0)
            {
                    for(i=0;i<=n-1;i++)
                            if(tracker[i]>=(m+1)/2)
                        br++;
            }}catch(Exception e){}
     
            int ans=0;
            try{
            if(br>=k)
            {
                     for(i=n-1;i>(n-1-k);i--)
                            ans+=tracker[i];
     
                     for(i=(n-1-k);i>=0;i--)
                             ans+=trackerl[i];
     
            }
     
     
            if(br<k)
            {
                    if(br!=0 && br!=n)
                    {for(i=n-1;i>(n-br);i--)
                            {ans+=tracker[i];k--;}
     
                    int pass1=0,pass2=0;
                                if(k%2==0)
                                    pass1=Math.max(tracker[(n-br)]+tracker[(n-br-1)],trackerl[(n-br)]+trackerl[(n-br-1)]);
                                    if(k%2!=0)
                                pass2=Math.max(tracker[(n-br)]+trackerl[(n-br-1)],trackerl[(n-br)]+tracker[(n-br-1)]);//System.out.print("Hp"+tracker[(n-br)]);}
     
                                    ans+=Math.max(pass1, pass2);
                            for(i=(n-2-br);i>=0;i--)
                             ans+=trackerl[i];}
                    if(br!=0 && br==n)
                    {
                            for(i=n-1;i>(n-br);i--)
                            {ans+=tracker[i];k--;}
                            if(k%2!=0){ans+=tracker[(n-br)];}
                            if(k%2==0){ans+=trackerl[(n-br)];}
                            for(i=(n-1-br);i>=0;i--)
                            { ans+=trackerl[i];}
     
                    }
                    if(br==0)
                    {
                            if(k%2!=0){ans+=tracker[(n-1)];}
                            if(k%2==0){ans+=trackerl[(n-1)];}
                            for(i=(n-2);i>=0;i--)
                            { ans+=trackerl[i];}
                    }
     
            }}catch(Exception e){}
     
     
     
            System.out.println(""+ans);
     
     
            }
     
     
     
     
            }
     
    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code ?

    I was solving an ICPC 2012 question.
    The question says -
    You have a wall of the cave consisting of 'lighted diamonds' arranged in a N by M grid (basically, you have a light behind each diamond which can be turned on or off). Further, you have a switch corresponding to each row of this diamond-grid. When you operate a switch, it will toggle (flip) the lights corresponding to that row.

    You are given the current configuration of the lighted diamonds. Gimli challenges Legolas to turn on as many diamonds as possible using EXACTLY K on/off operations of the switches. Since Legolas is an Elf of the Wood and doesn't care much for things that glitter, he instead asks for your help. Note that the same switch (i.e. row) can be chosen multiple times.

    Input (STDIN):

    The first line contains the number of test cases T. Each test case contains N, M and K on the first line followed by N lines containing M characters each. The ith line denotes the state of the diamonds in the ith row, where '*' denotes a diamond which is on and '.' denotes a diamond which is off.

    Output (STDOUT):

    Output T lines containing the answer for the corresponding case.
    Between successive test cases, there should not be any blank lines in the output.

    Constraints:

    1 <= T <= 100
    1 <= N,M <= 50
    1 <= K <= 100

    Sample Input:
    2
    2 2 1
    ..
    **
    2 2 2
    ..
    **


    Sample Output:
    4
    2

    Notes/Explanation of Sample Input:
    In the first test case, row 1 can be toggled hence leaving all 4 lights to be in the ON state.
    In the second test case, row 1 (or row 2) can be toggled twice, hence maintaining the state of the initial configuration.


    For more test cases please go thorugh -
    Ideone.com | Online Java Compiler & Debugging Tool
    I first toggle the rows which result in positive or zero(toggling a row 2 times gives 0 change) change in *'s in decreasing
    order. At the end, if there is 1 toggle left, toggle the row with the least absolute value amongst the rows having negative changes.
    I tried several test cases ,but the code isn't accepted by the online judges.Please help me to find what I am doing wrong/what i am missing.

    My code -

    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
                    Scanner sc = new Scanner(System.in);
                    int t = sc.nextInt();
                    for(t=t;t>0;t--){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int k = sc.nextInt();
            char [][] array= new char[n][m] ;
            int []tracker=new int[n];
            int []trackerl=new int[n];
            int i,j;
            for(i = 0; i < n; i++)
                    {
     
                              String temp = sc.next();
                              for(j = 0; j < m; j++)
                              {             
                                      array[i][j] = (temp.charAt(j));  
                              }
     
                    }
     
            int light=0,diamond=0;
     
            for(i=0;i<n;i++){
                    for(j=0;j<m;j++){
                    if(array[i][j]=='*')
                            {diamond++;}
     
                    }
                    tracker[i]=diamond;
                    diamond=0;
                    }
     
     
           int a, b;
            int temp;
            int sortTheNumbers = n;
     
            for (a = 1; a < sortTheNumbers; a++) {
                for (b = 0; b < sortTheNumbers-a; b++) {
                    if (tracker[b] > tracker[b + 1]) {
                        temp = tracker[b];
                        tracker[b] = tracker[b + 1];
                        tracker[b + 1] = temp;
                    }
                }
            }
     
            for(i=0;i<n;i++)
                    trackerl[i]=m-tracker[i];
     
     
            int br=0;
            try{
            if(m%2==0)
            {
                    for(i=0;i<=n-1;i++)
                            if(tracker[i]>(m)/2)
                        br++;  
            }
            if(m%2!=0)
            {
                    for(i=0;i<=n-1;i++)
                            if(tracker[i]>=(m+1)/2)
                        br++;
            }}catch(Exception e){}
     
            int ans=0;
            try{
            if(br>=k)
            {
                     for(i=n-1;i>(n-1-k);i--)
                            ans+=tracker[i];
     
                     for(i=(n-1-k);i>=0;i--)
                             ans+=trackerl[i];
     
            }
     
     
            if(br<k)
            {
                    if(br!=0 && br!=n)
                    {for(i=n-1;i>(n-br);i--)
                            {ans+=tracker[i];k--;}
     
                    int pass1=0,pass2=0;
                                if(k%2==0)
                                    pass1=Math.max(tracker[(n-br)]+tracker[(n-br-1)],trackerl[(n-br)]+trackerl[(n-br-1)]);
                                    if(k%2!=0)
                                pass2=Math.max(tracker[(n-br)]+trackerl[(n-br-1)],trackerl[(n-br)]+tracker[(n-br-1)]);//System.out.print("Hp"+tracker[(n-br)]);}
     
                                    ans+=Math.max(pass1, pass2);
                            for(i=(n-2-br);i>=0;i--)
                             ans+=trackerl[i];}
                    if(br!=0 && br==n)
                    {
                            for(i=n-1;i>(n-br);i--)
                            {ans+=tracker[i];k--;}
                            if(k%2!=0){ans+=tracker[(n-br)];}
                            if(k%2==0){ans+=trackerl[(n-br)];}
                            for(i=(n-1-br);i>=0;i--)
                            { ans+=trackerl[i];}
     
                    }
                    if(br==0)
                    {
                            if(k%2!=0){ans+=tracker[(n-1)];}
                            if(k%2==0){ans+=trackerl[(n-1)];}
                            for(i=(n-2);i>=0;i--)
                            { ans+=trackerl[i];}
                    }
     
            }}catch(Exception e){}
     
     
     
            System.out.println(""+ans);
     
     
            }
     
     
     
     
            }
     
    }

Similar Threads

  1. what's wrong with my code?
    By vanitha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 1st, 2012, 07:23
  2. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 03:59
  3. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 22:13
  4. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 14:13
  5. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 16:30