0

I have stared at this assignment for far too long now and I simply don't understand what I am supposed to do exactly. We are given a 3x3 chess board and have to produce some propositonal clauses to this problem. This is what information we have been given:

Write a Python program that generates the input for a SAT solver to solve the 3-Towers problem:

a) Write a function pair2int(r,c) which maps (1,1), (1,2), ..., (3,3) to 1 to 9 using the formula 3*(r-1)+c.

b) Write nested for-loops that go through all positions on the board from (1,1) to (3,3) and produces clauses that represent attacks.

c) Write a for-loop that produces clauses that specify that all 3 rows contain a tower

We are expected to write clauses in Conjunctive normal form as far as I understand. Could also be done directly in DIMACS

So I have done the a part, but I simply don't understand how I am supposed to express attacks or even what an attack constitutes, exactly.

This is the part of the program that I have done (a):

def pair2int(r):

return [3*(p[0]-1)+p[1] for p in r] 
print(pair2int([(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)]))

which simply returns a list of positions 1-9 on a 3x3 board:

[1,2,3,4,5,6,7,8,9]

I don't understand what I am supposed to do with this. Can somebody push me in the right direction?

1 Answers1

0

The three towers (rooks) have to be put on the chessboard without attacking eachother. Towers can either move horizontally (within a row) or vertically (within a column). Therefore, towers attack eachother if they are put on the same row or on the same column.

A problem solution consists of a set of coordinates (1..3;1..3) or cell numbers (1..9) of the three towers.

A possible binary encoding for tower coordinates consists of four binary variables per tower:

row (encoded with two bits)
column (encoded with two bits)

As alternative, you could encode the cell number 1..9 per tower. This also would require four bits per tower or 12 bits in total.

The constraints to be expressed as clauses:

All towers must have valid coordinates within the chessboard
No pair of towers shares the same row (i.e. one tower per row)
No pair of towers shares the same column (i.e. one tower per column)

3 towers is a simplification of the n queens problem.

Example solution for 8 rooks (Wikipedia):

enter image description here

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • I would be happy if my answer helped you. You can confirm this by clicking on the checkmark. If you particularly liked the answer, you can click the plus sign as an upvote. – Axel Kemper Oct 25 '22 at 21:25