[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

n-Queens Problem

  • From: "Costello, Roger L." <costello@mitre.org>
  • To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
  • Date: Sat, 1 Apr 2017 21:57:57 +0000

n-Queens Problem

Hi Folks,

 

The n-queen’s problem says that on an n x n chessboard, there cannot be queens in the same column, row, or diagonal. Here’s one solution to the n-queen’s problem for a 4 x 4 board:

 

One way to model the n-queen’s problem is to have an element for each column, whose contents is the row that its queen is located on. For example, this XML instance represents the above board:

<n-queens>
   
<column1><row>3</row></column1>
   
<column2><row>1</row></column2>
   
<column3><row>4</row></column3>
   
<column4><row>2</row></column4>
</n-queens>

 

Below is a Schematron schema that constrains XML instances to valid solutions of the n-queen’s problem. It’s a brute-force implementation. Is there is a more flexible implementation?  /Roger

<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
                     queryBinding="xslt2">
   
    
<sch:pattern id="n-Queens-Problem">


       
<sch:rule context="n-queens">
           
<sch:assert test="count(distinct-values(//row)) eq 4">
                There cannot be a queen on the same row.
           
</sch:assert>
       
</sch:rule>


       
<sch:rule context="column1">
           
<sch:assert test="
                number(../column2/row) ne (row + 1) and
                number(../column3/row) ne (row + 2) and
                number(../column4/row) ne (row + 3) and
                number(../column2/row) ne (row - 1) and
                number(../column3/row) ne (row - 2) and
                number(../column4/row) ne (row - 3)
                ">
                There cannot be a queen diagonal to the queen in column1.
           
</sch:assert>
       
</sch:rule>


       
<sch:rule context="column2">
           
<sch:assert test="
                number(../column3/row) ne (row + 1) and
                number(../column4/row) ne (row + 2) and
                number(../column3/row) ne (row - 1) and
                number(../column4/row) ne (row - 2)
                ">
                There cannot be a queen diagonal to the queen in column2.
           
</sch:assert>
       
</sch:rule>


       
<sch:rule context="column3">
           
<sch:assert test="
                number(../column4/row) ne (row + 1) and
                number(../column4/row) ne (row - 1)
                ">
                There cannot be a queen diagonal to the queen in column3.
           
</sch:assert>
       
</sch:rule>


   
</sch:pattern>
   
</sch:schema>

 



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.