2012-12-11 58 views
0

我試圖列出當前按字母順序排列的狀態,並按路線順序對其進行排序。如何訂購路線狀態列表

作爲實例:路線開始在科羅拉多州和華盛頓結束,我知道它通過俄勒岡州,愛達荷州,猶他州和懷俄明(例如http://goo.gl/maps/j6tyu

按字母順序的路由將是(CO,ID, OR,UT,WA,WY),路線順序將是(CO,WY,UT,ID,OR,WA)

是否有可能通過只知道哪些狀態觸及哪些其他狀態來計算路線順序,我需要哪些其他信息?有沒有建議閱讀鏈接,可以幫助我

我也實際上有每英里旅行的英里,如果有幫助。 ( CO = 90 ,ID = 275 ,OR = 372 ,UT = 149 ,WA = 8 ,WY = 368 )

+0

a)你打算使用什麼平臺 - GIS軟件,數據庫查詢,編程語言? b)你知道嗎,路線中的第一個狀態是什麼? – user1702401

+0

這似乎是相當棘手的任務,在某些情況下,你不能得到單一的結果。例如,路線可以是:阿拉巴馬州田納西州密西西比州阿肯色州或阿拉巴馬州密西西比州田納西州阿肯色州。另一個例子:當你有阿拉巴馬州,田納西州,密西西比州時,你不能說出哪個州的路線開始,哪裏結束。 – user1702401

+0

@ user1702401,我用php開發,正如我在文章中提到的,開始和結束點都是已知的。我開始認爲考慮到鄰接關係,這是不可能的。 –

回答

1

因爲它是有趣的問題,我寫了小JS腳本只是爲了好玩:http://jsfiddle.net/Ufbbt/

var from = 'CO'; 
var to = 'WY'; 
var thru = ['ID', 'OR', 'UT', 'WA']; 

var globalCount = 0; 

var doStuff = function(currState, states, str) { 

    if (states.length == 0) { 
     if (inArray(to, statesDesc[currState])) { 
      console.log(str + '->' + to); 
      globalCount++; 
     } 
     return; 
    } 

    for (var i = 0, len = states.length; i < len; i++) { 
     if (inArray(states[i], statesDesc[currState])) { 
      var newStates = states.slice(0); // clone original array 
      var newCurrState = newStates.splice(i, 1)[0]; 
      doStuff(newCurrState, newStates, str + '->' + newCurrState); 
     } 
    } 
} 

var inArray = function(what, where) { 
    for (var i = 0, len = where.length; i < len; i++) { 
     if (what == where[i]) 
      return true; 
    } 
    return false; 
} 

// Now, initial execute 
doStuff(from, thru, from); 
console.log(globalCount + ' route(s) found'); 

var statesDesc = { 
    AK: ['WA'], 
    AL: ['TN','GA','FL','MS'], 
    AR: ['MO','TN','MS','LA','TX','OK'], 
    AZ: ['UT','CO','NM','CA','NV'], 
    CA: ['OR','NV','AZ','HI'], 
    CO: ['WY','NE','KS','OK','NM','AZ','UT'], 
    CT: ['MA','RI','NY'], 
    DC: ['MD','VA'], 
    DE: ['PA','NJ','MD'], 
    FL: ['GA','AL'], 
    GA: ['NC','SC','FL','AL','TN'], 
    HI: ['CA'], 
    IA: ['MN','WI','IL','MO','NE','SD'], 
    ID: ['MT','WY','UT','NV','OR','WA'], 
    IL: ['WI','IN','KY','MO','IA'], 
    IN: ['MI','OH','KY','IL'], 
    KS: ['NE','MO','OK','CO'], 
    KY: ['OH','WV','VA','TN','MO','IL','IN'], 
    LA: ['AR','MS','TX'], 
    MA: ['NH','RI','CT','NY','VT'], 
    MD: ['PA','DE','DC','VA','WV'], 
    ME: ['NH'], 
    MI: ['OH','IN','WI'], 
    MN: ['WI','IA','SD','ND'], 
    MO: ['IA','IL','KY','TN','AR','OK','KS','NE'], 
    MS: ['TN','AL','LA','AR'], 
    MT: ['ND','SD','WY','ID'], 
    NC: ['VA','SC','GA','TN'], 
    ND: ['MN','SD','MT'], 
    NE: ['SD','IA','MO','KS','CO','WY'], 
    NH: ['ME','MA','VT'], 
    NJ: ['NY','DE','PA'], 
    NM: ['CO','OK','TX','AZ','UT'], 
    NV: ['ID','UT','AZ','CA','OR'], 
    NY: ['VT','MA','CT','NJ','PA'], 
    OH: ['PA','WV','KY','IN','MI'], 
    OK: ['KS','MO','AR','TX','NM','CO'], 
    OR: ['WA','ID','NV','CA'], 
    PA: ['NY','NJ','DE','MD','WV','OH'], 
    RI: ['MA','CT'], 
    SC: ['NC','GA'], 
    SD: ['ND','MN','IA','NE','WY','MT'], 
    TN: ['KY','VA','NC','GA','AL','MS','AR','MO'], 
    TX: ['OK','AR','LA','NM'], 
    UT: ['ID','WY','CO','NM','AZ','NV'], 
    VA: ['MD','DC','NC','TN','KY','WV'], 
    VT: ['NH','MA','NY'], 
    WA: ['AK','ID','OR'], 
    WI: ['MI','IL','IA','MN'], 
    WV: ['PA','MD','VA','KY','OH'], 
    WY: ['MT','SD','NE','CO','UT','ID'] 
} 
+0

不是我正在尋找的東西,但我認爲這仍然是一個有用的迴應。 –