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

WA?????WHY?????I DO NOT BELIEVE THAT!!!!!
Posted by ACM 28 Apr 2003 19:20
{$N+}
program ti;
const fx:array[0..2,1..2]of longint=((-1,0),(0,-1),(0,1));

var n,m:longint;
    ans,num:array[1..100]of extended;
    a:array[0..500,1..100]of extended;
    last:array[0..500,1..100]of longint;

procedure read_data;
var i,j:longint;
    begin
     read(n,m);
     fillchar(last,sizeof(last),0);
     for i:=0 to n do
         for j:=1 to m do a[i,j]:=0;
     for i:=1 to n do begin
         readln;
         for j:=1 to m do begin
             read(num[j]);
             a[i,j]:=a[i-1,j]+num[j];
         end;
         for j:=2 to m do if a[i,j-1]+num[j]<a[i,j] then begin
             a[i,j]:=a[i,j-1]+num[j];
             last[i,j]:=1;
         end;
         for j:=m-1 downto 1 do if a[i,j+1]+num[j]<a[i,j] then begin
             a[i,j]:=a[i,j+1]+num[j];
             last[i,j]:=2;
         end;
     end;
end;

procedure change(var i,j:longint);
var k:longint;
begin
     k:=i+fx[last[i,j],1];
     j:=j+fx[last[i,j],2];
     i:=k;
end;

procedure write_data;
var i,j,cc:longint;
begin
     cc:=0;
     j:=1;
     for i:=2 to m do if a[n,i]<a[n,j] then j:=i;
     i:=n;
     while i<>0 do begin
           cc:=cc+1;
           ans[cc]:=j;
           change(i,j);
     end;
     for i:=cc downto 1 do writeln(ans[i]:0:0);
end;

begin
     read_data;
     write_data;
end.