- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of expected moves required to win Lotus and Caterpillar game in Python

Suppose we have a grid with n rows and m columns. Amal and Bimal are playing a game on that grid. The game rule is like below −

Amal places white lotus tile somewhere at the top row and Bimal places a caterpillar tile somewhere on the bottom row. Amal starts the game and they are playing alternatively. Amal can move his tile to any of the 8 adjacent cells inside the grid of the current cell, but Bimal's caterpillar tile can only move left or right inside the grid, or stay at the same position. Amal's goal is to catch Bimal using as few moves as possible, while Bimal (with the caterpillar tile) has to survive for as long as possible. If they randomly select two columns to put their lotus and caterpillar, then we have to find expected number of moves required to win this game by Amal.

So, if the input is like n = 5 m = 7, then the output will be 4.571428571428571.

To solve this, we will follow these steps −

- r := 0
- for l in range 0 to m - 1, do
- temp := n - 1.0
- if l >= n, then
- temp := temp + (l - n + 1) * ((l - 1) / m)

- if l < m - n, then
- temp := temp + (m - n - l) * ((m - l - 2) / m)

- r := r + temp / m

- return r

## Example

Let us see the following implementation to get better understanding −

def solve(n, m): r = 0 for l in range(m): temp = n - 1.0 if l >= n: temp += (l - n + 1) * ((l - 1) / m) if l < m - n: temp += (m - n - l) * ((m - l - 2) / m) r += temp / m return r n = 5 m = 7 print(solve(n, m))

## Input

5, 7

## Output

4.571428571428571

- Related Articles
- Program to find number of moves to win deleting repeated integer game in Python
- Program to find number of possible moves to start the game to win by the starter in Python
- Minimum Players required to win the game in C++
- Program to find expected number of shuffle required to sort the elements of an array in Python
- Program to find out if we win in a game in Python
- Program to find out the minimum moves in a snakes and ladders game in Python
- Program to find winner of number reducing game in Python
- Program to check whether Amal can win stone game or not in Python
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to check whether first player win in candy remove game or not in Python?
- Number of moves required to guess a permutation in C++
- Program to find number of swaps required to sort the sequence in python
- Program to find number of given operations required to reach Target in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find winner of stone game in Python