Artificial intelligence samples

N-Queens worked example: repair a row conflict

The sample report proposes a min-conflicts agent. It places one queen in each column and stores the row numbers. Each move keeps the column fixed and tries a new row for a queen under attack.

Trace one move on a four-column board

Using rows numbered 1 to 4, start with [1, 4, 1, 3]. The queens in columns 1 and 3 share row 1. The agent selects the first queen and scores each possible row by how many other queens would attack it there.

Move the first queen to row 2. The board now reads [2, 4, 1, 3]. Every row is distinct. To check the diagonals, compare row minus column and row plus column: the differences are 1, 2, −2, −1 and the sums are 3, 6, 4, 7. Neither list repeats a value. The agent can stop.

What happens on a harder board?

The report proposes counting row and diagonal conflicts, breaking ties randomly and restarting when progress stalls. Scanning all candidate rows for one move takes O(n) with those counts. The number of moves varies. An unsuccessful run cannot establish that a board has no solution.

This trace illustrates the design. The report discusses expected complexity and limitations rather than reporting measured run times.

Download the N-Queens intelligent agent report (Word)

Get programming tutoring on algorithms and debugging