Strange behavior with Python flood-fill algorithm -
i'm implementing flood-fill algorithm python minesweeper.
before show algorithm, allow me define things important:
board, when pretty printed, looks this:
x 1 1 1 3 x 2 1 2 x 2 2 3 x 2 x x 3 1 2 1 1 2 2 1 2 x 3 x 3 2 x 2 2 x x 2 3 4 x 4 x 3 x 1 x 2 1 x 2 3 3 x 2 1 1 1 3 3 2 1 x 2 4 x 4 1 1 x 2 2 3 3 2 x 4 3 1 1 x 1 1 2 3 4 x 3 1 1 1 1 2 3 2 x x 2 2 x x 1 2 3 3 2 2 x x 3 4 x 2 1 x x 2 x 3 3 2 3 3 3 2 1 2 2 x x 4 x 3 2 3 x x 2 1 1 1 3 2 2 1 x 2 x 1 2 x 3 x 4 x x 4 3 1 2 x 3 1 1 2 x 2 2 2 2 1 1 3 x 4 2 3 3 3 x x 3 2 1 1 1 1 1 x 3 1 3 x 2 2 x 2 1 x 1 1 4 x x 1 1 x 2 x 3 1 3 x 2 1 2 2 2 2 2 2 3 x 3 1 2 3 x 2 3 x 2 1 1 1 1 1 1 x 2 2 x 4 x 3 1 1 x 2 x 2 1 1 1 1 2 x 1 1 2 3 x 2 2 x x 2 1 1 1 2 2 1 2 3 x 2 1 1 1 x 2 1 1 1 2 2 2 2 2 1 x 2 1 x x 3 2 1 1 3 3 2 1 1 1 1 1 2 x x 2 x 2 1 2 2 3 x 2 1 x x 2 2 x 1 2 x 3 3 x 2 1 1 1 1 2 1 1 2 x 2 2 3 4 x 2 2 2 4 x 3 1 1 1 1 x 1 1 x 1 2 2 3 2 x 3 2 1 1 x 3 x 2 2 2 2 1 1 1 1 x 2 x 3 x 1 1 1 2 1 1 1 x 1
obviously x's represent mines, , numbers represent number of mines surrounding place. empty spaces mean there no numbers there. board how i'm storing information.
current_board, when pretty printed, looks this:
[a][b][c][d][e][f][g][h][i][j][k][l][m][n][o][p][q][r][s][t][u][v][w][x][y] [1] o o o o o o o o o o o o o o o o o o o o o o o o o [2] o o o o o o o o o o o o o o o o o o o o o o o o o [3] o o o o o o o o o o o o o o o o o o o o o o o o o [4] o o o o o o o o o o o o o o o o o o o o o o o o o [5] o o o o o o o o o o o o o o o o o o o o o o o o o [6] o o o o o o o o o o o o o o o o o o o o o o o o o [7] o o o o o o o o o o o o o o o o o o o o o o o o o [8] o o o o o o o o o o o o o o o o o o o o o o o o o [9] o o o o o o o o o o o o o o o o o o o o o o o o o [10] o o o o o o o o o o o o o o o o o o o o o o o o o [11] o o o o o o o o o o o o o o o o o o o o o o o o o [12] o o o o o o o o o o o o o o o o o o o o o o o o o [13] o o o o o o o o o o o o o o o o o o o o o o o o o [14] o o o o o o o o o o o o o o o o o o o o o o o o o [15] o o o o o o o o o o o o o o o o o o o o o o o o o [16] o o o o o o o o o o o o o o o o o o o o o o o o o [17] o o o o o o o o o o o o o o o o o o o o o o o o o [18] o o o o o o o o o o o o o o o o o o o o o o o o o
the zeros covered cells. board , current_board have same number of rows , columns.
finally,
possible_mine_numbers = ['1','2','3','4','5','6','7','8']
so, when user enters row , column value, eg 1,a, coverted python indexing, (0,0) , passed floodfill algorithm (note print statements used rough debugging).
def floodfill(current_board, row, col): print row print col if board[row][col] in possible_mine_numbers: current_board[row][col] = board[row][col] + ' ' else: if current_board[row][col] != ' ': current_board[row][col] = ' ' if row > 0: print 'a' floodfill(current_board, row - 1, col) if row < len(board[row]) - 1: print 'b' floodfill(current_board, row + 1, col) if col > 0: print 'c' floodfill(current_board, row, col - 1) if col < len(board) - 1: print 'd' floodfill(current_board, row, col + 1)
now, algorithm works fine in position other bottom row of board (row 17). when seek run on bottom row, illustration 18, e (17, 4) next output (due debugging) (look @ lastly 3):
17 4 16 4 15 4 14 4 16 4 b 15 3 c 15 5 d 17 4 b 16 3 c 16 5 d 18 4
it says 'a', 18, , 4! reason floodfill algorithm jumping 16 18 on first if branch. makes absolutely no sense me , i'm not sure why i'm getting unusual behavior. if @ rest of output, can see jumping 2 row indices @ points.
can see wrong algorithm?
you're saying if row < len(board[row])
comparing row index length of row (i.e. number of columns) not number of rows. similarly, you're comparing col
, column index, len(board)
, len(board)
certainly number of rows, not number of columns.
python algorithm list flood-fill minesweeper
No comments:
Post a Comment