hello:
The following is my task:
The local commuter railroad services a number of towns in
Kiwiland. Because of monetary concerns, all of the tracks are
'one-way.'That is, a route from Kaitaia to Invercargill does not imply
the
existence of a route from Invercargill to Kaitaia. In fact, even if
both of these routes do happen to exist, they are distinct and are not
necessarily the
same distance!
My task is how to computer the number of trips between two station
with exactly some stops,for example ,A to C with exactly 4stops. In the
sample data below, there are three such trips: A to C (via B,C,D); A
to C (via D,C,D); and A to C (via D,E,B).
For the test ,the towns are named using the first few letters of the
alphabet from A to D. A route between two towns (A to B) with a
distance of 5 is represented as AB5.
Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7
I have design my method,but it works only for some situation.For
example,the route number for A to E with exactly x stops:the result for
1 or 2 or 3 or 4 stops is right,but for 5 or 6 or…,the result is
wrong,they all are zero:0.
The following are my methods.By the way,my model is Route in which i
can get the route_name,distance,start_station,end_station.For example,AE
route have the
attribute:route_name=“AE”,distance=5,start_station=“A”,end_station=“B”.The
Route model works fine for my other method,so i did not paste the model
code here.Please help me to find where are wrong?Thank you very much!
def exa_stops #this
@stops=params[:exastops].to_i
if @stops==1
onestop_route=route_with_onestop(params[:ss1],params[:es1])
if onestop_route
@exa_stops_number=1
else
@exa_stops_number=0
end
else
l=exa_with_stops(params[:ss1],params[:es1],@stops).length
@exa_stops_number=l
end
end
private
def find_route(s1,s2)
start_routes=Route.find_all_by_start_station(s1)
end_routes=Route.find_all_by_end_station(s2)
middle_array=Array.new
for s in start_routes
for e in end_routes
middle_array<<s.name+e.name
end
end
pair_group=middle_array
if pair_group.length !=0
for p in pair_group
pair_group.delete§ if p[2,1]==“A”
end
return pair_group
end
end
def route_with_onestop(s1,s2)
Route.find_by_name(s1+s2)
end
def exa_with_stops(s1,s2,esn)
if esn==2 || esn==3
@exastops_array=Array.new
@pair_group=find_route(s1,s2)
for p in @pair_group
if p[1,1]==p[2,1]
@exastops_array<<2
end
if Route.find_by_name(p[1,1]+p[2,1])
@exastops_array<<3
end
@part_of_exastops_array=Array.new
@exastops_array.each {|e| @part_of_exastops_array<<e if e==esn}
end
else
@n=1
@exastops_array=Array.new
@pair_group=find_route(s1,s2)
@middle_array=@pair_group
@exaroutes_array=Array.new
if (esn%2)==0
@i=esn/2
else
@i=(esn-1)/2
end
while(@n<@i)
for m in @middle_array
@exaroutes_array=@exaroutes_array+find_route(m[1,1],m[2,1])
end
for e in @exaroutes_array
if e[1,1]==e[2,1]
s=2+2*@n
@exastops_array<<s
end
if Route.find_by_name(e[1,1]+e[2,1])
s=3+2*@n
@exastops_array<<s
end
end
@middle_array=@exaroutes_array
@exaroutes_array.clear
@n+=1
end
if @n==@i
@part_of_exastops_array=Array.new
@exastops_array.each {|e| @part_of_exastops_array<<e if e==esn}
end
end
return @part_of_exastops_array
end