示例#1
0
文件: Sudoku.cs 项目: nofear/Mara
        public Sudoku()
            : base(1, 9)
        {
            int size	= 9;

            m_Matrix	= new IntVarMatrix( m_Solver, size, size, new IntInterval( 1, size ) );

            for( int idx = 0; idx < size; ++idx )
            {
                IntVarListAllDifferent constraint	= m_Matrix.Row( idx ).AllDifferent();
                constraint.Level	= PropagateLevel.High;

                m_Solver.Add( constraint );
            }

            for( int idx = 0; idx < size; ++idx )
            {
                IntVarListAllDifferent constraint	= m_Matrix.Col( idx ).AllDifferent();
                constraint.Level	= PropagateLevel.High;

                m_Solver.Add( constraint );
            }

            for( int subRow = 0; subRow < 3; ++subRow )
            {
                for( int subCol = 0; subCol < 3; ++subCol )
                {
                    IntVarListAllDifferent constraint	= m_Matrix.Matrix( subRow * 3, subCol * 3, 3, 3 ).VarList.AllDifferent();
                    constraint.Level	= PropagateLevel.High;

                    m_Solver.Add( constraint );
                }
            }
        }
示例#2
0
        public MagicSquare( int count )
            : base(0, 10000)
        {
            m_Matrix			= new IntVarMatrix( m_Solver, count, count, new IntInterval( 1, count * count ) );
            m_MagicConstant		= ( count * ( count * count + 1 ) ) / 2;

            for( int idx = 0; idx < count; ++idx )
            {
                m_Solver.Add( m_Matrix.Row( idx ).Sum( m_MagicConstant ) );
                m_Solver.Add( m_Matrix.Col( idx ).Sum( m_MagicConstant ) );
            }

            bool mm		= false;
            if( mm )
            {
                if( count % 2 == 0 )
                {
                    int magicConstant2	= m_MagicConstant / 2;

                    for( int idx = 0; idx < count; ++idx )
                    {
                        m_Solver.Add( m_Matrix.Row( idx, 0, count / 2 ).Sum( magicConstant2 ) );
                        m_Solver.Add( m_Matrix.Row( idx, count / 2, count ).Sum( magicConstant2 ) );
                        m_Solver.Add( m_Matrix.Col( idx, 0, count / 2 ).Sum( magicConstant2 ) );
                        m_Solver.Add( m_Matrix.Col( idx, count / 2, count ).Sum( magicConstant2 ) );
                    }
                }
            }

            m_Solver.Add( m_Matrix.DiagLeftTopToBottomRight().Sum( m_MagicConstant ) );
            m_Solver.Add( m_Matrix.DiagRightTopToBottomLeft().Sum( m_MagicConstant ) );

            IntVarListAllDifferent ad	= m_Matrix.VarList.AllDifferent();
            m_Solver.Add( ad );

            // remove symmetry
            m_Solver.Add( m_Matrix[ 0, 0 ] < m_Matrix[ 0, count - 1 ] );
            m_Solver.Add( m_Matrix[ 0, 0 ] < m_Matrix[ count - 1, count - 1 ] );
            m_Solver.Add( m_Matrix[ 0, 0 ] < m_Matrix[ count - 1, 0 ] );
            m_Solver.Add( m_Matrix[ 0, count - 1 ] < m_Matrix[ count - 1, 0 ] );
        }