2016-03-24 03:51:29

Hello,

I am trying to see if there is a column conflict given a board of queens and a given row and column. Aside from creating the function that returns a list of tuples of coordinates, I am stuck as to what to do next, which is quite laughable, as finding a row and column conflict were easy enough and my geometry skills are illuding me now.

2016-03-24 05:26:41

Could you be more specific? Do you mean a board of queens as in a Chess Board? What constitutes a conflict? Is it to determine if an object is along a given path? Or to see if two objects intersect?

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2016-03-25 05:26:24

The object is to see if a queen occupies a diagonal that is in line with the specified (row, column) parameter. The queens are booleans, True means there is a queen, False means there is not a queen.

2016-03-25 07:10:47

I think this might work:

#build 8x8 chess board grid
board = []
for a in xrange(0,8,1):
    board.append([])
    for b in xrange(0,8,1):
        board[a].append(0)

#set position of a queen
board[1][3] = 1

#diagonal queen finder
def find(row,column):
#list for storing coords of found queens
    queen = []

#search lower left
    x = row
    y = column
    while (x > 0) and (y > 0):
        x -= 1
        y -= 1
        if board[y][x] == 1:
            queen.append([x,y])
            print 'Queen Found'

#search lower right
    x = row
    y = column
    while (x < 7) and (y > 0):
        x += 1
        y -= 1
        if board[y][x] == 1:
            queen.append([x,y])
            print 'Queen Found'

#search upper left
    x = row
    y = column
    while (x > 0) and (y < 7):
        x -= 1
        y += 1
        if board[y][x] == 1:
            queen.append([x,y])
            print 'Queen Found'

#search upper right
    x = row
    y = column
    while (x < 7) and (y < 7):
        x += 1
        y += 1
        if board[y][x] == 1:
            queen.append([x,y])
            print 'Queen Found'

    return queen

find(6,4)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2016-03-25 07:31:26

Hmm. I think I confuse you a bit, my apoligies.
The board is of arbritrary length, and we want a location of queens relative to the given location.
diagonal_conflict([[True, True], [False, False]], 0, 1)
My coords function would take this input and say that queens are at [(0, 0) (0, 1)] I am not sure of the requisite algebra to do next.

2016-03-25 08:41:41

Hm, so am I to assume that [(0,0),(0,1)] is a set of coordinates for 0,0, and 0,1, on the grid? Are you looking for the distance between the given position and queens? Given your example, what would the output your looking for look like?

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2016-03-26 04:05:19 (edited by stewie 2016-03-26 04:13:36)

Just find the coordinates of the point and the queen. Find the difference in coordinates (say, dy for p2.y-p1.y) and the same for dx.

Now you're subtracting one set from another, so you're essentially finding the location of the second queen compared to the first. If its a diagonal, the x and y distances have to be the same. Therefore, check the absolute values on dy and dx are equal.

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2016-03-26 04:46:48

I wonder if I can take your implementation of the eight queens and adjust it to fit an arbitrary m by n board?
It looks like we're doing length(board)-1 for diagonal cases. Is this just an observation for this particular case, or would this actually work? Furthermore, how can you iterate over a tuple using a while loop?

2016-03-26 05:28:04

Eurika! Thanks much, @magurp244. I now have a working solution.

2016-03-26 05:37:39 (edited by magurp244 2016-03-26 05:37:57)

No problem, and as you'd guessed changing lines like "while (x > 0) and (y < 7):" to "while (x > 0) and (y < len(board)-1):" would work fine for arbitrary grid sizes.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer