Magi squares, recursive

Posted by user310827 on Stack Overflow See other posts from Stack Overflow or by user310827
Published on 2010-04-07T09:41:14Z Indexed on 2010/04/07 9:43 UTC
Read the original article Hit count: 312

Filed under:

Hi, my problem is, I'm trying to permute all posibilities for 3x3 square and check if the combination is magic. I've added a tweak with (n%3==0) if statement that if the sum of numbers in row differs from 15 it breaks the creation of other two lines... but it doesn't work, any suggestions, I call the function with Permute(1).

    public static class Global {
 //int[] j = new int[6];
 public static int[] a= {0,0,0,0,0,0,0,0,0};
 public static int[] b= {0,0,0,0,0,0,0,0,0}; 
 public static int count = 0;
}


public static void Permute(int n) { 
  int tmp=n-1;
     for (int i=0;i<9;i++){
         if (Global.b[i]==0 )  { 
          Global.b[i]=1;
          Global.a[n-1]=i+1; 
             if ((n % 3) == 0) {
                 if (Global.a[0+tmp]+Global.a[1+tmp]+Global.a[2+tmp] == 15) { 
                     if (n<9) {
                         Permute(n+1); 
                     }
                     else {
                      isMagic(Global.a);
                     }
                 }
                 else
                  break;
             } 
             else {
                 Permute(n+1);
             }
             Global.b[i]=0; 
         } 
     } 
 }

© Stack Overflow or respective owner

Related posts about java