Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_way(cell, p_x, p_y):
- stack = []
- while cell[0] != p_x or cell[1] != p_y:
- stack += [cell]
- cell = cell[2]
- stack += [cell]
- print len(stack) - 1
- for x in stack[::-1]:
- print x[0], x[1]
- def print_history(history):
- print len(history)
- for x in history:
- print x[0], x[1]
- def process(rows, columns, cur_cell, food_x, food_y, grid, history):
- history += [cur_cell]
- grid[cur_cell[0]][cur_cell[1]] = '#'
- if cur_cell[0] == food_x and cur_cell[1] == food_y:
- return True
- global order
- for dir in order:
- next_cell = [cur_cell[0] + dir[0], cur_cell[1] + dir[1], cur_cell]
- if grid[next_cell[0]][next_cell[1]] in ['%', '#']:
- continue
- dfs_result = process(rows, columns, next_cell, food_x, food_y, grid, history)
- if dfs_result:
- return True
- return False
- def main():
- [p_x, p_y] = [int(i) for i in raw_input().strip().split()]
- [food_x, food_y] = [int(i) for i in raw_input().strip().split()]
- [rows, columns] = [int(i) for i in raw_input().strip().split()]
- grid = []
- for i in xrange(rows):
- line = raw_input().strip()
- line_elem = []
- for x in line:
- line_elem += [x]
- grid += [line_elem]
- history = []
- cur_cell = [p_x, p_y]
- process(rows, columns, cur_cell, food_x, food_y, grid, history)
- print_history(history)
- print_way(history[-1], p_x, p_y)
- #order = [[0, 1], [1, 0], [-1, 0], [0, -1]]
- order = [[1, 0], [0, 1], [0, -1], [-1, 0]]
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement