ICode国际青少年编程竞赛- Python-6级训练场-多重递归
1、
def move(a, b):
if a > 12:
return
Dev.step(a)
Dev.turnRight()
if b < 4:
move(a, b+1)
else:
move(a+2, 1)
move(2, 1)
2、
def move(a, b):
if a < 2:
return
if b == 1: Spaceship.step(2)
Dev.step(a)
Dev.turnRight()
Dev.step(a)
if b < 4:
move(a, b+1)
else:
move(a-2, 1)
move(6, 1)
3、
def move(a, b):
if b == 0:
return
Dev.step(a)
if a > 0:
Dev.turnRight()
elif a != 0:
Dev.turnLeft()
Dev.step(a)
if a > 1:
Dev.turnLeft()
elif a != 0 and a != 1:
Dev.turnRight()
if a > b:
move(a-1, b)
else:
move(-a-1, b+1)
move(4, -4)
4、
def move(a, b):
if b < 3: return
if a > 0:
if a == b:
Dev.step(a)
Dev.step(a)
if a > 1:
Dev.turnRight()
move(a-1, b)
elif a < 1 and a > -b:
Dev.step(a-1)
Dev.turnLeft()
move(a-1, b)
else:
Dev.step(a-1)
Dev.turnRight()
a = b - 1
b = a
move(a, b)
move(5, 5)
5、
def move(a, b):
if b > 4:
return
if a == 5:
Dev.step(b)
if a >= 2:
Dev.step(a)
if a > 2:
Dev.turnRight()
if a == 2:
a = -1
move(a-1, b)
elif a >= -5:
Dev.step(a)
if a != -5:
Dev.turnLeft()
move(a-1, b)
else:
Dev.step(-b)
Dev.turnLeft()
move(5, b+1)
move(5, 1)
6、
def move(a):
Dev.step(a)
Dev.turnLeft()
Dev.step(a-1)
Dev.step(1-a)
Dev.turnRight()
Dev.step(-a)
Spaceship.step()
Spaceship.turnLeft()
Spaceship.step()
Spaceship.turnRight()
Spaceship.step()
if a < 5:
move(a+1)
Dev.step()
move(2)
7、
def move(a, b):
if b == 0:
Dev.step(5)
Dev.step(a)
Dev.turnRight()
if b < 2:
move(a, b+1)
if b == 2:
Dev.step(a-5)
Dev.turnRight()
if a < 4:
move(a+1, 0)
move(2, 0)
8、
def move(a, b):
if a == 0:
Spaceship.step(4)
Spaceship.turnRight()
Dev.step()
Dev.step(2)
Dev.turnRight()
if a < 3:
move(a+1, b)
if a == 3:
Dev.step(-1)
Spaceship.step(4)
Spaceship.turnLeft()
if b < 2:
move(0, b+1)
Dev.turnLeft()
move(0, 0)
9、
def move(a, b):
if b == 0:
Spaceship.step(5)
Spaceship.turnRight()
Spaceship.step()
Dev.turnRight()
Dev.step()
Dev.step(3)
Dev.turnRight()
if b < 3:
move(a, b+1)
if b == 3:
Dev.step(-1)
Spaceship.turnRight()
Spaceship.turnRight()
Spaceship.step()
Spaceship.turnLeft()
Spaceship.step(5)
Spaceship.turnLeft()
if a < 3:
move(a+1, 0)
move(0, 0)
10、
def move(a, b, c):
if (b-a) == 2:
Dev.step(a)
Dev.step(b)
if c == 0:
Dev.turnRight()
if c == 1 and b < a + 1:
Dev.turnLeft()
if c == 0 and b > 1:
move(a, b-1, 0)
if c == 1 and b < a + 1:
move(a, b+1, 1)
if c == 0 and b == 1:
Dev.turnRight()
move(a, b, 1)
if c == 1 and b == a + 1 and a > 0:
move(a-1, b, 0)
move(4, 6, 0)
11、
def move(a, b, c):
if a == 0:
Dev.step(2)
Dev.step(b)
if b == 2:
Dev.turnLeft()
if b == 4:
Dev.turnRight()
if b < 4 and a < 2:
move(0, b+2, 0)
if b < 4 and a == 2:
move(0, b+2, 1)
if a < 2:
move(a+1, b, c)
if c == 0 and a == 2 and b == 4:
Dev.step(b)
Dev.turnLeft()
Dev.step(2)
if c == 1 and a == 2 and b == 4:
Dev.step(b-1)
move(0, 2, 0)
12、
def move(a, b, c):
if a == 0: Dev.step(-3)
Dev.step(-b)
Dev.turnLeft()
if b > 2 and a < 2:
move(0, b-2, 0)
if b > 2 and a == 2:
move(0, b-2, 1)
if a < 2:
move(a+1, b, c)
if a == 2 and b == 2:
Dev.step(-a)
if c == 0:
Dev.turnRight()
Dev.step(-3)
move(0, 4, 0)
13、
def move(a, b):
if a == 0:
Dev.step()
Dev.step(2)
Dev.turnRight()
if a < 3:
move(a+1, b)
if a == 3:
Dev.step(-1)
Dev.turnLeft()
Spaceship.step(b)
if b < 11:
Spaceship.turnLeft()
move(0, b+3)
move(0, 1)
14、
def move(a, b, c):
if a == 0:
Spaceship.step(b)
Dev.step(c)
Dev.turnLeft()
Dev.step(c)
if a < 3:
move(a+1, b, c)
if a == 3 and c > 1:
move(0, b+1, c-2)
move(0, 1, 5)
15、
def move(a, b):
if a == b:
Spaceship.step(b)
Spaceship.turnRight()
Spaceship.step(b)
Spaceship.turnLeft()
Dev.step(a)
if a > 1:
Dev.turnLeft()
move(a-1, b)
if a < 0 and a > -b:
Dev.turnRight()
if a == 1:
move(-1, b)
if a < 0 and a > -b:
move(a-1, b)
if a == -b:
move(b-1, b-1)
move(4, 4)
16、
def move(a, b):
if a == 1:
Spaceship.step(2)
Spaceship.turnRight()
Spaceship.step(2)
Dev.step(a)
Dev.step(-a)
if a < 3:
move(a+1, b)
if a == 3 and b < 3:
Spaceship.turnRight()
Spaceship.turnRight()
Spaceship.step(6)
Spaceship.turnRight()
Spaceship.step(3)
Spaceship.turnLeft()
Dev.turnLeft()
if b < 4:
move(1, b+1)
move(1, 0)
17、
def move(a, b):
Spaceship.step(b)
Dev.step(a)
Dev.step(-a)
if a < 3:
move(a+1, b)
if a == 3:
Spaceship.turnLeft()
Dev.turnLeft()
if b < 4:
move(1, b+1)
move(1, 2)
18、
def move(a, b):
Dev.step(a)
Dev.step(-a)
Spaceship.step(2)
if a < 3:
move(a+1, b)
if a == 3 and b < 2:
Spaceship.turnRight()
Dev.turnRight()
move(1, b+1)
move(1, 0)
19、
def move(a, b):
if a == 0:
Spaceship.turnRight()
if b < 7 and b > 3:
Dev.turnRight()
Spaceship.step(b)
if b > 2 and b < 7:
if a == 0:
Dev.step(-1)
Dev.step(-2)
Dev.turnRight()
if a < 3:
move(a+1, b)
if a == 3:
Dev.step(1)
if b < 7:
move(0, b+1)
elif b < 7:
move(0, b+1)
move(0, 2)
20、
def move(a, b, c):
if c == 0:
Spaceship.turnLeft()
Spaceship.step(b)
Dev.step(3)
Dev.step(a)
Dev.turnRight()
if c < 3:
move(a, b, c+1)
if c == 3:
Dev.step(-3)
Dev.turnLeft()
if b > 2:
move(a+1, b-2, 0)
move(2, 8, 0)