

from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload






class 	A_		:
							def __init__(		self		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      Collection[float] | None = None):
								if components is None:
									__lowerCamelCase    :    List[str] 	=			[]
								__lowerCamelCase    :    Tuple 	=			list(SCREAMING_SNAKE_CASE__)
							def __len__(		self		:      List[Any]):
								return len(self.__components)
							def __str__(		self		:      Optional[int]):
								return "(" + ",".join(map(SCREAMING_SNAKE_CASE__       ,self.__components)) + ")"
							def __add__(		self		:      Tuple       ,SCREAMING_SNAKE_CASE__		:      Vector):
								__lowerCamelCase    :    Tuple 	=			len(self)
								if size == len(SCREAMING_SNAKE_CASE__):
									__lowerCamelCase    :    Optional[int] 	=			[self.__components[i] + other.component(SCREAMING_SNAKE_CASE__) for i in range(SCREAMING_SNAKE_CASE__)]
									return Vector(SCREAMING_SNAKE_CASE__)
								else:
									raise Exception('must have the same size')
							def __sub__(		self		:      Dict       ,SCREAMING_SNAKE_CASE__		:      Vector):
								__lowerCamelCase    :    str 	=			len(self)
								if size == len(SCREAMING_SNAKE_CASE__):
									__lowerCamelCase    :    Any 	=			[self.__components[i] - other.component(SCREAMING_SNAKE_CASE__) for i in range(SCREAMING_SNAKE_CASE__)]
									return Vector(SCREAMING_SNAKE_CASE__)
								else:  # error case
									raise Exception('must have the same size')
							@overload
							def __mul__(		self		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      float):
								...
							@overload
							def __mul__(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      Vector):
								...
							def __mul__(		self		:      int       ,SCREAMING_SNAKE_CASE__		:      float | Vector):
								if isinstance(SCREAMING_SNAKE_CASE__       ,(float, int)):
									__lowerCamelCase    :    List[str] 	=			[c * other for c in self.__components]
									return Vector(SCREAMING_SNAKE_CASE__)
								elif isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__) and len(self) == len(SCREAMING_SNAKE_CASE__):
									__lowerCamelCase    :    List[str] 	=			len(self)
									__lowerCamelCase    :    Any 	=			[self.__components[i] * other.component(SCREAMING_SNAKE_CASE__) for i in range(SCREAMING_SNAKE_CASE__)]
									return sum(SCREAMING_SNAKE_CASE__)
								else:  # error case
									raise Exception('invalid operand!')
							def    lowerCAmelCase					(		self		:      Dict):
								return Vector(self.__components)
							def    lowerCAmelCase					(		self		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      int):
								if isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__) and -len(self.__components) <= i < len(self.__components):
									return self.__components[i]
								else:
									raise Exception('index out of range')
							def    lowerCAmelCase					(		self		:      Tuple       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      float):
								assert -len(self.__components) <= pos < len(self.__components)
								__lowerCamelCase    :    List[Any] 	=			value
							def    lowerCAmelCase					(		self		:      int):
								if len(self.__components) == 0:
									raise Exception('Vector is empty')
								__lowerCamelCase    :    str 	=			[c**2 for c in self.__components]
								return math.sqrt(sum(SCREAMING_SNAKE_CASE__))


							def    lowerCAmelCase					(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      Vector       ,SCREAMING_SNAKE_CASE__		:      bool = False):
								__lowerCamelCase    :    List[str] 	=			self * other
								__lowerCamelCase    :    Any 	=			self.euclidean_length() * other.euclidean_length()
								if deg:
									return math.degrees(math.acos(num / den))
								else:
									return math.acos(num / den)






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					Vector:
	assert isinstance(lowerCamelCase__     ,			lowerCamelCase__      )
	return Vector([0] * dimension      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__      )  ->					Vector:
	assert isinstance(lowerCamelCase__     ,			lowerCamelCase__      ) and (isinstance(lowerCamelCase__     ,			lowerCamelCase__      ))
	__lowerCamelCase    :    str 	=			[0] * dimension
	__lowerCamelCase    :    Optional[Any] 	=			1
	return Vector(lowerCamelCase__      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      )  ->					Vector:
	assert (
	    isinstance(lowerCamelCase__     ,			lowerCamelCase__      )
	    and isinstance(lowerCamelCase__     ,			lowerCamelCase__      )
	    and (isinstance(lowerCamelCase__     ,			(int, float)      ))
	)
	return x * scalar + y






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      )  ->					Vector:
	random.seed(lowerCamelCase__      )
	__lowerCamelCase    :    List[str] 	=			[random.randint(lowerCamelCase__     ,			lowerCamelCase__      ) for _ in range(lowerCamelCase__      )]
	return Vector(lowerCamelCase__      )






