path 449 B

123456789101112131415161718192021222324
  1. /* Report the distance from src = ARGV[0] to dst = ARGV[1]
  2. */
  3. BEG_G {
  4. int dist[node_t];
  5. node_t n, curn;
  6. node_t src = node($G, ARGV[0]);
  7. node_t dst = node($G, ARGV[1]);
  8. $tvroot = src;
  9. $tvtype = TV_bfs;
  10. }
  11. N {
  12. curn = $;
  13. if ($ == dst) {
  14. printf ("dist from %s to %s is %d\n", src.name, dst.name, dist[dst]);
  15. exit(0);
  16. }
  17. }
  18. E {
  19. if ($.head == curn) n = $.tail;
  20. else n = $.head;
  21. if (dist[n] == 0) dist[n] = dist[curn]+1;
  22. }