0

I am doing the Knight move Problem for my class. My codes (Python 3) are like this. But when I run, I get this error. I think the values are not going in the list. What should I do? Help me!!!

This is the question:

Find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once.

Write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a..h) representing the column and a digit (1..8) representing the row on the chessboard.

Output

For each test case, print one line saying ‘To get from xx to yy takes n knight moves.’.

Sample Input

e2 e4

a1 b2

b2 c3

a1 h8

a1 h7

h8 a1

b1 c3

f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.

To get from a1 to b2 takes 4 knight moves.

To get from b2 to c3 takes 2 knight moves.

To get from a1 to h8 takes 6 knight moves.

To get from a1 to h7 takes 5 knight moves.

To get from h8 to a1 takes 6 knight moves.

To get from b1 to c3 takes 1 knight moves.


My error:

Traceback (most recent call last):
 File "C:/Users/Faijiuy/Documents/Python Scripts/Knight Move/knight_moves.py", line 60, in <module>
    s = chess_board.pop(0)
IndexError: pop from empty list

My Codes:

import sys,copy

N = 8
pos = input().split()
kn = list(pos[0])
ta = list(pos[1])
 

colChar = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
for i in range(8):
    if kn[0] == colChar[i]:
        kn[0] = i

    if ta[0] == colChar[i]:
        ta[0] = i

knightpos = list(map(int, kn))
targetpos = list(map(int, ta))


class state():
    def __init__(self, c, r, dist):
        self.r = r
        self.c = c
        self.dist = dist

def isValid(c, r):
    if(c >= 1 and c < N and r >= 1 and r > N):
        return True
    return False

Adj = [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)]  

def successor(u):

    succ = []
    for relativePos in Adj:
        v = copy.deepcopy(u)
        v.c += relativePos[0]
        v.r += relativePos[1]
        v.dist += 1
        if isValid(v.c, v.r):
            succ.append(v)
            board[v.c][v.r] = v.dist
    return succ

def goal(u):
    if (u.c == targetpos[0] and u.r == targetpos[1]):
        return True
    else:
        return False

s = state(knightpos[0], knightpos[1], 0)
chess_board = []

while not goal(s):
    for m in successor(s):
        chess_board.append(m)
    print(chess_board)
    s = chess_board.pop(0)

print(s.dist)


false
  • 10,264
  • 13
  • 101
  • 209
faijiuy
  • 55
  • 1
  • 9
  • I ran your code with `e2 e4` as input and won't raise an error – Brad Figueroa Aug 26 '20 at 02:38
  • But, i try to run it with e2 e4, it still get the same error even in cmd. Where should i put the input instead to make the value go into the list or make what ever compile go in the list? – faijiuy Aug 26 '20 at 02:49

1 Answers1

0

Line 28

if(c >= 1 and c < N and r >= 1 and r > N):
    return True

Is that should be:

if(c >= 1 and c < N and r >= 1 and r < N):
    return True

I change that line and comment out line 44 (which will cause a undefined argument NameError), the result is 2.

Keijack
  • 778
  • 6
  • 12
  • But there still some test case that took a long time to finish running like a1 h8 or g5 h8. What can I do to the code to improve that? – faijiuy Aug 27 '20 at 02:03