Advertisement
triclops200

PathGen

Apr 22nd, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.31 KB | None | 0 0
  1. Edge = Struct.new(:nd,:ed)
  2. class Node
  3.     attr_accessor :visited,:cons,:st,:bad
  4.     def initialize(st)
  5.         @st = st
  6.         @cons = []
  7.         @visited = Hash.new(false)
  8.     end
  9.     def addlink(obj)
  10.         @cons << obj
  11.     end
  12. end
  13. t = gets.chomp
  14. a = t.split ""
  15. a.uniq!
  16. t = gets
  17. b = t.to_i - 1
  18. x = (a*b).combination(b).to_a.uniq
  19. nmap = {}
  20. x.each do |el|
  21.     nmap[el.join","] = Node.new(el.join",")
  22. end
  23. x.each do |el|
  24.     a.each do |it|
  25.         nmap[el.join","].addlink(Edge.new(nmap[(el[1..-1] << it).join","],it))
  26.     end
  27. end
  28.  
  29. visited = Hash.new(false)
  30. notvisited = nmap.values
  31. path = []
  32. currp = []
  33. edges = []
  34. curredges = []
  35. index = 0
  36. cnode = nmap[x[0].join","]
  37. currp << cnode
  38. while notvisited.length > 0
  39.     e = true
  40.     nnode = nil
  41.     if visited[cnode]
  42.     end
  43.     cnode.cons.each do |con|
  44.         if not cnode.visited[con]
  45.             cnode.visited[con] = true
  46.             nnode = con.nd
  47.             currp << nnode
  48.             curredges << con
  49.             e = false
  50.             break
  51.         end
  52.     end
  53.     if e
  54.         visited[cnode] = true
  55.         notvisited.delete cnode
  56.         path[index,0]=currp.clone
  57.         edges[index,0]=curredges.clone
  58.         index = nil
  59.         i = -1
  60.         while index.nil?
  61.             i+=1
  62.             index = path.index(notvisited[i])
  63.             if i > notvisited.length
  64.                 break
  65.             end
  66.         end
  67.         cnode = notvisited[i]
  68.         currp = []
  69.         curredges = []
  70.         next
  71.     end
  72.     cnode = nnode
  73. end
  74. edges.each do |edge|
  75.     print edge.ed
  76. end
  77. puts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement