Skip to content

Instantly share code, notes, and snippets.

@02015678
Created April 9, 2015 13:59
Show Gist options
  • Save 02015678/9264a04343869e8436d9 to your computer and use it in GitHub Desktop.
Save 02015678/9264a04343869e8436d9 to your computer and use it in GitHub Desktop.
火车转轨/铁轨 (题目来源:UVA Problem Archive)
Const maxn=1000;
Var
x,y,i,j,n:1..maxn+1;{x:待出轨火车 y:待入轨火车}
d,a:array[1..maxn+1]OF 1..maxn;{a:原进站顺序 d:中间栈}
top:0..maxn+1;{栈顶指针}
Begin
readln(n);
For i:=1 to n do read(a[i]);
top:=0; x:=1; j:=1; y:=a[j];
For i:=1 to 2*n+1 do
IF y=0{如果待转轨火车为0,那么输出解}
THEN Begin writeln('Have Solution'); halt; End
ELSE
IF x=y{如果待转轨火车就是待出轨火车,那么直接从右边开到左边}
THEN Begin inc(x); inc(j); y:=a[j]; End
ELSE
IF (top<>0) and (y=d[top]){如果栈顶火车就是待出轨火车,那么出栈}
THEN Begin dec(top); inc(j); y:=a[j]; End
ELSE
IF x<>n+1{如果栈没有满,压栈}
THEN Begin inc(top); d[top]:=x; inc(x); End
ELSE Begin writeln('No Solution'); halt; End;{所有条件均不满足,无解}
End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment