Knight's Shortest Path on Chessboard

后端 未结 16 1286
情深已故
情深已故 2020-11-30 16:42

I\'ve been practicing for an upcoming programming competition and I have stumbled across a question that I am just completely bewildered at. However, I feel as though it\'s

16条回答
  •  离开以前
    2020-11-30 17:05

    You have a graph here, where all available moves are connected (value=1), and unavailable moves are disconnected (value=0), the sparse matrix would be like:

    (a1,b3)=1,
    (a1,c2)=1,
      .....
    

    And the shortest path of two points in a graph can be found using http://en.wikipedia.org/wiki/Dijkstra's_algorithm

    Pseudo-code from wikipedia-page:

    function Dijkstra(Graph, source):
       for each vertex v in Graph:           // Initializations
           dist[v] := infinity               // Unknown distance function from source to v
           previous[v] := undefined          // Previous node in optimal path from source
       dist[source] := 0                     // Distance from source to source
       Q := the set of all nodes in Graph
       // All nodes in the graph are unoptimized - thus are in Q
       while Q is not empty:                 // The main loop
           u := vertex in Q with smallest dist[]
           if dist[u] = infinity:
              break                         // all remaining vertices are inaccessible from source
           remove u from Q
           for each neighbor v of u:         // where v has not yet been removed from Q.
               alt := dist[u] + dist_between(u, v) 
               if alt < dist[v]:             // Relax (u,v,a)
                   dist[v] := alt
                   previous[v] := u
       return dist[]
    

    EDIT:

    1. as moron, said using the http://en.wikipedia.org/wiki/A*_algorithm can be faster.
    2. the fastest way, is to pre-calculate all the distances and save it to a 8x8 full matrix. well, I would call that cheating, and works only because the problem is small. But sometimes competitions will check how fast your program runs.
    3. The main point is that if you are preparing for a programming competition, you must know common algorithms including Dijkstra's. A good starting point is reading Introduction to Algorithms ISBN 0-262-03384-4. Or you could try wikipedia, http://en.wikipedia.org/wiki/List_of_algorithms

提交回复
热议问题