ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1029. Ministry

I got WA at #12
Posted by xiaomengxian 27 Jul 2005 18:02
I think my code is right,but WA...
Help~
Program Ural1029;
  Var M,N,O,Ot,Aa,Bb:Longint;
      A:Array[1..100,1..500] Of Longint;
      F:Array[False..True,1..500] Of Longint;
      T:Array[1..100,1..500] Of Char;
  Procedure Init;
    Var I,J:Longint;
    Begin
      Fillchar(A,Sizeof(A),0);
      Readln(M,N);
      For I:=1 To M Do Begin
        For J:=1 To N Do
          Read(A[I,J]);
        Readln;
      End;
    End;
  Procedure Main;
    Var I,J,Min,MinJ,Last:Longint;
    Begin
      Fillchar(F,Sizeof(F),0);
      Fillchar(T,Sizeof(T),0);
      For J:=1 To N Do Begin
        F[True,J]:=A[1,J];
        F[False,J]:=F[True,J]+A[2,J];
        T[2,J]:='U';
      End;
      For I:=2 To M Do Begin
        Min:=Maxlongint;
        Last:=MinJ;
        For J:=1 To N Do
          If F[Odd(I),J]<Min Then Begin
            Min:=F[Odd(I),J];
            MinJ:=J;
          End;
        For J:=MinJ-1 DownTo 1 Do
          If F[Odd(I),J+1]+A[I,J]<F[Odd(I),J] Then Begin
            F[Odd(I),J]:=F[Odd(I),J+1]+A[I,J];
            T[I,J]:='R';
          End;
        For J:=MinJ+1 To N Do
          If F[Odd(I),J-1]+A[I,J]<F[Odd(I),J] Then Begin
            F[Odd(I),J]:=F[Odd(I),J-1]+A[I,J];
            T[I,J]:='L';
          End;
        Fillchar(F[Not Odd(I)],Sizeof(F[Not Odd(I)]),0);
        If I<>M Then
          For J:=1 To N Do Begin
            F[Not Odd(I),J]:=F[Odd(I),J]+A[I+1,J];
            T[I+1,J]:='U';
          End;
      End;
      O:=Maxlongint;
      For J:=1 To N Do Begin
        T[M,J]:='U';
        If F[Odd(M),J]<O Then Begin
          O:=F[Odd(M),J];
          Ot:=J;
        End;
      End;
    End;
  Procedure Print;
    Begin
      If Aa=1 Then Begin
        Writeln(Bb);
        Exit;
      End;
      If T[Aa,Bb]='U' Then Begin
        Aa:=Aa-1;
        Print;
        Aa:=Aa+1;
      End;
      If T[Aa,Bb]='L' Then Begin
        Bb:=Bb-1;
        Print;
        Bb:=Bb+1;
      End;
      If T[Aa,Bb]='R' Then Begin
        Bb:=Bb+1;
        Print;
        Bb:=Bb-1;
      End;
      Writeln(Bb);
    End;
  Procedure Out;
    Begin
      Aa:=M;Bb:=Ot;
      Print;
    End;
Begin
  Init;
  Main;
  Out;
End.