# Is this a Hamiltonian cycle?

There is a 3x3 square block made of 1x1 square blocks, with coins in each 1x1 block, starting from top left block you want to collect all the coins and return to top left block again, if possible provide instructions to achieve this.

Rules

• From block $$\(x,y)\$$ in one step you can move right(R) to $$\(x,y+1)\$$ ,left(L) to $$\(x,y-1)\$$ ,up(U) to $$\(x-1,y)\$$ ,down(D) to $$\(x+1,y)\$$ assuming the block to which we jump is not empty and is well inside our block
• We can take only one coin at a time.
• Just after performing the jump we collect 1 coin from our new position.
• Provide the route to take in form of a string, for example like RLRRDULU...
• You should collect all coins
• If there doesn't exist a solution route print/output -1
• If there exist multiple solution you are free to provide any

Each 1x1 block can have upto 69 coins

Examples :

integer in each place denotes number of coins at each block

1 1 1
1 1 1
1 2 1
here DDRUDRUULL and RRDDLUDLUU both are correct (there might be more but you can print any one of your choice)

2 4 4
2 1 3
1 2 3
here DDUURDDRUULRLRDDLRUULL is a valid answer

5 5 1
5 4 5
5 2 2
here DURLRLRLRDLDUDUDUDRLRURLRLRDUDUULL is a valid answer

2 4 2
2 1 1
1 1 2
here doesn't exist a valid solution for this case so output -1

29 7 6
8 5 4
4 28 3
here doesn't exist a valid solution for this case so output -1

4 3 5
5 4 5
3 5 2
here DUDURLRDLDRULRLRDLRLRRLRUUDUDUDUDULL is valid solution

18 8 33
16 4 38
10 28 25
here DUDUDUDUDUDUDUDUDUDUDURLRLRLRLRLRLRDLDUDURLRLRDLRLRLRLRLRLRLRLRRUUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDDUDUDUDUDLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRUULL is a valid solution

69 69 69
69 69 69
69 69 69
here doesn't exist a valid solution for this case so output -1

7 5 9
21 10 68
15 3 56
here
DUDUDUDUDUDUDDUDUDUDUDUDUDUDUDUDUDUDUDUDUDRRLRLRUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUUDUDUDUDUDUDUDUDUDLRLRLRLRLRLUDUDUDUDUL is a valid solution

17 65 29
16 35 69
19 68 56
here doesn't exist a valid solution for this case so output -1



This is so the goal is to minimize your source code with answers being scored in bytes, but feel free to showcase any of your approach, I will be glad to read all those.

Edit : As mentioned we are starting from top left block, so the number of coins in the top-left block is apparently not decremented when the game starts.Also, as mentioned coin in a block can be as big as 69 so your code should be able to give output in reasonable time(not more than 60 seconds or timeouts), you can test your code in last 3 examples, feel free to ask for some more examples.

Note that it is promised that coin exist in each cell.

Edit : releaxed IO as suggested, return a list or string representing the moves, you can use 4 distinct values for L,R,U,D. If there is no solution, return a consistent value distinct from any solution

• You may want to clarify that the number of coins in the top-left block is apparently not decremented when the game starts. Jan 29, 2022 at 16:03
• Personally, I would relax the IO a bit. Something like "Return a list or string representing the moves. You can use 4 distinct values for L,R,U,D. If there is no solution, return a consistent value distinct from any solution". Jan 29, 2022 at 17:01
• @Arnauld Done, Thanks. Jan 29, 2022 at 17:10
• As done in this task, I have added some time ristrictions for your golfed code. Jan 29, 2022 at 17:33
• Maybe add some 69 cases?
– l4m2
Jan 29, 2022 at 17:35

# Python NumPy, 245 bytes

def f(A,d="RL",e="DU"):
for x in r_[:A[0]]:
B=A[c_[[2,8,6,0],[1,5,7,3]]]-1;p="";m=A[4];t=m==sum(diff(B));B[3,0]-=x
for b,c in B:q,r=d;y=min(c-x,b);p+=x*d+q+(u:=c-x-y)*e+y*d+q;x=b-y;d=e;e=r+q;m-=u;t&=y>-1<m
if t:return p
from numpy import*

Attempt This Online!

Not very golfed yet; apologies. This takes a flat array as input and returns a path or None if there is none.

#### How?

It essentially walks the boundary clockwise stepping back and forth as needed to collect all the coins using the centre as a reservoir.

If we label the blocks

a+1 b+1 c+1
d+1  e  f+1
g+1 h+1 i+1


then we are looking for a nonnegative solution to

 a = a1+a2
c = c1+c2
g = g1+g2
i = i1+i2
e = e1+e2+e3+e4
b = a2+e1+c1
f = c2+e2+i1
h = i2+e3+g1
d = g2+e4+a1

• +1, Beautiful solution, didn't even think in this direction. Feb 1, 2022 at 5:39