Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 need help with Python code with best performance Jafar is playing checkers with his friend Aladdin. Checkers is a board game in which both

1 need help with Python code with best performance
Jafar is playing checkers with his friend Aladdin. Checkers is a board game in which both players take turns to move their pawns. Jafar has just one pawn left, and he is going to take a final turn, beating as many of Aladdin's pawns as possible.
Pawns in checkers move diagonally. The pawn always moves one step in the up-right or up-left direction. If Jafar's pawn moves and its target field is occupied by one of Aladdin's pawns, Aladdin's pawn can be beaten: Jafar's pawn leaps over Aladdin's pawn, taking two steps in the chosen direction and removing Aladdin's pawn from the board. Jarar can beat Aladdin's pawn in this way only when the field beyond Aladdin's pawn is empty.
After beating Aladdin's pawn, Jafar can continue his turn and make another move, but only if he will again beat another one of Aladdin's pawns. Of course, after this additional move, Jafar can continue his turn again by beating another of Aladdin's pawns, and so on for as long as there are further pawns to beat. When it is no longer possible to beat one of Aladdin's pawns, Jafar's turn ends.
For example, in the situation depicted below (where Jafar's pawn is white and Aladdin's pawns are black):
Jafar can beat one of Aladdin's pawns by moving his pawn two steps ir the up-right direction, and then he can make an additional move, beatin another of Aladdin's pawns in the up-left direction. After this move ther are no more of Aladdin's pawns available to beat, so Jafar's turn ends.
In the following situations:
Jafar cannot beat Aladdin's pawn. In the first case, the field two steps in the up-right direction from Jafar's pawn (white) is occupied by one of Aladdin's pawns (black); in the second case, this field is placed outside the board; in the third case, Jafar's pawn cannot move directly upwards; and finally, in the fourth case, Jafar's pawn cannot move in the downright direction.
What is the maximum number of pawns owned by Aladdin that Jafar can beat in his turn?
Write a function:
def solution(B)
which, given a square board of NN size describing Aladdin's and Jafar's pawns, returns the maximum number of pawns Jafar can beat in one turn. If none of Aladdin's pawns can be beaten, the function should return 0.
Jafar's pawn is described by the '0' character, Aladdin's pawns by 'x' sharanters and omntv fiplds hv''(dnts) The hnard is descrihed from
nanges saved
Jafar's pawn is described by the '0' character, Aladdin's pawns by 'x' characters and empty fields by '.'(dots). The board is described from top to bottom and from left to right.
For example, given:
B[0]=dots X dots
B[1]=dots...
B[2]=dots..
B[3]=.X dots.
B[4]=dotsx.x.
B[5]=dots0dots
the function should return 2(Jafar can beat Aladdin's pawn in the upright direction and then another one in the up-left direction).
Given:
B[0]=X dots...
B[1]=.xdots
B[2]=dots0dots
B[3]=dots..
B[4]=dots..
the function should return 0.
Assume that:
N is an integer within the range [1..30];
all strings in B are made only of the following characters: ".","X" and/or "O";
the board is NN square;
the board contains exactly one pawn owned by Jafar.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

Why do you want to be a clinical psychologist?

Answered: 1 week ago

Question

Do you suggest Lisa use a PEO? Why?

Answered: 1 week ago