本帖最后由 caruko 于 2011-6-6 14:40 编辑
这是完善版本,默认只输出换乘次数最少的路线。要得到更多换乘次数的路线,注释掉49行。
可输出多个: 路线 换乘次数 途经站点数 | @ECHO OFF&SETLOCAL ENABLEDELAYEDEXPANSION | | set /p input=请输入起点站-终点站: | | for /f "tokens=1,2 delims=-, " %%a in ("!input!") do set "start=%%a"&set "end=%%b" | | ::取得交点。 | | for /f "tokens=1* delims= " %%a in (dt.txt) do ( | | set "xl_%%a=%%b" | | for %%i in (%%b) do set /a #%%i+=1 | | ) | | for /f "tokens=1,2 delims=#=" %%a in ('set #') do ( | | if !#%%a! geq 2 set "dd_list=!dd_list! %%a" | | set "#%%a=" | | ) | | ::取得每个交点可到达的线路。 | | for /f "tokens=2,3 delims=_=" %%a in ('set xl_') do ( | | set "str=%%b" | | for /f "tokens=1,2" %%x in ("!start! !end!") do ( | | if not "!str:%%x=#!"=="!str!" set "start_xl=!start_xl! %%a" | | if not "!str:%%y=#!"=="!str!" set "end_xl=!end_xl! %%a" | | ) | | for %%D in (!dd_list!) do ( | | if not "!str:%%D=!"=="!str!" ( | | set "%%x=!%%x:%%D=@%%D!" | | set "@%%D=!@%%D! %%a " | | ))) | | ::检查是否可直达 | | for %%a in (!start_xl!) do for %%b in (!end_xl!) do if %%a==%%b set "路线=^"!start! !end!^"" | | if not "!路线!"=="" goto :metic | | ::计算换乘路线 | | set "查询表=^"!start!=!start_xl!^"" | | set "剩余交点表=!dd_list!" | | :loop | | for %%i in (!查询表!) do ( | | for /f "tokens=1* delims==" %%A in ("%%~i") do ( | | for %%a in (%%B) do ( | | for %%b in (!剩余交点表!) do ( | | for %%c in (!@%%b!) do ( | | if %%a==%%c ( | | set "转车点=!转车点! %%b" | | set "剩余查询表=!剩余查询表! ^"%%A %%b=!@%%b:%%c=!^"" | | for %%d in (!end_xl!) do for %%e in (!@%%b!) do if %%e==%%d ( | | set "路线=!路线! ^"%%A %%b !end!^" " | | ))))))) | | for %%a in (!剩余交点表!) do for %%b in (!转车点!) do set "剩余交点表=!剩余交点表:%%b=!" | | set "查询表=!剩余查询表!" | | set "剩余查询表=" | | set 最多换乘次数+=1 | | | | | | if !路线!.==. goto :loop | | ::计算站点 | | :metic | | if !路线!.==. echo,无法找到换乘路径&goto :eof | | set /a xn=0 | | for %%a in (!路线!) do ( | | set /a xn+=1 | | set "线路_!xn!=%%~a" | | set "now=" | | set "last=" | | set /a 换乘次数_!xn!=0 | | for %%a in (%%~a) do ( | | set /a 换乘次数_!xn!+=1 | | if defined now set "last=!now!" | | set "now=%%a" | | if defined last call :js !xn! !last! !now! | | ) | | set /a 换乘次数_!xn!-=2 | | ) | | for /l %%i in (1,1,!xn!) do echo,线路%%i:"!线路_%%i: =--!",换乘 !换乘次数_%%i! 次,途经 !num_%%i! 站 | | goto :eof | | :js | | for /f "tokens=1,2 delims==" %%a in ('set xl_') do ( | | set "xl=%%b" | | if not "!xl:%2=!"=="!xl!" if not "!xl:%3=!"=="!xl!" ( | | set /a n=0 | | for %%x in (%%b) do ( | | set /a n+=1 | | if %%x.==%2. set /a st=n | | if %%x.==%3. set /a et=n | | ) | | set /a num=st-et | | if !num! lss 0 set /a num=0-num | | set /a num_%1+=num | | ) )COPY |
| 请输入起点站-终点站:奥体中心 西直门 | | 线路1:"奥体中心--北土城--海淀黄庄--西直门",换乘 2 次,途经 12 站 | | 线路2:"奥体中心--北土城--知春路--西直门",换乘 2 次,途经 7 站 | | 线路3:"奥体中心--北土城--芍药居--西直门",换乘 2 次,途经 16 站COPY |
|