"Enter No of Vertices: " "Enter No of Stages: ": Int Input Int Input

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

global min,stages

n=int(input("Enter no of vertices: "))


stages=int(input("Enter no of stages : "))
stage_vertices=[0 for x in range(n)]

cost=[0 for x in range(n)]


pa=[0 for x in range(n)]
d=[0 for x in range(n)]
c=[[9999 for x in range(n)]for x in range(n)]

def Get_min(s,n):
min_vertex=0
min=9999
for i in range(n):
if(min>(c[s][i]+cost[i])):
min=c[s][i]+cost[i]
min_vertex=i
return min_vertex

def Forward(n):
global totalcost
totalcost=0

cost=[0 for i in range(n)]


for i in range(n-2,-1,-1):
r=Get_min(i,n)
cost[i]=c[i][r]+cost[r]
totalcost = cost[i]
d[i]=r
pa[0]=0
pa[stages-1]=n-1
for i in range(1,stages):
pa[i]=d[pa[i-1]]

def display( ):
print(" Shortest path is...")
ch=''
for i in range(stages-1):
ch+=str(pa[i]+1)+"--"
ch+=str(n)
print ch
print 'totalcost=', totalcost

for i in range(stages):
print('Enter no of vertices in stage ',i+1)
stage_vertices[i]=int(input(' '))

i=0
j=stage_vertices[0]
k=0
for m in range(stages-1):
for i in range(stage_vertices[m]):
for p in range(stage_vertices[m+1]):
print("Enter cost for " ,k+i+1," to ",j+(p+1),": ")
c[k+i][p+j]=input('')
if c[k+i][p+j]==0:
c[k+i][p+j]=9999
j=j+stage_vertices[m+1]
k=k+stage_vertices[m]

print c
Forward(n)
display( )

N queens

board = []
size=input("enter number of queens")
a= [[0 for x in range(size)] for y in range(size)]

def printSolution():
for i in range(size):
print a[i]
return

def danger(row, col):


for (i, j) in board:
if row == i or col == j or abs(row - i) == abs(col - j):
return True
return False

def placequeen(row):
if row > size:
print board
printSolution()
else:
for col in range(1, size + 1):
if not danger(row, col):

board.append((row, col))
a[row-1][col-1]=1
placequeen(row + 1)
board.remove((row,col))
a[row-1][col-1]=0
placequeen(1)

You might also like