Please help me answer this coding question. You can write the code in either Pyt
ID: 3754496 • Letter: P
Question
Please help me answer this coding question. You can write the code in either Python3 or Java8 ; whichever one is easier and faster but please comment the code. Please also provide screenshots of your output after entering the inputs; it should match the sample inputs and outputs in the question. Thank You!
In chess, the knight is one of the more interesting pieces. As illustrated below, it moves by jumping two spaces up or down and one to the left or right. Alternatively, it can jump two spaces to the left or right and one space up or down. This lets the knight cover a lot of chess board in just a few moves. No matter where the knight starts out, it can eventually reach any other space 6 Given a chess board with just one knight on it, you want to find the hiding places, the spaces that it would take the knight the most jumps to reach from its initial location. For example, it would take the knight at least two jumps to reach space a8 in the board above. However, it would take at least three jumps for it to reach ci. Since there are other spaces that it would take the knight even more jumps to reach, neither a8 nor ci are among the hiding places for this initial position of the knight. Input Input begins with a line containing an integer 1Explanation / Answer
Answer:
def getPosition(initialPostion):
xchar = initialPostion[0]
yint = int(initialPostion[1]);
xint = ord(xchar) - 97;
return 8- yint,xint
def isPositionPossible(x ,y):
if x>=0 and x<=7 and y>=0 and y<=7:
return True
return False
def runAllJumps(ChessBoard, x, y):
currentsteps = ChessBoard[x][y]
#For x+2,y-1
nx, ny = x+2, y-1
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x+2,y+1
nx, ny = x+2, y+1
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x-2,y-1
nx, ny = x-2, y-1
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x-2,y+1
nx, ny = x-2, y+1
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x+1,y+2
nx, ny = x+1, y+2
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x-1,y+2
nx, ny = x-1, y+2
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x+1,y-2
nx, ny = x+1, y-2
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
#For x-1,y-2
nx, ny = x-1, y-2
if isPositionPossible(nx, ny):
if currentsteps + 1 < ChessBoard[nx][ny]:
ChessBoard[nx][ny] = currentsteps + 1
runAllJumps(ChessBoard, nx, ny)
return ChessBoard
def convertToChessPostion(i, j):
position = str(chr(j+97))
position = position + str(8-i)
return position
def determineHidingPlaces(initialPostion):
# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 8;
maxsteps = 1000
ChessBoard = [[maxsteps for x in range(w)] for y in range(h)]
x, y = getPosition(initialPostion)
# set Initial position
ChessBoard[x][y] = 0;
# Run the horse for all possibilites of jump
ChessBoard = runAllJumps(ChessBoard, x,y)
#determine steps to reach hiding places
steps = -1;
for i in range(0,8):
for j in range(0,8):
steps = max(steps, ChessBoard[i][j])
#uncomment to debug
#print ChessBoard
#determine positions to reach hiding places
list = []
for i in range(0,8):
for j in range(0,8):
if steps == ChessBoard[i][j]:
chessPosition = convertToChessPostion(i, j)
list.append(chessPosition)
#uncomment to debug
#print ChessBoard
return steps, list
'''
Description: Controller method to get input and determine ouptut for knight hiding places problem
'''
def main():
testCases = int(raw_input())
while testCases > 0:
# get Intial position
initialPostion = raw_input()
#Determin knight hiding places
distance, list = determineHidingPlaces(initialPostion)
#print distance of hiding places
print distance,
#print hiding places
for position in list:
print position,
testCases = testCases - 1
'''
Program Entery point
'''
if __name__ == "__main__":
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.