class 	A_		:
							def __init__(		self		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      list[list[float]]       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      int):
								__lowerCamelCase    :    Dict 	=			matrix
								__lowerCamelCase    :    Tuple 	=			w
								__lowerCamelCase    :    Tuple 	=			h
							def __str__(		self		:      List[str]):
								__lowerCamelCase    :    Dict 	=			''
								for i in range(self.__height):
									ans += "|"
									for j in range(self.__width):
										if j < self.__width - 1:
											ans += str(self.__matrix[i][j]) + ","
										else:
											ans += str(self.__matrix[i][j]) + "|\n"
								return ans
							def __add__(		self		:      List[str]       ,SCREAMING_SNAKE_CASE__		:      Matrix):
								if self.__width == other.width() and self.__height == other.height():
									__lowerCamelCase    :    Union[str, Any] 	=			[]
									for i in range(self.__height):
										__lowerCamelCase    :    List[Any] 	=			[
										    self.__matrix[i][j] + other.component(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
										    for j in range(self.__width)
										]
										matrix.append(SCREAMING_SNAKE_CASE__)
									return Matrix(SCREAMING_SNAKE_CASE__       ,self.__width       ,self.__height)
								else:
									raise Exception('matrix must have the same dimension!')
							def __sub__(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      Matrix):
								if self.__width == other.width() and self.__height == other.height():
									__lowerCamelCase    :    Optional[Any] 	=			[]
									for i in range(self.__height):
										__lowerCamelCase    :    Optional[int] 	=			[
										    self.__matrix[i][j] - other.component(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
										    for j in range(self.__width)
										]
										matrix.append(SCREAMING_SNAKE_CASE__)
									return Matrix(SCREAMING_SNAKE_CASE__       ,self.__width       ,self.__height)
								else:
									raise Exception('matrices must have the same dimension!')
							@overload
							def __mul__(		self		:      int       ,SCREAMING_SNAKE_CASE__		:      float):
								...
							@overload
							def __mul__(		self		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      Vector):
								...
							def __mul__(		self		:      int       ,SCREAMING_SNAKE_CASE__		:      float | Vector):
								if isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__):  # matrix-vector
									if len(SCREAMING_SNAKE_CASE__) == self.__width:
										__lowerCamelCase    :    Tuple 	=			zero_vector(self.__height)
										for i in range(self.__height):
											__lowerCamelCase    :    Union[str, Any] 	=			[
											    self.__matrix[i][j] * other.component(SCREAMING_SNAKE_CASE__)
											    for j in range(self.__width)
											]
											ans.change_component(SCREAMING_SNAKE_CASE__       ,sum(SCREAMING_SNAKE_CASE__))
										return ans
									else:
										raise Exception(
										    'vector must have the same size as the '
										    'number of columns of the matrix!')
								elif isinstance(SCREAMING_SNAKE_CASE__       ,(int, float)):  # matrix-scalar
									__lowerCamelCase    :    List[str] 	=			[
									    [self.__matrix[i][j] * other for j in range(self.__width)]
									    for i in range(self.__height)
									]
									return Matrix(SCREAMING_SNAKE_CASE__       ,self.__width       ,self.__height)
								return None
							def    lowerCAmelCase					(		self		:      Any):
								return self.__height
							def    lowerCAmelCase					(		self		:      Dict):
								return self.__width
							def    lowerCAmelCase					(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      int):
								if 0 <= x < self.__height and 0 <= y < self.__width:
									return self.__matrix[x][y]
								else:
									raise Exception('change_component: indices out of bounds')
							def    lowerCAmelCase					(		self		:      str       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      float):
								if 0 <= x < self.__height and 0 <= y < self.__width:
									__lowerCamelCase    :    Optional[int] 	=			value
								else:
									raise Exception('change_component: indices out of bounds')
							def    lowerCAmelCase					(		self		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      int):
								if self.__height != self.__width:
									raise Exception('Matrix is not square')
								__lowerCamelCase    :    int 	=			self.__matrix[:x] + self.__matrix[x + 1 :]
								for i in range(len(SCREAMING_SNAKE_CASE__)):
									__lowerCamelCase    :    Any 	=			minor[i][:y] + minor[i][y + 1 :]
								return Matrix(SCREAMING_SNAKE_CASE__       ,self.__width - 1       ,self.__height - 1).determinant()
							def    lowerCAmelCase					(		self		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      int):
								if self.__height != self.__width:
									raise Exception('Matrix is not square')
								if 0 <= x < self.__height and 0 <= y < self.__width:
									return (-1) ** (x + y) * self.minor(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
								else:
									raise Exception('Indices out of bounds')


							def    lowerCAmelCase					(		self		:      Tuple):
								if self.__height != self.__width:
									raise Exception('Matrix is not square')
								if self.__height < 1:
									raise Exception('Matrix has no element')
								elif self.__height == 1:
									return self.__matrix[0][0]
								elif self.__height == 2:
									return (
									    self.__matrix[0][0] * self.__matrix[1][1]
									    - self.__matrix[0][1] * self.__matrix[1][0]
									)
								else:
									__lowerCamelCase    :    Optional[int] 	=			[
									    self.__matrix[0][y] * self.cofactor(0       ,SCREAMING_SNAKE_CASE__) for y in range(self.__width)
									]
									return sum(SCREAMING_SNAKE_CASE__)






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					Matrix:
	__lowerCamelCase    :    list[list[float]] 	=			[[0] * n for _ in range(lowerCamelCase__      )]
	return Matrix(lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      )






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      )  ->					Matrix:
	random.seed(lowerCamelCase__      )
	__lowerCamelCase    :    list[list[float]] 	=			[
	    [random.randint(lowerCamelCase__     ,			lowerCamelCase__      ) for _ in range(lowerCamelCase__      )] for _ in range(lowerCamelCase__      )
	]
	return Matrix(lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      )







from __future__ import annotations






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			)  ->					None:
	__lowerCamelCase    :    int 	=			len(lowerCamelCase__      )

	# If row is equal to the size of the board it means there are a queen in each row in
	# the current board (possible_board)
	if row == n:
		# We convert the variable possible_board that looks like this: [1, 3, 0, 2] to
		# this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
		boards.append(['. ' * i + 'Q ' + '. ' * (n - 1 - i) for i in possible_board]      )
		return

	# We iterate each column in the row to find all possible results in each row
	for col in range(lowerCamelCase__      ):
		# We apply that we learned previously. First we check that in the current board
		# (possible_board) there are not other same value because if there is it means
		# that there are a collision in vertical. Then we apply the two formulas we
		# learned before:
		#
		# 45º: y - x = b or 45: row - col = b
		# 135º: y + x = b or row + col = b.
		#
		# And we verify if the results of this two formulas not exist in their variables
		# respectively.  (diagonal_right_collisions, diagonal_left_collisions)
		#
		# If any or these are True it means there is a collision so we continue to the
		# next value in the for loop.
		if (
		    col in possible_board
		    or row - col in diagonal_right_collisions
		    or row + col in diagonal_left_collisions
		):
			continue

		# If it is False we call dfs function again and we update the inputs
		depth_first_search(
		    [*possible_board, col]     ,			[*diagonal_right_collisions, row - col]     ,			[*diagonal_left_collisions, row + col]     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			)






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					None:
	__lowerCamelCase    :    list[list[str]] 	=			[]
	depth_first_search([]     ,			[]     ,			[]     ,			lowerCamelCase__     ,			lowerCamelCase__      )

	# Print all the boards
	for board in boards:
		for column in board:
			print(lowerCamelCase__      )
		print(''      )

	print(len(lowerCamelCase__      )     ,			'solutions were found.'      )


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					n_queens_solution(4)




