Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def wave(r, c):
- q = [(r, c)]
- number = 1
- while q:
- new_q = []
- for r, c in q:
- m[r][c] = number
- for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
- rr = r + dr
- cc = c + dc
- if 0 <= rr < nr and 0 <= cc < nc:
- if a[rr][cc] != 'X' and m[rr][cc] == 0:
- new_q.append((rr, cc))
- number += 1
- q = new_q
- def get_path(r, c):
- while m[r][c] > 1:
- next_res = None
- for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
- rr = r + dr
- cc = c + dc
- if 0 <= rr < nr and 0 <= cc < nc:
- if m[rr][cc] != 0:
- if next_res is None or m[rr][cc] < next_res:
- next_res = m[rr][cc]
- next_r = rr
- next_c = cc
- a[next_r][next_c] = '*'
- r, c = next_r, next_c
- f = open('input.txt')
- a = []
- r = 0
- for line in f.readlines():
- a.append(list(line.strip()))
- if 's' in line:
- start = (r, line.find('s'))
- if 'f' in line:
- finish = (r, line.find('f'))
- r += 1
- f.close()
- nc = len(a[0])
- nr = len(a)
- m = [[0 for c in range(nc)] for r in range(nr)]
- wave(*start)
- get_path(*finish)
- r, c = finish
- res = m[r][c] - 1
- if res > 0:
- print(res)
- else:
- print('Пути нет')
- r, c = start
- a[r][c] = 's'
- for r in range(nr):
- for c in range(nc):
- print(a[r][c], end='')
- f.write()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement