题意:
给你个n*m的矩阵,他们有权值,0不能走,而且只能向下向右走。求出一条路径的字典序最小。走不到输出Oh,the life is too difficult!
思路:
先把所有的不可以走的路求出来,然后找他们可以走的路,全部入队,然后只保留最小的,如果有同个就全部保留,然后用这些数接着走,直到有n*m-1个队列就好了。
程序:
const
maxn=1000;
var
a,f:array [0..maxn+2,0..maxn+2] of longint;
i,j,n,m,x,y:longint;
begin
readln(n,m);
fillchar(a,sizeof(a),$7f);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(a[i,j]);
if a[i,j]=0 then a[i,j]:=maxlongint;
end;
readln;
end;
for i:=n downto 1 do
for j:=m downto 1 do
begin
if (i=n) and (j=m) then continue;
if (a[i+1,j]>20000000) and (a[i,j+1]>20000000) then a[i,j]:=a[i+1,j];
if a[i+1,j]<a[i,j+1] then f[i,j]:=1
else if a[i+1,j]>a[i,j+1] then f[i,j]:=2
else if a[i+2,j]>a[i,j+2] then f[i,j]:=2
else f[i,j]:=1;
end;
if a[1,1]>200000000 then
begin
writeln('Oh,the life is too difficult!');
halt;
end;
x:=1; y:=1;
while (x<>n) or (y<>m) do
begin
write(a[x,y],' ');
if f[x,y]=1 then inc(x)
else inc(y);
end;
writeln(a[n,m]);
end.