Thursday, June 18, 2015

Today we cover a few more exercises.
Depth-first-search of a binary tree:

DFS-VISIT (G,u)
{
time = time  + 1
u.d  = time
U.color = gray
For each vertex v in adj (u):
    If v.color == white
        DFS-VISIT  (G,v)
        V.pi = u
time  = time +1
u.f = time
u.color  = black
}

  • Knight’s tour on a chess board (tour all positions on the chessboard)
  • We use backtracking 
  •  Void KnightsTour(board B, int row, int col, int countOfPositions) 
  •  { 
  •      B[row,col] = countOfPositions 
  •      If (countOfpositions == row_max*col_max -1) return; 
  •      For all possible moves to a new position p 
               if B[row,col] != NIL
  •                 KnightsTour(B, p, countOfPositions + 1)
  •    } 

Four glasses are on square table corners. They have to be all up or down. The table can rotate by a random angle and a bell will ring if the glasses are all up or down.we can only feel two of the glasses.

Use diagonals and adjacent glasses alternatively in the moves
for example, if two diagonals are turned up , the table is rotated, you get back one of the diagonals for comparison with the adjacents. If the adjacent and diagonal corner in the next round are opposite, flip the down glass up. If they are same you have three glasses up.
if after two turns the bell doesn't ring flip two adjacents the other way and try again. This will put the two flipped glasses as adjacent for comparison with the others.
when we reverse the next two adjacent glasses the diagonals become opposite. The bell rings in the search finite moves.

#codingexercise 

Double  GetNthRootProductEachTermRaisedPMinusQoverPPlusQ (Double [] A,Double  p, Double q)

{


If ( A== null) return 0;


Return A.NthRootProductEachTermRaisedPMinusQoverPPlusQ(p, q);


}

No comments:

Post a Comment