DFS shortest path Prepare the Bunnies Escape Time limit exceeded

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I've exceeded the time limit for my challenge at this point, so I was curious what further optimizations I could have made to my code, as it was exceeding the execution time limit allowed.



import sys
class maze_search_with_wall_removal():
def __init__(self, input_maze):
#pre-compute the range of values in our len of x and y dimensions
self.maze = input_maze
self.x_len = len(input_maze)
self.y_len = len(input_maze[-1])
# set values to be very large for comparison's sake
self.cost = [[sys.maxsize for x in range(self.x_len)] for y in range(self.y_len)]
self.x_range = range(self.x_len)
self.y_range = range(self.y_len)

def dfs_recursive(self, x, y, current_cost=1, wall_consumed=False):
# proceed if our current state is within bounds of maze
if current_cost > self.cost[self.x_len - 1][self.y_len - 1]:
return
#check if the cost is worth considering the rest of these computations
if current_cost > self.cost[x][y]:
return
if current_cost < self.cost[x][y]:
self.cost[x][y] = current_cost

#if the position we are considering is a wall
if self.isWall(x, y):
if wall_consumed == True:
return
else:
wall_consumed = True

x_y = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for positions in x_y:
x_i, y_i = x + positions[0], y + positions[1]
if x_i not in self.x_range or y_i not in self.y_range:
continue
self.dfs_recursive(x_i, y_i, current_cost+1, wall_consumed)

def isWall(self, x, y):
if self.maze[x][y] == 1:
return True
else:
return False

def get_terminal_cost(self):
return self.cost[self.x_len - 1][self.y_len - 1]

def answer(maze):
#must be a square with a dimensional range of [2, 20] inclusive
if len(maze) in range(2, 20) and len(maze[0]) in range(2,20):
my_maze = maze_search_with_wall_removal(maze)
my_maze.dfs_recursive(0, 0)
return my_maze.get_terminal_cost()
else:
return 0


I was thinking that would have to cut the recursion out- but I don't think that would be such a huge source of inefficiency.



Likewise, I tried pre-computing whether or not the x_i and y_i values were within an acceptable range so as to avoid additional/needless function calls.







share|improve this question





















  • Why am I getting downvoted?
    – Whatamia
    May 25 at 22:05






  • 3




    I'm not the person who downvoted, but I assume it's because you provide extremely little context about the code, what it is supposed to do, how it is supposed to work, what the 'challange' was. Nobody wants to have to reverse engineer your code just to be able to start working on a review.
    – Kelson Ball
    May 25 at 22:14
















up vote
0
down vote

favorite












I've exceeded the time limit for my challenge at this point, so I was curious what further optimizations I could have made to my code, as it was exceeding the execution time limit allowed.



import sys
class maze_search_with_wall_removal():
def __init__(self, input_maze):
#pre-compute the range of values in our len of x and y dimensions
self.maze = input_maze
self.x_len = len(input_maze)
self.y_len = len(input_maze[-1])
# set values to be very large for comparison's sake
self.cost = [[sys.maxsize for x in range(self.x_len)] for y in range(self.y_len)]
self.x_range = range(self.x_len)
self.y_range = range(self.y_len)

def dfs_recursive(self, x, y, current_cost=1, wall_consumed=False):
# proceed if our current state is within bounds of maze
if current_cost > self.cost[self.x_len - 1][self.y_len - 1]:
return
#check if the cost is worth considering the rest of these computations
if current_cost > self.cost[x][y]:
return
if current_cost < self.cost[x][y]:
self.cost[x][y] = current_cost

#if the position we are considering is a wall
if self.isWall(x, y):
if wall_consumed == True:
return
else:
wall_consumed = True

x_y = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for positions in x_y:
x_i, y_i = x + positions[0], y + positions[1]
if x_i not in self.x_range or y_i not in self.y_range:
continue
self.dfs_recursive(x_i, y_i, current_cost+1, wall_consumed)

def isWall(self, x, y):
if self.maze[x][y] == 1:
return True
else:
return False

def get_terminal_cost(self):
return self.cost[self.x_len - 1][self.y_len - 1]

def answer(maze):
#must be a square with a dimensional range of [2, 20] inclusive
if len(maze) in range(2, 20) and len(maze[0]) in range(2,20):
my_maze = maze_search_with_wall_removal(maze)
my_maze.dfs_recursive(0, 0)
return my_maze.get_terminal_cost()
else:
return 0


I was thinking that would have to cut the recursion out- but I don't think that would be such a huge source of inefficiency.



Likewise, I tried pre-computing whether or not the x_i and y_i values were within an acceptable range so as to avoid additional/needless function calls.







share|improve this question





















  • Why am I getting downvoted?
    – Whatamia
    May 25 at 22:05






  • 3




    I'm not the person who downvoted, but I assume it's because you provide extremely little context about the code, what it is supposed to do, how it is supposed to work, what the 'challange' was. Nobody wants to have to reverse engineer your code just to be able to start working on a review.
    – Kelson Ball
    May 25 at 22:14












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've exceeded the time limit for my challenge at this point, so I was curious what further optimizations I could have made to my code, as it was exceeding the execution time limit allowed.



import sys
class maze_search_with_wall_removal():
def __init__(self, input_maze):
#pre-compute the range of values in our len of x and y dimensions
self.maze = input_maze
self.x_len = len(input_maze)
self.y_len = len(input_maze[-1])
# set values to be very large for comparison's sake
self.cost = [[sys.maxsize for x in range(self.x_len)] for y in range(self.y_len)]
self.x_range = range(self.x_len)
self.y_range = range(self.y_len)

