Good evening. Thank you for your continued support.
I have seen various opinions of experts, but It was quite difficult. It's probably a matter of my understanding, so I can't help it.
Let's do it first. Try moving the two disks from the left edge to the right edge. Here, the expressions at the right end, center, and left end are subtle, so Allocate a number. Left edge: 1, center: 2, right edge: 3. I wrote it, The disk moved to the right and where from where It has been moved or supplemented.
I recursively captured the factorial in the previous article Can it be diverted? Let's take a look.
cure.py
def test(n:int=3):
if n>1:
return n * test(n-1)
else:
return 1
As for what I want to do. ..
I want to move two disks from 1 to 3
Move the number 1 disk from 1 to 2 and then
Move the number 2 disk from 1 to 3 and
Move the number 1 disk from 2 to 3.
Make the number and sheet common as n, Let's move from x to y. Again, here is the image.
2 sheets(n)Disk of 1(x)From 3(y)I want to move to
No. 1(n)Disk of 1(x)From 2(y)After moving to
No. 2(n)Disk of 1(x)From 3(y)Move to
No. 1(n)Disk of 2(x)From 3(y)Move to.
Therefore, it seems good to start with def test (n, x, y).
2 sheets(n)Disk of 1(x)From 3(y)I want to move to=> def test(n , x , y):
No. 1(n)Disk of 1(x)From 2(y)After moving to
No. 2(n)Disk of 1(x)From 3(y)Move to
No. 1(n)Disk of 2(x)From 3(y)Move to.
Next. After starting from test (2,1,3), next Must be configured to pass test (1,1,2). test(1,1,?) How do you make the last? Recall that each axis was numbered. Since 1 + 2 + 3 = 6, 6-x-y can be used to express?.
2 sheets(n)Disk of 1(x)From 3(y)I want to move to=> def test(n , x , y):
No. 1(n)Disk of 1(x)From 2(y)After moving to=> test(n-1,x ,6-x-y)
No. 2(n)Disk of 1(x)From 3(y)Move to
No. 1(n)Disk of 2(x)From 3(y)Move to.
If the above is left as it is, the state of n == 0 will occur. 0 disk? 0 disk? It seems to be a little broken, so Let's make a condition with an if statement. If n> 1, then You can avoid the condition of n == 0.
2 sheets(n)Disk of 1(x)From 3(y)I want to move to=> def test(n , x , y):
No. 1(n)Disk of 1(x)From 2(y)After moving to=> if n>1:
test(n-1,x ,6-x-y)
No. 2(n)Disk of 1(x)From 3(y)Move to
No. 1(n)Disk of 2(x)From 3(y)Move to.
Considering by substituting with test (1,1,2), the above configuration I'm not addicted to if n> 1, so pass. At that stage, it is OK if you can display "Move the 1st (n) disk from 1 (x) to 2 (y)" using print.
2 sheets(n)Disk of 1(x)From 3(y)I want to move to=> def test(n , x , y):
No. 1(n)Disk of 1(x)From 2(y)After moving to=> if n>1:
test(n-1,x ,6-x-y)
print(f"{n}To{x}=>{y}What")
No. 2(n)Disk of 1(x)From 3(y)Move to
No. 1(n)Disk of 2(x)From 3(y)Move to.
It's a little difficult to explain, test (1,1,2) is equal to print (f "{1} to {1} => {2}"). After exiting it, print (f "{2} to {1} => {3}") at test (2,1,3) You will be connected automatically. What about the last "Move the 1st (n) disk from 2 (x) to 3 (y)"? I tried it like this.
test.py
def test(n,x,y):
if n>1:
test(n-1,x,6-x-y)
print(f"{n}To{x}=>{y}What")
if n>1:
test(n-1,6-x-y,y)
Well, I'm thinking about explaining it (laughs).
Recommended Posts