getMeOutOfTheRoom
PSG College of Technology
MurtazaA
NageswaranS
Specification
Language C++
Platform Portable
Data Structures
Stack to store the visited path
Array to store multiple inputs
Algorithm
Backtracking brute force with a brain!
Finds all paths that are unvisited previously and settles on to the minimum path
solveTheProblem()
maze()
work our way thro’ the maze to find the key
findNearestTrap()
Locate the very first ‘#’ (wall) in each of the four directions
neighbourTravel()
Get to the ‘.’ (space) within touching distance of the current position
findKey()
It checks if the key is at a walkable distance from current position
maze()
maze (testcase_no, input_graph, rows, cols, currentPosition,
keyPosition, move, visited_stack)
if currentPosition == keyPosition {
minmoves = move;
return 1
}
findNearestTrap (graph, distances, currentPosition, outPositions)
min = minimum(distances)
……. //next slide
maze()
for i=1 to 4 {
if(directions = min) continue;
else if move < minmoves {
has the currrentPosition already been visited? if so return 0
else {
returnValue= maze ( testcase_no, input_graph, rows, cols, outPositions[i],
keyPosition, move+min, visited_stack )
if returnValue != 1 {
neighbourTravel ( testcase_no, input_graph, rows, cols, outPositions[i],
keyPosition, move+min, visited_stack )
}
}}
else return 0
}
findNearestTrap()
findNearestTrap(graph, distances, currentPosition, outPositions)
//east
i = currentPosition [col] + 1
while ( graph[ currentPosition[row] ] [i]!= ‘#’ ) i++
outPosition [east][row] = currentPosition [row]
outPosition [east][col] = i-1
distances[east] = i – currentPosition [col]
//west
i = currentPosition [col] - 1
while ( graph[ currentPosition[row] ] [i]!= ‘#’ ) i--
outPosition [west][row] = currentPosition [row]
outPosition [west][col] = i + 1
distances[west] = currentPosition [col] - i
findNearestTrap()
findNearestTrap(graph, distances, currentPosition, outPositions)
//south
i = currentPosition [row] + 1
while ( graph [i] [ currentPosition[col] ] != ‘#’ ) i++
outPosition [south][row] = i - 1
outPosition [south][col] = currentPosition [col]
distances[south] = i – currentPosition [row]
//north
i = currentPosition [row] - 1
while (graph [i] [ currentPosition[col] ] != ‘#’ ) i--
outPosition [north][row] = i + 1
outPosition [north][col] = currentPosition [col]
distances[north] = currentPosition [row] - i
neighbourTravel()
neighbourTravel(neighbourTravel(testcase_no, input_graph,
rows, cols, currentPositions, keyPosition, move, visited_stack)
find if there are ‘.’ in each of the four directions
if ‘.’ exists in the east and this position is not already visited {
findNearestTrap (input_graph, distances, currentPosition, outPositions)
returnValue = findKey (input_graph, currentPosition, outPositions, visited_stack)
if returnValue != 0 and returnValue+move < minmoves {
minmove = returnValue+move
} else {
move a position towards east, that is into the white space and call
returnValue = maze()
if (returnValue == 0 and move+1 < minmoves) neighbourTravel ()
}
}

Pristine

  • 1.
    getMeOutOfTheRoom PSG College ofTechnology MurtazaA NageswaranS
  • 2.
    Specification Language C++ Platform Portable DataStructures Stack to store the visited path Array to store multiple inputs Algorithm Backtracking brute force with a brain! Finds all paths that are unvisited previously and settles on to the minimum path
  • 3.
    solveTheProblem() maze() work our waythro’ the maze to find the key findNearestTrap() Locate the very first ‘#’ (wall) in each of the four directions neighbourTravel() Get to the ‘.’ (space) within touching distance of the current position findKey() It checks if the key is at a walkable distance from current position
  • 4.
    maze() maze (testcase_no, input_graph,rows, cols, currentPosition, keyPosition, move, visited_stack) if currentPosition == keyPosition { minmoves = move; return 1 } findNearestTrap (graph, distances, currentPosition, outPositions) min = minimum(distances) ……. //next slide
  • 5.
    maze() for i=1 to4 { if(directions = min) continue; else if move < minmoves { has the currrentPosition already been visited? if so return 0 else { returnValue= maze ( testcase_no, input_graph, rows, cols, outPositions[i], keyPosition, move+min, visited_stack ) if returnValue != 1 { neighbourTravel ( testcase_no, input_graph, rows, cols, outPositions[i], keyPosition, move+min, visited_stack ) } }} else return 0 }
  • 6.
    findNearestTrap() findNearestTrap(graph, distances, currentPosition,outPositions) //east i = currentPosition [col] + 1 while ( graph[ currentPosition[row] ] [i]!= ‘#’ ) i++ outPosition [east][row] = currentPosition [row] outPosition [east][col] = i-1 distances[east] = i – currentPosition [col] //west i = currentPosition [col] - 1 while ( graph[ currentPosition[row] ] [i]!= ‘#’ ) i-- outPosition [west][row] = currentPosition [row] outPosition [west][col] = i + 1 distances[west] = currentPosition [col] - i
  • 7.
    findNearestTrap() findNearestTrap(graph, distances, currentPosition,outPositions) //south i = currentPosition [row] + 1 while ( graph [i] [ currentPosition[col] ] != ‘#’ ) i++ outPosition [south][row] = i - 1 outPosition [south][col] = currentPosition [col] distances[south] = i – currentPosition [row] //north i = currentPosition [row] - 1 while (graph [i] [ currentPosition[col] ] != ‘#’ ) i-- outPosition [north][row] = i + 1 outPosition [north][col] = currentPosition [col] distances[north] = currentPosition [row] - i
  • 8.
    neighbourTravel() neighbourTravel(neighbourTravel(testcase_no, input_graph, rows, cols,currentPositions, keyPosition, move, visited_stack) find if there are ‘.’ in each of the four directions if ‘.’ exists in the east and this position is not already visited { findNearestTrap (input_graph, distances, currentPosition, outPositions) returnValue = findKey (input_graph, currentPosition, outPositions, visited_stack) if returnValue != 0 and returnValue+move < minmoves { minmove = returnValue+move } else { move a position towards east, that is into the white space and call returnValue = maze() if (returnValue == 0 and move+1 < minmoves) neighbourTravel () } }