一般使用队列,经典题型:
class Solution:
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 0:
return 0
step = 1
queue = [0]
visited = [False for _ in range(n+1)]
visited[0] = True
while queue != []:
for i in range(len(queue)):
cur = queue.pop(0)
for j in range(1, n+1):
temp = cur + j*j
if temp > n:
break
if temp == n:
return step
if not visited[temp]:
visited[temp] = True
queue.append(temp)
step += 1
return step
给定起点和终点,迷宫中有障碍,每次可以走到不可以走为止,问是否可以走到终点。
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up
, down
, left
or right
, but it won't stop rolling until hitting a wall
. When the ball stops, it could choose the next direction.
Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
Given:
a maze represented by a 2D array
0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
start coordinate (rowStart, colStart) = (0, 4)
destination coordinate (rowDest, colDest) = (4, 4)
Return:true
class Solution:
"""
@param maze: the maze
@param start: the start
@param destination: the destination
@return: whether the ball could stop at the destination
"""
def hasPath(self, maze, start, destination):
if len(maze) == 0 or len(start) == 0 or len(destination) == 0:
return False
m = len(maze)
n = len(maze[0])
queue = [start]
visited = [[False for _ in range(n)] for _ in range(m)]
while queue != []:
p = queue.pop(0)
if p[0] == destination[0] and p[1] == destination[1]:
return True
#先向上已知走到不能走为止
x = p[0]
y = p[1]
while x - 1 >= 0 and maze[x-1][y] == 0:
x -= 1
if not visited[x][y]:
queue.append([x,y])
visited[x][y] = True
#向下走到不能走为止
x = p[0]
y = p[1]
while x + 1 < m and maze[x+1][y] == 0:
x += 1
if not visited[x][y]:
queue.append([x,y])
visited[x][y] = True
#向左走
x = p[0]
y = p[1]
while y - 1 >= 0 and maze[x][y-1] == 0:
y -= 1
if not visited[x][y]:
queue.append([x,y])
visited[x][y] = True
#向右走
x = p[0]
y = p[1]
while y + 1 < n and maze[x][y+1] == 0:
y += 1
if not visited[x][y]:
queue.append([x,y])
visited[x][y] = True
return False
class Solution:
"""
@param maze: the maze
@param start: the start
@param destination: the destination
@return: whether the ball could stop at the destination
"""
def hasPath(self, maze, start, destination):
if len(maze) == 0 or len(start) == 0 or len(destination) == 0:
return False
m = len(maze)
n = len(maze[0])
queue = [start]
visited = [[False for _ in range(n)] for _ in range(m)]
orients = [(0,1),(0,-1),(-1,0),(1,0)]
while queue != []:
node = queue.pop(0)
i = node[0]
j = node[1]
if i == destination[0] and j == destination[1]:
return True
for x, y in orients:
p = i
q = j
while 0<=p+x
class Solution:
"""
@param maze: the maze
@param start: the start
@param destination: the destination
@return: whether the ball could stop at the destination
"""
def hasPath(self, maze, start, destination):
# write your code here
if len(maze) == 0 or len(start) == 0 or len(destination) == 0:
return False
visited = [[False for _ in range(len(maze[0]))] for _ in range(len(maze))]
return self.dfs(maze, start[0], start[1], destination, visited)
def dfs(self, maze, i, j, destination, visited):
if i == destination[0] and j == destination[1]:
return True
if visited[i][j]:
return False
visited[i][j] = True
m = len(maze)
n = len(maze[0])
orients = [(0,1),(0,-1),(1,0),(-1,0)]
for x, y in orients:
p = i
q = j
while 0<=p+x
https://www.lintcode.com/problem/the-maze-ii/
class Solution:
"""
@param maze: the maze
@param start: the start
@param destination: the destination
@return: the shortest distance for the ball to stop at the destination
"""
def shortestDistance(self, maze, start, destination):
# write your code here
if len(maze) == 0 or len(start) == 0 or len(destination) == 0:
return
m = len(maze)
n = len(maze[0])
visited = [[False for _ in range(n)] for _ in range(m)]
orients = [(0,1),(0,-1),(1,0),(-1,0)]
res = float('inf')
queue = [start + [0]]
while queue != []:
node = queue.pop(0)
if node[0] == destination[0] and node[1] == destination[1]:
res = min(node[2],res)
continue
for x,y in orients:
p = node[0]
q = node[1]
dist = node[2]
while 0<=p+x