# Task
 We have a N×N `matrix` (N<10) and a robot. 
 
 We wrote in each point of matrix x and y coordinates of a point of matrix. 
 
 When robot goes to a point of matrix, reads x and y and transfer to point with x and y coordinates.
 
 For each point in the matrix we want to know if robot returns back to it after `EXACTLY k` moves. So your task is to count points to which Robot returns in `EXACTLY k` moves. 
 
 You should stop counting moves as soon as the robot returns to the starting point. That is, if the robot returns to the starting point in fewer than k moves, that point should not count as a valid point.
 
# example

 For:
 ```
 matrix=[
 ["0,1","0,0","1,2"], 
 ["1,1","1,0","0,2"], 
 ["2,1","2,0","0,0"]]
 k= 2
 ```
 The result should be `8`
```
Robot start at (0,0) --> (0,1) --> (0,0), total 2 moves
Robot start at (0,1) --> (0,0) --> (0,1), total 2 moves
Robot start at (0,2) --> (1,2) --> (0,2), total 2 moves
Robot start at (1,2) --> (0,2) --> (1,2), total 2 moves
Robot start at (1,0) --> (1,1) --> (1,0), total 2 moves
Robot start at (1,1) --> (1,0) --> (1,1), total 2 moves
Robot start at (2,0) --> (2,1) --> (2,0), total 2 moves
Robot start at (2,1) --> (2,0) --> (2,1), total 2 moves
Robot start at (2,2) --> (0,0) --> (0,1) --> (0,0) --> (0,1) ....
(Robot can not transfer back to 2,2)
```
So the result is 8.

# Input/Output


 - `[input]` 2D integer array matrix
 
 n x n matrix. 3 <= n <=9
 
 
 - `[input]` integer `k`
 
 `2 <= k <= 5`
 
 
 - `[output]` an integer

Source: codewars | Difficulty: EASY
Tags: Ad-hoc

Starter code: def robot_transfer(matrix, k):
	

Solutions:
def robot_transfer(matrix, k):
	c = 0
	for (l, i) in enumerate(matrix):
		for (o, j) in enumerate(i):
			(x, y) = j.split(',')
			(current, count, new) = ([l, o], 0, [])
			while count < k and current != new:
				new = [int(x), int(y)]
				(x, y) = matrix[int(x)][int(y)].split(',')
				count += 1
			if current == new and count == k:
				c += 1
	return c


def robot_transfer(matrix, k):
	(res, seen) = (0, set())
	for i in range(len(matrix)):
		for j in range(len(matrix)):
			if (i, j) in seen:
				continue
			(x, y, S) = (i, j, {(i, j)})
			for _ in range(k):
				(x, y) = map(int, matrix[x][y].split(','))
				S.add((x, y))
			if (x, y) == (i, j) and len(S) == k:
				res += k
				seen |= S
	return res


def f(matrix, k, i, j):
	(i0, j0) = (i, j)
	for n in range(1, k + 1):
		(i, j) = matrix[i][j]
		if i == i0 and j == j0:
			return n == k
	return False

def robot_transfer(matrix, k):
	matrix = [[tuple(map(int, x.split(','))) for x in row] for row in matrix]
	return sum((f(matrix, k, i, j) for (i, row) in enumerate(matrix) for (j, x) in enumerate(row)))


def robot_transfer(matrix, k):
	result = 0
	for i in range(len(matrix[0])):
		for j in range(len(matrix)):
			(x, y, back) = (i, j, 0)
			for _ in range(k):
				(x, y) = (int(n) for n in matrix[x][y].split(','))
				back += (i, j) == (x, y)
			result += (i, j) == (x, y) and back == 1
	return result


def robot_transfer(matrix, k):
	cycles = 0
	for (x, l) in enumerate(matrix):
		for (y, _) in enumerate(l):
			(u, v) = (x, y)
			for n in range(k):
				(u, v) = map(int, matrix[u][v].split(','))
				if (x, y) == (u, v):
					cycles += n == k - 1
					break
	return cycles


def robot_transfer(matrix, k):
	count = 0
	for (start_y, row) in enumerate(matrix):
		for (start_x, point) in enumerate(row):
			(y, x) = (int(point[0]), int(point[2]))
			for t in range(k - 1):
				(y, x) = (int(matrix[y][x][0]), int(matrix[y][x][2]))
				if (y, x) == (start_y, start_x):
					count += t + 2 == k
					break
	return count


def robot_transfer(matrix, k):
	r = 0
	n = len(matrix)
	for i in range(n):
		for j in range(n):
			(x, y) = (i, j)
			for step in range(k):
				(x, y) = map(int, matrix[x][y].split(','))
				if x == i and y == j:
					break
			if x == i and y == j and (step == k - 1):
				r += 1
	return r


def robot_transfer(matrix, k):
	res = []
	for (r, rows) in enumerate(matrix):
		for (c, point) in enumerate(rows):
			kk = 1
			while kk <= k:
				t = list(map(int, point.split(',')))
				print(t)
				point = matrix[t[0]][t[1]]
				kk += 1
				if list(map(int, point.split(','))) == [r, c]:
					break
			if kk == k and list(map(int, point.split(','))) == [r, c]:
				res.append(1)
	return sum(res)


def floyd(matrix, row, col):

	def f(row, col):
		return list(map(int, matrix[row][col].split(',')))
	x0 = [row, col]
	tortoise = f(*x0)
	hare = f(*f(*x0))
	while tortoise != hare:
		tortoise = f(*tortoise)
		hare = f(*f(*hare))
	mu = 0
	tortoise = x0
	while tortoise != hare:
		tortoise = f(*tortoise)
		hare = f(*hare)
		mu += 1
	lam = 1
	hare = f(*tortoise)
	while tortoise != hare:
		hare = f(*hare)
		lam += 1
	return lam if mu == 0 else -1

def robot_transfer(matrix, k):
	SIZE = len(matrix)
	return sum((1 for row in range(SIZE) for col in range(SIZE) if floyd(matrix, row, col) == k))


def robot_transfer(matrix, k):
	N = len(matrix)
	mat = [[0] * N for i in range(N)]
	for i in range(N):
		for j in range(N):
			mat[i][j] = (int(matrix[i][j][0]), int(matrix[i][j][2]))
	tot = 0
	for i in range(N):
		for j in range(N):
			y = (i, j)
			for kk in range(k):
				y = mat[y[0]][y[1]]
				if y == (i, j) and kk < k - 1:
					break
			if y == (i, j) and kk == k - 1:
				tot += 1
	return tot