GithubHelp home page GithubHelp logo

knightspath's Introduction

Knight's Path

Gustavo H. Alves Pereira

Introduction

The Knight's Path is a traditional computer science problem, usually employed to teach backtracking. It consists in a chess Knight on a chessboard trying to cover all the squares on the board from a starting point without stepping more than once at each square. The knight has a 'L' shaped movement: any valid combination of two squares on the vertical/horizontal plus one square in the complimentary vertical/horizontal. The following diagram shows it:

  .A.B.         Fig. 1: Possible movements for the Knight (K) on a chessboard
  H...C
  ..K..
  G...D
  .F.E.

From the Knight's starting point at 'K' it can move to the 8 possible points marked as A, B, ..., H.

Algorithm

The algorithm I'm going to implement here is really naive. I am going to emulate a knight starting from the chessboard's top-left corner (in matrix notation, (0,0)) and then trying to move. Each movement will be registered as the corresponding letter on an array. At each iteration of the algorithm it will be checked either the next movement is valid within the board's limits or if it steps on a previously visited square. The next movement always start trying to go to position 'A' according to Fig. 1, then if not available to 'B', and so on. If it is already on 'H' and not valid the backtracking step will be activated. The current movement will be erased from the array and the previous one will be the new current one. It will be updated and then the algorithm will continue as planned. It has two stopping conditions:

  1. When all squares are covered
  2. When all possibilites are exhausted

Condition 1 means all squares are checked and the Knight could traverse all board without needing to repeat a square. Condition 2 means it was impossible to do it.

Programwise, condition 1 is checked simply when the movement array is fully covered, and condition 2 when in the first position the program tries to go to a movement after 'H'.

Build instructions

The program can be built by using make. Alternatively it can be built by using Docker. Currently the only thing that works are the tests, that are the default operation specified in the Dockerfile. The execution is as follows: docker build -t knightspath .

knightspath's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.