哈哈。。魔方啊。。往1右上角走,超过最上边就跳到那列的最下边位置,超过最右边就回那一行的最左边位置,如果i能被N(这里是3)带除,那么i+1放到它的下边一行,列相同                   8 1  6 
                                     3  5  7        
                                     4  9  2     

解决方案 »

  1.   

    8 1  6 
    3  5  7        
    4  9  2
      

  2.   

    哪位仁兄能否写出算法!!
    谢谢!!
    大家一目了然!!!
      

  3.   

    先抛砖引玉一下:
    class Test{
      static int[][] a=new int[3][3];
      public static void doit() {
        a[0][1]=1;
        for(int i = 2; i <= 9; i++) {
          for(int j = 2; j <= 9; j++) {
            if(j == i) continue;
            a[0][0]=i;a[1][1]=j;
            try{
              calOther();
              if (!isOK()) continue;
              System.out.println("Result:");
              for(int ii = 0; ii < 9; ii++){
                System.out.print(a[ ii / 3][ii % 3] + " ");
                if(ii%3 == 2) System.out.println();
              }          
            }catch(Exception e){}
          }
        }
      }
      private static void calOther()throws Exception {
        a[2][2] = 15 - a[0][0] - a[1][1];
        a[0][2] = 15 - a[0][0] - 1;
        a[1][2] = 15 - a[0][2] - a[2][2];
        a[2][1] = 15 - a[1][1] - 1;
        a[2][0] = 15 - a[2][1] - a[2][2];
        a[1][0] = 15 - a[0][0] - a[2][0];
        Set s = new HashSet();
        for(int i = 0; i < 9; i++) {
          if (a[i / 3][i % 3] < 1 || a[i / 3][i % 3] > 9)
            throw new Exception("Not Good");
          s.add("" + a[i / 3][i % 3]);
        }
        if(s.size() != 9) throw new Exception("Not good");
      }
      private static boolean isOK() {
        for(int i = 0; i < 3; i++)
          if( a[i][0]+a[i][1]+a[i][2] != 15) return false;
        for(int j = 0; j < 3; j++)
           if( a[0][j]+a[1][j]+a[2][j] != 15) return false;
        if( a[0][0]+a[1][1]+a[2][2] != 15 ||
            a[2][0]+a[1][1]+a[0][2] != 15 ) return false;
       return true;
      }
    }