


import copy
import re







class 	__lowercase    :





							"""simple docstring"""







							_UpperCAmelCase      =						"""hp"""
							_UpperCAmelCase      =						{}
							_UpperCAmelCase      =						None






							@classmethod
							def 					UpperCamelCase__     (      cls			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					prefix
										SCREAMING_SNAKE_CASE_		:     Dict					=					defaults
										cls.build_naming_info()






							@staticmethod
							def 					UpperCamelCase__     (      lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										if len(lowerCAmelCase__       ) == 0:
													return ""
										SCREAMING_SNAKE_CASE_		:     Tuple					=					None
										if any(char.isdigit() for char in word       ):
													raise Exception(F'''Parameters should not contain numbers: \'{word}\' contains a number'''       )
										if word in info["short_word"]:
													return info["short_word"][word]
										for prefix_len in range(1			,						len(lowerCAmelCase__       ) + 1       ):
													SCREAMING_SNAKE_CASE_		:     Tuple					=					word[:prefix_len]
													if prefix in info["reverse_short_word"]:
																continue
													else:
																SCREAMING_SNAKE_CASE_		:     Tuple					=					prefix
																break

										if short_word is None:
													# Paranoid fallback
													def int_to_alphabetic(lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     Tuple					=					''
																while integer != 0:
																			SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					chr(ord('A'       ) + integer % 1_0       ) + s
																			integer //= 1_0
																return s

													SCREAMING_SNAKE_CASE_		:     Dict					=					0
													while True:
																SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					word + '#' + int_to_alphabetic(lowerCAmelCase__       )
																if sword in info["reverse_short_word"]:
																			continue
																else:
																			SCREAMING_SNAKE_CASE_		:     Dict					=					sword
																			break

										SCREAMING_SNAKE_CASE_		:     List[str]					=					short_word
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					word
										return short_word






							@staticmethod
							def 					UpperCamelCase__     (      lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					param_name.split('_'       )

										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					[TrialShortNamer.shortname_for_word(lowerCAmelCase__			,						lowerCAmelCase__       ) for word in words]

										# We try to create a separatorless short name, but if there is a collision we have to fallback
										# to a separated short name
										SCREAMING_SNAKE_CASE_		:     Any					=					['', '_']

										for separator in separators:
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					separator.join(lowerCAmelCase__       )
													if shortname not in info["reverse_short_param"]:
																SCREAMING_SNAKE_CASE_		:     Any					=					shortname
																SCREAMING_SNAKE_CASE_		:     Optional[int]					=					param_name
																return shortname

										return param_name






							@staticmethod
							def 					UpperCamelCase__     (      lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					TrialShortNamer.shortname_for_key(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					short_name
										SCREAMING_SNAKE_CASE_		:     List[str]					=					param_name






							@classmethod
							def 					UpperCamelCase__     (      cls       ):





										"""simple docstring"""




										if cls.NAMING_INFO is not None:
													return

										SCREAMING_SNAKE_CASE_		:     Any					=					{
										    'short_word': {},
										    'reverse_short_word': {},
										    'short_param': {},
										    'reverse_short_param': {},
										}

										SCREAMING_SNAKE_CASE_		:     int					=					list(cls.DEFAULTS.keys()       )

										for k in field_keys:
													cls.add_new_param_name(lowerCAmelCase__			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     str					=					info






							@classmethod
							def 					UpperCamelCase__     (      cls			,						lowerCAmelCase__       ):





										"""simple docstring"""




										cls.build_naming_info()
										assert cls.PREFIX is not None
										SCREAMING_SNAKE_CASE_		:     str					=					[copy.copy(cls.PREFIX       )]

										for k, v in params.items():
													if k not in cls.DEFAULTS:
																raise Exception(F'''You should provide a default value for the param name {k} with value {v}'''       )
													if v == cls.DEFAULTS[k]:
																# The default value is not added to the name
																continue

													SCREAMING_SNAKE_CASE_		:     int					=					cls.NAMING_INFO['short_param'][k]

													if isinstance(lowerCAmelCase__			,						lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					1 if v else 0

													SCREAMING_SNAKE_CASE_		:     int					=					'' if isinstance(lowerCAmelCase__			,						(int, float)       ) else '-'
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					F'''{key}{sep}{v}'''
													name.append(lowerCAmelCase__       )

										return "_".join(lowerCAmelCase__       )






							@classmethod
							def 					UpperCamelCase__     (      cls			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					repr[len(cls.PREFIX       ) + 1 :]
										if repr == "":
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					[]
										else:
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					repr.split('_'       )

										SCREAMING_SNAKE_CASE_		:     int					=					{}

										for value in values:
													if "-" in value:
																SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     List[str]					=					value.split('-'       )
													else:
																SCREAMING_SNAKE_CASE_		:     str					=					re.sub('[0-9.]'			,						''			,						lowerCAmelCase__       )
																SCREAMING_SNAKE_CASE_		:     List[str]					=					float(re.sub('[^0-9.]'			,						''			,						lowerCAmelCase__       )       )

													SCREAMING_SNAKE_CASE_		:     int					=					cls.NAMING_INFO['reverse_short_param'][p_k]

													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					p_v

										for k in cls.DEFAULTS:
													if k not in parameters:
																SCREAMING_SNAKE_CASE_		:     Tuple					=					cls.DEFAULTS[k]

										return parameters









from __future__ import annotations

lowerCAmelCase__			:      Union[str, Any]								=[
    [-1, 0],  # left
    [0, -1],  # down
    [1, 0],  # right
    [0, 1],  # up
]






def 						a__			(				A__,      A__,      A__,      A__,      A__,      ):
			SCREAMING_SNAKE_CASE_		:     List[Any]					=					[
			    [0 for col in range(len(grid[0]				)				)] for row in range(len(A__				)				)
			]  # the reference grid
			SCREAMING_SNAKE_CASE_		:     List[Any]					=					1
			SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					[
			    [0 for col in range(len(grid[0]				)				)] for row in range(len(A__				)				)
			]  # the action grid

			SCREAMING_SNAKE_CASE_		:     Optional[int]					=					init[0]
			SCREAMING_SNAKE_CASE_		:     List[str]					=					init[1]
			SCREAMING_SNAKE_CASE_		:     Dict					=					0
			SCREAMING_SNAKE_CASE_		:     List[Any]					=					g + heuristic[x][y]  # cost from starting cell to destination cell
			SCREAMING_SNAKE_CASE_		:     List[str]					=					[[f, g, x, y]]

			SCREAMING_SNAKE_CASE_		:     Dict					=					False  # flag that is set when search is complete
			SCREAMING_SNAKE_CASE_		:     Tuple					=					False  # flag set if we can't find expand

			while not found and not resign:
						if len(A__				) == 0:
									raise ValueError('Algorithm is unable to find solution'				)
						else:  # to choose the least costliest action so as to move closer to the goal
									cell.sort()
									cell.reverse()
									SCREAMING_SNAKE_CASE_		:     Any					=					cell.pop()
									SCREAMING_SNAKE_CASE_		:     Any					=					next_cell[2]
									SCREAMING_SNAKE_CASE_		:     str					=					next_cell[3]
									SCREAMING_SNAKE_CASE_		:     Tuple					=					next_cell[1]

									if x == goal[0] and y == goal[1]:
												SCREAMING_SNAKE_CASE_		:     Dict					=					True
									else:
												for i in range(len(A__				)				):  # to try out different valid actions
															SCREAMING_SNAKE_CASE_		:     Optional[int]					=					x + DIRECTIONS[i][0]
															SCREAMING_SNAKE_CASE_		:     List[Any]					=					y + DIRECTIONS[i][1]
															if xa >= 0 and xa < len(A__				) and ya >= 0 and ya < len(grid[0]				):
																		if closed[xa][ya] == 0 and grid[xa][ya] == 0:
																					SCREAMING_SNAKE_CASE_		:     List[Any]					=					g + cost
																					SCREAMING_SNAKE_CASE_		:     List[Any]					=					ga + heuristic[xa][ya]
																					cell.append([fa, ga, xa, ya]				)
																					SCREAMING_SNAKE_CASE_		:     int					=					1
																					SCREAMING_SNAKE_CASE_		:     int					=					i
			SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					[]
			SCREAMING_SNAKE_CASE_		:     List[str]					=					goal[0]
			SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					goal[1]
			invpath.append([x, y]				)  # we get the reverse path from here
			while x != init[0] or y != init[1]:
						SCREAMING_SNAKE_CASE_		:     Any					=					x - DIRECTIONS[action[x][y]][0]
						SCREAMING_SNAKE_CASE_		:     int					=					y - DIRECTIONS[action[x][y]][1]
						SCREAMING_SNAKE_CASE_		:     int					=					xa
						SCREAMING_SNAKE_CASE_		:     List[Any]					=					ya
						invpath.append([x, y]				)

			SCREAMING_SNAKE_CASE_		:     List[Any]					=					[]
			for i in range(len(A__				)				):
						path.append(invpath[len(A__				) - 1 - i]				)
			return path, action


if __name__ == "__main__":
				lowerCAmelCase__			:      Union[str, Any]								=[
				    [0, 1, 0, 0, 0, 0],
				    [0, 1, 0, 0, 0, 0],  # 0 are free path whereas 1's are obstacles
				    [0, 1, 0, 0, 0, 0],
				    [0, 1, 0, 0, 1, 0],
				    [0, 0, 0, 0, 1, 0],
				]

				lowerCAmelCase__			:      Any								=[0, 0]
				# all coordinates are given in format [y,x]
				lowerCAmelCase__			:      List[str]								=[len(grid) - 1, len(grid[0]) - 1]
				lowerCAmelCase__			:      str								=1

				# the cost map which pushes the path closer to the goal
				lowerCAmelCase__			:      Union[str, Any]								=[[0 for row in range(len(grid[0]))] for col in range(len(grid))]
				for i in range(len(grid)):
								for j in range(len(grid[0])):
												lowerCAmelCase__			:      List[str]								=abs(i - goal[0]) + abs(j - goal[1])
												if grid[i][j] == 1:
																# added extra penalty in the heuristic map
																lowerCAmelCase__			:      List[str]								=99

				lowerCAmelCase__							,lowerCAmelCase__			:      Any								=search(grid, init, goal, cost, heuristic)

				print('ACTION MAP')
				for i in range(len(action)):
								print(action[i])

				for i in range(len(path)):
								print(path[i])





