Created
July 6, 2016 15:23
-
-
Save stek29/2d7d3c9006af8add51b508cf440323df to your computer and use it in GitHub Desktop.
https://repl.it/C9PH/0 created by stek29
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
class CellState(Enum): | |
non_visited, block, visited, last_cell = range(4) | |
def __repr__(self): | |
return self.name[0].upper() | |
class Done(Exception): | |
pass | |
class Cell: | |
def __init__(self, state: CellState): | |
self.state = state | |
def tick(self, neighbor_states: tuple): | |
""" returns True if state was changed | |
raises Done if visited while in last_cell state """ | |
old_state = self.state | |
if self.state != CellState.block: | |
if CellState.last_cell in neighbor_states: | |
self.state = CellState.last_cell | |
if self.state in (CellState.non_visited, CellState.last_cell): | |
if CellState.visited in neighbor_states: | |
if self.state is CellState.last_cell: | |
raise Done() | |
else: | |
self.state = CellState.visited | |
return self.state != old_state: | |
def __repr__(self): | |
return str(self.state) | |
def get_state(x, y): | |
if x < 0 or y < 0: | |
return CellState.block | |
try: | |
return cells[x][y].state | |
except IndexError: | |
return CellState.block | |
def run_tick(x, y): | |
return cells[x][y].tick(( | |
get_state(x-1, y), | |
get_state(x+1, y), | |
get_state(x, y-1), | |
get_state(x, y+1), | |
)) | |
def read_cords(): | |
return map(int, input().split(' ')) | |
cells = list() | |
M, N = read_cords() | |
for i in range(M): | |
cells.append(list()) | |
digits = list(map(int, input())) | |
for j in range(N): | |
cells[i].append(Cell(CellState(digits[j]))) | |
i, j = read_cords() | |
u, v = read_cords() | |
cells[i-1][j-1].state = CellState.visited | |
cells[u-1][v-1].state = CellState.last_cell | |
can = False | |
itercnt = 0 | |
try: | |
keep_running = True | |
while keep_running: | |
keep_running = False | |
for i in range(M): | |
for j in range(N): | |
keep_running = run_tick(i, j) or keep_running | |
itercnt+=1 | |
except Done: | |
can = True | |
print(("M" if can else "Ne m") + "ozhet") | |
print(itercnt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 3.5.1 (default, Dec 2015, 13:05:11) | |
[GCC 4.8.2] on linux | |
>>> 5 6 | |
000000 | |
111110 | |
000000 | |
011111 | |
000000 | |
1 1 | |
5 6 | |
Mozhet | |
284 | |
=> None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment