




'''simple docstring'''




import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
					import ctypes
					import msvcrt  # noqa






					class      UpperCAmelCase_     (  ctypes.Structure     ):


						"""simple docstring"""


						snake_case									=		[('''size''', ctypes.c_int), ('''visible''', ctypes.c_byte)]
def      __snake_case       (   ):
					if os.name == "nt":
										snake_case_	=      CursorInfo()
										snake_case_	=      ctypes.windll.kernelaa.GetStdHandle(-11  )
										ctypes.windll.kernelaa.GetConsoleCursorInfo(_SCREAMING_SNAKE_CASE			, ctypes.byref(_SCREAMING_SNAKE_CASE  )  )
										snake_case_	=      False
										ctypes.windll.kernelaa.SetConsoleCursorInfo(_SCREAMING_SNAKE_CASE			, ctypes.byref(_SCREAMING_SNAKE_CASE  )  )
					elif os.name == "posix":
										sys.stdout.write("\033[?25l"  )
										sys.stdout.flush()
def      __snake_case       (   ):
					if os.name == "nt":
										snake_case_	=      CursorInfo()
										snake_case_	=      ctypes.windll.kernelaa.GetStdHandle(-11  )
										ctypes.windll.kernelaa.GetConsoleCursorInfo(_SCREAMING_SNAKE_CASE			, ctypes.byref(_SCREAMING_SNAKE_CASE  )  )
										snake_case_	=      True
										ctypes.windll.kernelaa.SetConsoleCursorInfo(_SCREAMING_SNAKE_CASE			, ctypes.byref(_SCREAMING_SNAKE_CASE  )  )
					elif os.name == "posix":
										sys.stdout.write("\033[?25h"  )
										sys.stdout.flush()





@contextmanager
def      __snake_case       (   ):
					try:
										hide_cursor()
										yield
					finally:
										show_cursor()






from heapq import heappop, heappush

import numpy as np






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				np.ndarray		, _SCREAMING_SNAKE_CASE     :				tuple[int, int]		, _SCREAMING_SNAKE_CASE     :				tuple[int, int]		, _SCREAMING_SNAKE_CASE     :				bool		, ):
						UpperCamelCase_,UpperCamelCase_		:							Union[str, Any]  =		grid.shape
						UpperCamelCase_		:							List[str]  =		[-1, 1, 0, 0]
						UpperCamelCase_		:							Dict  =		[0, 0, -1, 1]
						if allow_diagonal:
												dx += [-1, -1, 1, 1]
												dy += [-1, 1, -1, 1]

						UpperCamelCase_,UpperCamelCase_		:							List[Any]  =		[(0, source)], set()
						UpperCamelCase_		:							Any  =		np.full((rows, cols)		, np.inf			)
						UpperCamelCase_		:							List[str]  =		0
						UpperCamelCase_		:							Union[str, Any]  =		np.empty((rows, cols)		, dtype=_SCREAMING_SNAKE_CASE			)
						UpperCamelCase_		:							Optional[int]  =		None

						while queue:
												((UpperCamelCase_),(UpperCamelCase_))		:							Any  =		heappop(_SCREAMING_SNAKE_CASE			)
												if (x, y) in visited:
																		continue
												visited.add((x, y)			)

												if (x, y) == destination:
																		UpperCamelCase_		:							Tuple  =		[]
																		while (x, y) != source:
																								path.append((x, y)			)
																								UpperCamelCase_,UpperCamelCase_		:							Union[str, Any]  =		predecessors[x, y]
																		path.append(_SCREAMING_SNAKE_CASE			)  # add the source manually
																		path.reverse()
																		return matrix[destination], path

												for i in range(len(_SCREAMING_SNAKE_CASE			)			):
																		UpperCamelCase_,UpperCamelCase_		:							Optional[Any]  =		x + dx[i], y + dy[i]
																		if 0 <= nx < rows and 0 <= ny < cols:
																								UpperCamelCase_		:							Union[str, Any]  =		grid[nx][ny]
																								if next_node == 1 and matrix[nx, ny] > dist + 1:
																														heappush(_SCREAMING_SNAKE_CASE		, (dist + 1, (nx, ny))			)
																														UpperCamelCase_		:							Optional[Any]  =		dist + 1
																														UpperCamelCase_		:							List[Any]  =		(x, y)

						return np.inf, []


if __name__ == "__main__":
	import doctest

	doctest.testmod()






