decvarx = [] for r1 in range(0,8): decvarx.append([]) decvarx[r1] = [] for r2 in range(0,8): decvarx[r1].append([]) decvarx[r1][r2] = [] #print decvarx def print_board(board): for i in board: print i #print_board(decvarx) for i in range(0,8): decvarx[i][i] = 0 decvarx[0][1] = decvarx[1][0] = 883 decvarx[0][2] = decvarx[2][0] = 186 decvarx[0][3] = decvarx[3][0] = 277 decvarx[0][4] = decvarx[4][0] = 401 decvarx[0][5] = decvarx[5][0] = 328 decvarx[0][6] = decvarx[6][0] = 484 decvarx[0][7] = decvarx[7][0] = 481 decvarx[1][2] = decvarx[2][1] = 1022 decvarx[1][3] = decvarx[3][1] = 869 decvarx[1][4] = decvarx[4][1] = 605 decvarx[1][5] = decvarx[5][1] = 844 decvarx[1][6] = decvarx[6][1] = 1303 decvarx[1][7] = decvarx[7][1] = 1178 decvarx[2][3] = decvarx[3][2] = 410 decvarx[2][4] = decvarx[4][2] = 519 decvarx[2][5] = decvarx[5][2] = 312 decvarx[2][6] = decvarx[6][2] = 298 decvarx[2][7] = decvarx[7][2] = 296 decvarx[3][4] = decvarx[4][3] = 442 decvarx[3][5] = decvarx[5][3] = 517 decvarx[3][6] = decvarx[6][3] = 708 decvarx[3][7] = decvarx[7][3] = 706 decvarx[4][5] = decvarx[5][4] = 245 decvarx[4][6] = decvarx[6][4] = 787 decvarx[4][7] = decvarx[7][4] = 600 decvarx[5][6] = decvarx[6][5] = 562 decvarx[5][7] = decvarx[7][5] = 365 decvarx[6][7] = decvarx[7][6] = 271 print_board(decvarx) # Input Data Above i = int(raw_input("Please Select Starting Node #0~7")) # To abandon a chosen node def abandon(row_sel): aba = decvarx for a in range(0,8): aba[row_sel][a] = 99999 aba[a][row_sel] = 99999 return aba #print_board(abandon(i)) # Return the minum # in seleced row def min_num(row_sel): selr = [] for s in range(0,8): selr.append([]) selr[s].append(decvarx[row_sel][s]) # Selected row list list #print selr selr[row_sel] = [999999] # Don't select itself #print selr m = min(selr) # [minum number] in a list m = m[0] # m is a number #print m return m #print min_num(i) # Return the Column # of minum in seleced row def min_col(row_sel): for t in range(0,8): if t != row_sel and decvarx[row_sel][t] == min_num(row_sel): return t #print min_col(i) def cal(row_sel): s = 0 while s < 8: print row_sel res = row_sel row_sel = min_col(row_sel) abandon(res) s += 1 cal(i)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question