CCC '10 J5 - Knight Hop

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 64M

Problem type
Canadian Computing Competition: 2010 Stage 1, Junior #5

Below is an 8 \times 8 chessboard on which we will designate square locations using the ordered pairs as indicated. For example, notice that piece A is at position (2, 2) and piece B is at position (4, 3).


A knight is a special game piece that can leap over other pieces, moving in the "L" pattern. Specifically, in the diagram below, K represents the knight's starting position and the numbers 1 through 8 represent possible places the knight may move to.


Your program will read the starting location of the knight and output the smallest number of jumps or moves needed to arrive at a location specified in the second input.

Input Specification

Your program will read four integers, where each integer is in the range 1 \dots 8. The first two integers represent the starting position of the knight. The second two integers represent the final position of the knight.

Output Specification

Your program should output the minimum (non-negative integer) number of moves required to move the knight from the starting position to the final position. Note that the knight is not allowed to move off the board during the sequence of moves.

Sample Input 1

2 1
3 3

Output for Sample Input 1


Sample Input 2

4 2
7 5

Output for Sample Input 2



  • 1
    Pimienta  commented on Sept. 9, 2021, 7:28 p.m.

    I'm getting RTE only in the first case ...

  • 10
    billsboard  commented on Nov. 28, 2020, 1:34 p.m.

    A chess knight will take at most 5 moves to reach a given square. I don't know if that bit of chess knowledge can be helpful for this problem.

    • 4
      Dav_Did  commented on April 18, 2021, 11:06 p.m.

      Good to know I won't need to make a recursion that loops like 50 times without TLE.

    • 4
      aegirwang  commented on Dec. 11, 2020, 11:31 a.m.

      It's actually 6 in some cases, such as from corner to corner.

      • 4
        billsboard  commented on Jan. 8, 2021, 8:29 p.m.

        Ah right, it is 6 moves. Sorry.

    • -1
      Evanhyd  commented on Dec. 1, 2020, 12:36 a.m.

      It helps you to become a grandmaster, but in reality, it has absolutely nothing to do with this question.

      • 3
        billsboard  commented on Dec. 1, 2020, 9:29 p.m.

        I was thinking you could pass the problem by setting an exit condition on recursion to be 5, instead of using a visited cache which is what I assume the problem intended.

  • 5
    sjay05  commented on June 15, 2020, 3:44 p.m. edited

    Using std::queue with pair<int,int>: still MLE's.

    Edit: For comment below.

    • 1
      kresteodymium  commented on June 23, 2021, 5:03 p.m.

      I believe I did the same thing and my program passed without MLEing. I'm not sure why yours did not pass :(.

      • 1
        Badmode  commented on June 29, 2021, 2:07 p.m.

        If there was no memory limit their code would of TLE instead of MLE on test case #6 because of an infinite loop. See Jonathan's comment down below.

    • 1
      vishnus  commented on May 21, 2021, 9:39 p.m.


    • 1
      kevze  commented on June 15, 2020, 3:52 p.m.

      One mb ://

  • 0
    kevze  commented on June 15, 2020, 12:32 p.m. edited

    I mle by one mb on case 6 :(. Any optimization tips?

    • 0
      BreadTurtle  commented on June 15, 2020, 1:58 p.m. edit 3

      instead of making separate queues per dimension try using pairs to reduce the number of possible queue entries.

      • 0
        kevze  commented on June 15, 2020, 3:51 p.m.

        A queue of pair<int,int> still mle's. Maybe my BFS algorithm is inefficient? Best solutions only use like 1-2 mb.

        • 4
          Jonathan_Uy  commented on June 15, 2020, 4:14 p.m. edit 2

          Your program enters an infinite loop for the test case:

          3 1
          1 3

          • 1
            kevze  commented on June 15, 2020, 4:33 p.m.

            Thanks! I got it now!

        • 0
          BreadTurtle  commented on June 15, 2020, 4:08 p.m.

          Then it is most likely that you have a logical error/edge case you did not account for. The runtime for that case took abnormally long as well. I would advise you to look over the semantics.

          • 0
            kevze  commented on June 15, 2020, 4:35 p.m. edit 3

            "Then it is most likely that you have a logical error/edge case you did not account for."

            You were right, I had if ( == 0) instead of if ( == 0).


            Edit: edits were glitching idk why

  • 8
    aayushICS  commented on May 7, 2020, 10:45 a.m.

    Did the point total for this question decrease?? I recall this being worth 10 points.

    • 4
      Ninjaclasher  commented on May 8, 2020, 3:30 p.m.

      Yes, the points rewarded for this problems was reduced from 10 points to 7 points to match the other classical graph theory problems.

    • 3
      Potatoritos  commented on May 8, 2020, 3:13 p.m.

      I think

  • 0
    delara3838  commented on Feb. 9, 2019, 11:29 a.m.

    What does IR mean and why do i get it when i submit

  • 0
    349081547  commented on Jan. 4, 2019, 10:43 a.m.

    Which would be better?Using a Queue or recursive?

    • 0
      qiao_yun20060930  commented on May 6, 2021, 12:34 a.m.

      use a queue, it is easy to understand

  • 0
    Pleedoh  commented on Aug. 1, 2017, 9:34 p.m.

    wleung_bvg you're getting it too?

    • 0
      wleung_bvg  commented on Aug. 2, 2017, 1:34 p.m. edited

      Your code has variables that have indeterminate ("random") values. This can sometimes happen when declaring a variable in a non global scope without initializing it with a value. Simply initializing the boolean variable with the value false in your spot struct will allow your code to pass.

      • 0
        Pleedoh  commented on Aug. 2, 2017, 3:37 p.m.

        Ah,thanks. Why does the first case work though?

  • -1
    Pleedoh  commented on Aug. 1, 2017, 11:28 a.m.

    No TLE when I submit in Java. Why does my C++ code get stuck when I submit, but never when I try cases on myself?

  • 0
    Pleedoh  commented on July 31, 2017, 10:36 p.m. edited


    Can anyone tell me why I'm getting TLE? I've tested my code with all the test data, and I get all the right answers. Given the fact that it's an 8 by 8 board, I find it very hard to believe my code is actually too slow. I even used a timer, with all cases under 0.1. Is my code getting stuck with the input or something?

    • -4
      hello12_2015  commented on Aug. 16, 2017, 4:15 p.m.

      TLE means Time Limit Exceeded. time limit has nothing to do with answers, only the format you've coded the solution.

      • -2
        Pleedoh  commented on Aug. 16, 2017, 4:25 p.m.

        I know... My code wasn't too slow, as mentioned above. The struct bool wasn't initialized.

        • -2
          hamdog  commented on April 4, 2020, 7:28 p.m.

          The judges can be wonky at times making your code run abnormally slow.

          • 0
            Jonathan_Uy  commented on April 5, 2020, 10:05 a.m.

            This was not the judges fault, as explained by wleung_bvg's reply. The judges may have been wonky in the past, but have lately been upgraded to provide very stable and also very fast run times.