0

I am doing the Knight move Problem for my class. My codes (Python 3) are like this. I get the right output on my PC But when I submit to Online Judge, I get this result. I think the that input should be multiple line and put it in to 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 submitted result:

enter image description here

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+1

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

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)
    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)
    s = chess_board.pop(0)

print('To get from', pos[0], 'to', pos[1], 'takes', s.dist, 'knight moves.')


What should I change in my solution for it to work correctly in online judge too?

false
  • 10,264
  • 13
  • 101
  • 209
faijiuy
  • 55
  • 1
  • 9
  • 1
    Often in these coding tests, there are few test cases shared to participant. But the evaluation is done based on test cases that are different from the ones that are shared to you. In short, your code satisfies the test cases shared but the solution itself is not correct. In order to find out the bug and fix, we need to solve the problem on your behalf. This might not be a reasonable thing to do since you are taking the test. – nightgaunt Aug 26 '20 at 07:15
  • This is not actually a test. I am doing it as a assignment for the class. – faijiuy Aug 26 '20 at 13:00

0 Answers0