UnderCoverFed UnderCoverFed - 1 year ago 77
Scala Question

How to recursively get the number of elements until reached all sqaures in 5 x 5 grid in scala

So I have a grid 5 x 5 grid. And my initial position is at (0,0)

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
# 0 0 0 0

I have a method that finds all possible position in 'L' shape from that position
So from

So either

  • ( x + 2 )( y + 1 )


  • ( x + 1 )( y + 2 )

We have two positions

0 0 0 0 0
0 0 0 0 0
0 # 0 0 0
0 0 0 0 0
# 0 0 0 0


0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 # 0 0
# 0 0 0 0

Then my method will call it self from the list of all the possible moves and finds the position of each one.

The recursion only breaks if the last position in the possible moves list is the same as the initial position.

So the possible moves for position:

  • (0)(0)

  • List((possible moves for (1,2) ), (possible moves for (2,1) ) )

and so on:

My method so far

def CountTour(dimension: Int, path: Path): Int = {
// possibleMoves returns list of possible moves
val Moves = possibleMoves(dimension, path, path.head).contains(path.last)

// if the last element is not the same as first position, and has visited the whole graph, then break the recursion
if (legalMoves && path.size == (dimension * dimension)) {
} else {
// else call the same method again with each possible move
val newList = for (i <- legal_moves(dimension, path, path.head)) yield i

However this won't work. How can i fix it?

Answer Source

It looks to me like you don't actually have any working code for this yet. I say this because A) the code you posted has lots of holes, and B) I didn't get a straight answer when I asked what output you're currently getting. For these reasons (and because this looks like a homework assignment) I'm not going to post an answer with real code but, instead, I'll describe what I think should work.

The goal is to write a function (actually a method) that takes the dimension of the playing board, and a starting point on it, and returns a count of all paths (successive moves) that exhaust the board (no returning to an already visited square).

1st - From any starting point there are 8 different positions that can be moved to (2 steps to the east/west/north/south and then 1 step to the left/right). I'd write a procedure that takes the grid dimensions and a starting point. It first creates a collection (maybe a List) of all 8 possible new locations and then keeps only those that fall within the grid (i.e. no negative numbers or coordinates larger than the grid dimension).

2nd - That list will have to be checked against the list of places that have already been visited and any duplicates removed. I'd use the diff method. Note that ...

possible_moves diff already_been_there  // one result
already_been_there diff possible_moves  // different result

One of those is the one you want.

3rd - Now you have a list of all permissible moves. If that list is empty then you're at the end of the road and there are no more moves to make. Return 1 because you've found one path that exhausts the board.

4th - If the list is not empty then you want to re-call this same procedure (recursion) with a new starting point and an updated list of already-been-there squares. The problem is that this list has at least one, and maybe as many as eight, new locations. How can you invoke the recursive call a different number of times with each iteration? Well, what you want to do is change your list from a list of new locations to a list of returned values from the recursive call. You know how to turn a List[A] into a List[B]? (Hint: it rhymes with "nap".)

5th - If you've gotten this far then you have a list of numbers, each is a count of how many ways to exhaust the board from each new starting point. Add them together and you've got a count of how many ways to exhaust the board from this starting point, and that's the number you return.

If I've described your goal correctly, and I've coded it up correctly, then you can check my work. For a 5X5 board, starting at 0,0, I came up with 625,308 different paths. (17 lines of code, but I wasn't trying to be terribly concise.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download