




'''simple docstring'''





from __future__ import annotations





def      __a     (       _UpperCamelCase:   Optional[Any] ,						_UpperCamelCase:   Dict   )						->			list[tuple[int, int]]:

					"""simple docstring"""




					_snake_case							, _snake_case							=				position
					_snake_case							=				[
					    (y + 1, x + 2),
					    (y - 1, x + 2),
					    (y + 1, x - 2),
					    (y - 1, x - 2),
					    (y + 2, x + 1),
					    (y + 2, x - 1),
					    (y - 2, x + 1),
					    (y - 2, x - 1),
					]
					_snake_case							=				[]

					for position in positions:
										_snake_case							, _snake_case							=				position
										if 0 <= y_test < n and 0 <= x_test < n:
															permissible_positions.append(a__   )

					return permissible_positions





def      __a     (       _UpperCamelCase:   Union[str, Any]   )						->			bool:

					"""simple docstring"""




					return not any(elem == 0 for row in board for elem in row   )





def      __a     (       _UpperCamelCase:   Dict ,						_UpperCamelCase:   Optional[Any] ,						_UpperCamelCase:   List[Any]   )						->			bool:

					"""simple docstring"""




					if is_complete(a__   ):
										return True

					for position in get_valid_pos(a__ ,						len(a__   )   ):
										_snake_case							, _snake_case							=				position

										if board[y][x] == 0:
															_snake_case							=				curr + 1
															if open_knight_tour_helper(a__ ,						a__ ,						curr + 1   ):
																				return True
															_snake_case							=				0

					return False





def      __a     (       _UpperCamelCase:   Dict   )						->			list[list[int]]:

					"""simple docstring"""




					_snake_case							=				[[0 for i in range(a__   )] for j in range(a__   )]

					for i in range(a__   ):
										for j in range(a__   ):
															_snake_case							=				1
															if open_knight_tour_helper(a__ ,						(i, j) ,						1   ):
																				return board
															_snake_case							=				0

					_snake_case							=				F"""Open Kight Tour cannot be performed on a board of size {n}"""
					raise ValueError(a__   )


if __name__ == "__main__":
			import doctest

			doctest.testmod()








'''simple docstring'''





from __future__ import annotations

from collections import namedtuple
from dataclasses import dataclass

@dataclass
class        _a    :
     SCREAMING_SNAKE_CASE_							:   int
     SCREAMING_SNAKE_CASE_							:   TreeNode | None       =						None
     SCREAMING_SNAKE_CASE_							:   TreeNode | None       =						None







UpperCamelCase_						:      Any       =						namedtuple('''CoinsDistribResult''', '''moves excess''')





def      __a     (       _UpperCamelCase:   TreeNode | None   )						->			int:

     """simple docstring"""




     if root is None:
          return 0

     # Validation
     def count_nodes(_UpperCamelCase:   TreeNode | None   ) -> int:
          if node is None:
               return 0

          return count_nodes(node.left   ) + count_nodes(node.right   ) + 1

     def count_coins(_UpperCamelCase:   TreeNode | None   ) -> int:
          if node is None:
               return 0

          return count_coins(node.left   ) + count_coins(node.right   ) + node.data

     if count_nodes(_UpperCamelCase   ) != count_coins(_UpperCamelCase   ):
          raise ValueError("The nodes number should be same as the number of coins"   )

     # Main calculation
     def get_distrib(_UpperCamelCase:   TreeNode | None   ) -> CoinsDistribResult:

          if node is None:
               return CoinsDistribResult(0 ,						1   )

          _snake_case							, _snake_case							=				get_distrib(node.left   )
          _snake_case							, _snake_case							=				get_distrib(node.right   )

          _snake_case							=				1 - left_distrib_excess
          _snake_case							=				1 - right_distrib_excess

          _snake_case							=				(
              left_distrib_moves
              + right_distrib_moves
              + abs(_UpperCamelCase   )
              + abs(_UpperCamelCase   )
          )
          _snake_case							=				node.data - coins_to_left - coins_to_right

          return CoinsDistribResult(_UpperCamelCase ,						_UpperCamelCase   )

     return get_distrib(_UpperCamelCase   )[0]


if __name__ == "__main__":
   import doctest

   doctest.testmod()