def dfs_recursive(self, x, y, current_cost=1, wall_consumed=False):
# proceed if our current state is within bounds of maze
if current_cost > self.cost[self.x_len - 1][self.y_len - 1]:
return
#check if the cost is worth considering the rest of these computations
if current_cost > self.cost[x][y]:
return
if current_cost < self.cost[x][y]:
self.cost[x][y] = current_cost

#if the position we are considering is a wall
if self.isWall(x, y):
if wall_consumed == True:
return
else:
wall_consumed = True

x_y = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for positions in x_y:
x_i, y_i = x + positions[0], y + positions[1]
if x_i not in self.x_range or y_i not in self.y_range:
continue
self.dfs_recursive(x_i, y_i, current_cost+1, wall_consumed)

def isWall(self, x, y):
if self.maze[x][y] == 1:
return True
else:
return False

def get_terminal_cost(self):
return self.cost[self.x_len - 1][self.y_len - 1]

def answer(maze):
#must be a square with a dimensional range of [2, 20] inclusive
if len(maze) in range(2, 20) and len(maze[0]) in range(2,20):
my_maze = maze_search_with_wall_removal(maze)
my_maze.dfs_recursive(0, 0)
return my_maze.get_terminal_cost()
else:
return 0


I was thinking that would have to cut the recursion out- but I don't think that would be such a huge source of inefficiency.



Likewise, I tried pre-computing whether or not the x_i and y_i values were within an acceptable range so as to avoid additional/needless function calls.







share|improve this question













I've exceeded the time limit for my challenge at this point, so I was curious what further optimizations I could have made to my code, as it was exceeding the execution time limit allowed.



import sys
class maze_search_with_wall_removal():
def __init__(self, input_maze):
#pre-compute the range of values in our len of x and y dimensions
self.maze = input_maze
self.x_len = len(input_maze)
self.y_len = len(input_maze[-1])
# set values to be very large for comparison's sake
self.cost = [[sys.maxsize for x in range(self.x_len)] for y in range(self.y_len)]
self.x_range = range(self.x_len)
self.y_range = range(self.y_len)

def dfs_recursive(self, x, y, current_cost=1, wall_consumed=False):
# proceed if our current state is within bounds of maze
if current_cost > self.cost[self.x_len - 1][self.y_len - 1]:
return
#check if the cost is worth considering the rest of these computations
if current_cost > self.cost[x][y]:
return
if current_cost < self.cost[x][y]:
self.cost[x][y] = current_cost

#if the position we are considering is a wall
if self.isWall(x, y):
if wall_consumed == True:
return
else:
wall_consumed = True

x_y = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for positions in x_y:
x_i, y_i = x + positions[0], y + positions[1]
if x_i not in self.x_range or y_i not in self.y_range:
continue
self.dfs_recursive(x_i, y_i, current_cost+1, wall_consumed)

def isWall(self, x, y):
if self.maze[x][y] == 1:
return True
else:
return False

def get_terminal_cost(self):
return self.cost[self.x_len - 1][self.y_len - 1]

def answer(maze):
#must be a square with a dimensional range of [2, 20] inclusive
if len(maze) in range(2, 20) and len(maze[0]) in range(2,20):
my_maze = maze_search_with_wall_removal(maze)
my_maze.dfs_recursive(0, 0)
return my_maze.get_terminal_cost()
else:
return 0


I was thinking that would have to cut the recursion out- but I don't think that would be such a huge source of inefficiency.



Likewise, I tried pre-computing whether or not the x_i and y_i values were within an acceptable range so as to avoid additional/needless function calls.









share|improve this question












share|improve this question




share|improve this question








edited May 25 at 23:03









Sam Onela

5,77461543




5,77461543









asked May 25 at 22:01









Whatamia

6




6











  • Why am I getting downvoted?
    – Whatamia
    May 25 at 22:05






  • 3




    I'm not the person who downvoted, but I assume it's because you provide extremely little context about the code, what it is supposed to do, how it is supposed to work, what the 'challange' was. Nobody wants to have to reverse engineer your code just to be able to start working on a review.
    – Kelson Ball
    May 25 at 22:14
















  • Why am I getting downvoted?
    – Whatamia
    May 25 at 22:05






  • 3




    I'm not the person who downvoted, but I assume it's because you provide extremely little context about the code, what it is supposed to do, how it is supposed to work, what the 'challange' was. Nobody wants to have to reverse engineer your code just to be able to start working on a review.
    – Kelson Ball
    May 25 at 22:14















Why am I getting downvoted?
– Whatamia
May 25 at 22:05




Why am I getting downvoted?
– Whatamia
May 25 at 22:05




3




3




I'm not the person who downvoted, but I assume it's because you provide extremely little context about the code, what it is supposed to do, how it is supposed to work, what the 'challange' was. Nobody wants to have to reverse engineer your code just to be able to start working on a review.
– Kelson Ball
May 25 at 22:14




I'm not the person who downvoted, but I assume it's because you provide extremely little context about the code, what it is supposed to do, how it is supposed to work, what the 'challange' was. Nobody wants to have to reverse engineer your code just to be able to start working on a review.
– Kelson Ball
May 25 at 22:14















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195183%2fdfs-shortest-path-prepare-the-bunnies-escape-time-limit-exceeded%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195183%2fdfs-shortest-path-prepare-the-bunnies-escape-time-limit-exceeded%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods