【NOIP2014模拟9.7】我要的幸福 (Standard IO)

本文介绍了一种寻找从起点到终点字典序最小路径的算法。该算法首先标记不可通行区域,然后通过比较相邻格子权值确定下一步方向,最终输出路径。适用于n*m矩阵环境,仅允许向下或向右移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

给你个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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值