花园 打表0.1秒AC

本文介绍了一种通过预处理生成所有可能状态并利用打表优化算法解决特定花园种植问题的方法,有效降低了求解时间至0.1秒。

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

今天做了一道题,打表,花了几个小时,当然最后0.1秒AC。

【题目描述】【大意】

有一个花园,可以看作3*3的格子

花园中有四种花,分别是Lv1,Lv2,Lv3,Lv4,每种花都有其价值(不保证单调递增)

Lv1的花可以任意种;

Lv2的花必须在其相邻格子中种有Lv1的花时才能种;

Lv3的花必须在其相邻格子中种有Lv1和Lv2的花才能种;

Lv4的花必须在其相邻格子中种有Lv1,Lv2,Lv3的花才能种;

花可以铲除后在种,铲除不花费时间。

 

给n组数据,每组数据含b1,b2,b3,b4,minv,表示bi表示lvi的花的价值,minv表示最短距离。

求最短需要多少时间,使得花的总价值大于minv。

 

我的方法是先搜索出所有的状态,即lvi的花有ai种,其总耗时为time

然后打出表,带进另外一个程序,直接枚举每种状态即可

由于状态数只有340种,所以最后耗时0.1秒。

 

搜索程序:

program garden_pre;
const tx:array[1..4]of integer=(1,-1,0,0); ty:array[1..4]of integer=(0,0,1,-1);
type
  gardentype=record
    a:array[1..3,1..3]of byte;
    b:array[1..3,1..3]of integer;
    step:longint;
  end;
var
  d:array[1..1000000]of gardentype;
  w:array[1..4]of longint; wmin:longint;
  hash:array[0..4,0..4,0..4,0..4,0..4,0..4,0..4,0..4,0..4]of boolean;
  t:array[0..9,0..8,0..6,0..4]of longint;
  head,tail,i,j,k,l:longint;
procedure decs(i,j,s:byte; var now:gardentype; add:integer);
var t:byte;
begin
  for t:=1 to 4 do
    if (i+tx[t] in [1..3])and(j+ty[t] in [1..3]) then
    case add of
    -1:dec(now.b[i+tx[t]][j+ty[t]],trunc(exp((s-1)*ln(10))));
     1:inc(now.b[i+tx[t]][j+ty[t]],trunc(exp((s-1)*ln(10))));
    end;
end;
procedure simp(now:gardentype);
var tx:array[0..4]of integer;
begin
  fillchar(tx,sizeof(tx),0);
  with now do
  for i:=1 to 3 do
    for j:=1 to 3 do
      inc(tx[a[i,j]]);
  if (now.step<t[tx[1],tx[2],tx[3],tx[4]]) then t[tx[1],tx[2],tx[3],tx[4]]:=now.step;
end;
procedure expand(now:gardentype);
var i,j:longint;
begin
  for i:=1 to 3 do
   for j:=1 to 3 do
  begin
    inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=1;
    with d[head] do
      if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
      begin
        hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
        decs(i,j,now.a[i,j],d[head],-1);
        decs(i,j,1,d[head],1);
        simp(d[head]);
      end else dec(head);
    if now.b[i,j] mod 10<=0 then continue;
    inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=2;
    with d[head] do
      if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
      begin
        hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
        decs(i,j,now.a[i,j],d[head],-1);
        decs(i,j,2,d[head],1);
        simp(d[head]);
      end else dec(head);
    if now.b[i,j] mod 100<=10 then continue;
    inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=3;
    with d[head] do
      if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
      begin
        hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
        decs(i,j,now.a[i,j],d[head],-1);
        decs(i,j,3,d[head],1);
        simp(d[head]);
      end else dec(head);
    if now.b[i,j] mod 1000<=100 then continue;
    inc(head); d[head]:=now; inc(d[head].step); d[head].a[i,j]:=4;
    with d[head] do
      if not hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]] then
      begin
        hash[a[1][1]][a[1][2]][a[1][3]][a[2][1]][a[2][2]][a[2][3]][a[3][1]][a[3][2]][a[3][3]]:=true;
        decs(i,j,now.a[i,j],d[head],-1);
        decs(i,j,4,d[head],1);
        simp(d[head]);
      end else dec(head);
  end;
end;
begin
assign(output,'garden.txt');rewrite(output);
fillchar(t,sizeof(t),$7f);
  head:=1; tail:=0;
  repeat
    inc(tail);
    expand(d[tail]);
  until tail>head;
  writeln('const a:array[1..340,1..5]of integer=(');
  for i:=0 to 9 do
    for j:=0 to 8 do
      for k:=0 to 6 do
        for l:=0 to 4 do
          if t[i,j,k,l]<>2139062143 then
           writeln('(',i,',',j,',',k,',',l,',',t[i,j,k,l],'),');
close(output);
end.
//2139062143


打表后的主程序:

 

const a:array[1..340,1..5]of integer=(
(1,0,0,0,1),
(1,1,0,0,2),
(1,1,1,0,4),
(1,1,2,0,5),
(1,2,0,0,3),
(1,2,1,0,5),
(1,2,1,1,6),
(1,2,1,2,9),
(1,2,2,0,6),
(1,2,2,1,7),
(1,2,2,2,9),
(1,2,2,4,13),
(1,2,3,0,7),
(1,2,3,1,9),
(1,2,3,2,10),
(1,2,3,3,13),
(1,2,4,0,9),
(1,2,4,1,10),
(1,2,4,2,12),
(1,2,5,0,10),
(1,2,5,1,12),
(1,2,6,0,12),
(1,3,0,0,4),
(1,3,0,1,9),
(1,3,1,0,6),
(1,3,1,1,7),
(1,3,1,2,10),
(1,3,1,3,13),
(1,3,1,4,15),
(1,3,2,0,7),
(1,3,2,1,8),
(1,3,2,2,10),
(1,3,2,3,12),
(1,3,3,0,8),
(1,3,3,1,10),
(1,3,3,2,11),
(1,3,4,0,10),
(1,3,4,1,11),
(1,3,5,0,11),
(1,4,0,0,5),
(1,4,0,1,10),
(1,4,0,2,13),
(1,4,1,0,7),
(1,4,1,1,8),
(1,4,1,2,11),
(1,4,1,3,13),
(1,4,2,0,8),
(1,4,2,1,10),
(1,4,2,2,11),
(1,4,3,0,10),
(1,4,3,1,11),
(1,4,4,0,11),
(1,5,0,0,7),
(1,5,0,1,11),
(1,5,0,2,14),
(1,5,0,3,16),
(1,5,1,0,8),
(1,5,1,1,10),
(1,5,1,2,12),
(1,5,2,0,10),
(1,5,2,1,11),
(1,5,3,0,11),
(1,6,0,0,8),
(1,6,0,1,12),
(1,6,0,2,14),
(1,6,1,0,10),
(1,6,1,1,11),
(1,6,2,0,11),
(1,7,0,0,10),
(1,7,0,1,13),
(1,7,1,0,11),
(1,8,0,0,11),
(2,0,0,0,2),
(2,0,1,0,5),
(2,0,2,0,6),
(2,1,0,0,3),
(2,1,1,0,4),
(2,1,1,1,6),
(2,1,1,2,8),
(2,1,2,0,5),
(2,1,2,1,7),
(2,1,2,2,9),
(2,1,2,4,13),
(2,1,3,0,7),
(2,1,3,1,9),
(2,1,3,2,10),
(2,1,3,3,13),
(2,1,4,0,8),
(2,1,4,1,10),
(2,1,4,2,13),
(2,1,5,0,10),
(2,1,5,1,13),
(2,1,6,0,13),
(2,2,0,0,4),
(2,2,0,1,7),
(2,2,0,2,10),
(2,2,1,0,5),
(2,2,1,1,6),
(2,2,1,2,8),
(2,2,1,3,11),
(2,2,1,4,12),
(2,2,2,0,6),
(2,2,2,1,8),
(2,2,2,2,10),
(2,2,2,3,12),
(2,2,3,0,8),
(2,2,3,1,9),
(2,2,3,2,11),
(2,2,4,0,9),
(2,2,4,1,11),
(2,2,5,0,10),
(2,3,0,0,5),
(2,3,0,1,7),
(2,3,0,2,10),
(2,3,0,3,14),
(2,3,0,4,16),
(2,3,1,0,6),
(2,3,1,1,7),
(2,3,1,2,9),
(2,3,1,3,11),
(2,3,2,0,7),
(2,3,2,1,9),
(2,3,2,2,11),
(2,3,3,0,9),
(2,3,3,1,10),
(2,3,4,0,10),
(2,4,0,0,6),
(2,4,0,1,8),
(2,4,0,2,11),
(2,4,0,3,13),
(2,4,1,0,7),
(2,4,1,1,9),
(2,4,1,2,11),
(2,4,2,0,9),
(2,4,2,1,10),
(2,4,3,0,10),
(2,5,0,0,7),
(2,5,0,1,10),
(2,5,0,2,12),
(2,5,1,0,9),
(2,5,1,1,10),
(2,5,2,0,10),
(2,6,0,0,9),
(2,6,0,1,12),
(2,6,1,0,10),
(2,7,0,0,10),
(3,0,0,0,3),
(3,0,1,0,5),
(3,0,1,1,7),
(3,0,1,2,9),
(3,0,2,0,6),
(3,0,2,1,8),
(3,0,2,2,10),
(3,0,2,4,14),
(3,0,3,0,8),
(3,0,3,1,10),
(3,0,3,2,11),
(3,0,3,3,14),
(3,0,4,0,9),
(3,0,4,1,11),
(3,0,4,2,14),
(3,0,5,0,11),
(3,0,5,1,14),
(3,0,6,0,14),
(3,1,0,0,4),
(3,1,0,1,7),
(3,1,0,2,9),
(3,1,1,0,5),
(3,1,1,1,7),
(3,1,1,2,9),
(3,1,1,3,11),
(3,1,1,4,13),
(3,1,2,0,6),
(3,1,2,1,8),
(3,1,2,2,10),
(3,1,2,3,12),
(3,1,3,0,7),
(3,1,3,1,9),
(3,1,3,2,11),
(3,1,4,0,9),
(3,1,4,1,11),
(3,1,5,0,11),
(3,2,0,0,5),
(3,2,0,1,7),
(3,2,0,2,9),
(3,2,0,3,12),
(3,2,0,4,13),
(3,2,1,0,6),
(3,2,1,1,7),
(3,2,1,2,9),
(3,2,1,3,11),
(3,2,2,0,7),
(3,2,2,1,8),
(3,2,2,2,10),
(3,2,3,0,8),
(3,2,3,1,10),
(3,2,4,0,9),
(3,3,0,0,6),
(3,3,0,1,8),
(3,3,0,2,10),
(3,3,0,3,12),
(3,3,1,0,7),
(3,3,1,1,8),
(3,3,1,2,9),
(3,3,2,0,8),
(3,3,2,1,9),
(3,3,3,0,9),
(3,4,0,0,7),
(3,4,0,1,9),
(3,4,0,2,10),
(3,4,1,0,8),
(3,4,1,1,9),
(3,4,2,0,9),
(3,5,0,0,8),
(3,5,0,1,10),
(3,5,1,0,9),
(3,6,0,0,9),
(4,0,0,0,4),
(4,0,0,1,8),
(4,0,0,2,10),
(4,0,1,0,6),
(4,0,1,1,8),
(4,0,1,2,10),
(4,0,1,3,12),
(4,0,1,4,14),
(4,0,2,0,7),
(4,0,2,1,9),
(4,0,2,2,11),
(4,0,2,3,13),
(4,0,3,0,8),
(4,0,3,1,10),
(4,0,3,2,12),
(4,0,4,0,10),
(4,0,4,1,12),
(4,0,5,0,12),
(4,1,0,0,5),
(4,1,0,1,8),
(4,1,0,2,10),
(4,1,0,3,12),
(4,1,0,4,14),
(4,1,1,0,6),
(4,1,1,1,8),
(4,1,1,2,10),
(4,1,1,3,12),
(4,1,2,0,7),
(4,1,2,1,9),
(4,1,2,2,11),
(4,1,3,0,8),
(4,1,3,1,10),
(4,1,4,0,10),
(4,2,0,0,6),
(4,2,0,1,8),
(4,2,0,2,10),
(4,2,0,3,12),
(4,2,1,0,7),
(4,2,1,1,8),
(4,2,1,2,10),
(4,2,2,0,8),
(4,2,2,1,9),
(4,2,3,0,9),
(4,3,0,0,7),
(4,3,0,1,9),
(4,3,0,2,10),
(4,3,1,0,8),
(4,3,1,1,9),
(4,3,2,0,9),
(4,4,0,0,8),
(4,4,0,1,10),
(4,4,1,0,9),
(4,5,0,0,9),
(5,0,0,0,5),
(5,0,0,1,9),
(5,0,0,2,11),
(5,0,0,3,13),
(5,0,0,4,15),
(5,0,1,0,7),
(5,0,1,1,9),
(5,0,1,2,11),
(5,0,1,3,13),
(5,0,2,0,8),
(5,0,2,1,10),
(5,0,2,2,12),
(5,0,3,0,9),
(5,0,3,1,11),
(5,0,4,0,11),
(5,1,0,0,6),
(5,1,0,1,9),
(5,1,0,2,11),
(5,1,0,3,13),
(5,1,1,0,7),
(5,1,1,1,9),
(5,1,1,2,11),
(5,1,2,0,8),
(5,1,2,1,10),
(5,1,3,0,9),
(5,2,0,0,7),
(5,2,0,1,9),
(5,2,0,2,11),
(5,2,1,0,8),
(5,2,1,1,9),
(5,2,2,0,9),
(5,3,0,0,8),
(5,3,0,1,10),
(5,3,1,0,9),
(5,4,0,0,9),
(6,0,0,0,6),
(6,0,0,1,10),
(6,0,0,2,12),
(6,0,0,3,14),
(6,0,1,0,8),
(6,0,1,1,10),
(6,0,1,2,12),
(6,0,2,0,9),
(6,0,2,1,11),
(6,0,3,0,10),
(6,1,0,0,7),
(6,1,0,1,10),
(6,1,0,2,12),
(6,1,1,0,8),
(6,1,1,1,10),
(6,1,2,0,9),
(6,2,0,0,8),
(6,2,0,1,10),
(6,2,1,0,9),
(6,3,0,0,9),
(7,0,0,0,7),
(7,0,0,1,11),
(7,0,0,2,13),
(7,0,1,0,9),
(7,0,1,1,11),
(7,0,2,0,10),
(7,1,0,0,8),
(7,1,0,1,11),
(7,1,1,0,9),
(7,2,0,0,9),
(8,0,0,0,8),
(8,0,0,1,12),
(8,0,1,0,10),
(8,1,0,0,9),
(9,0,0,0,9));
var
  i,j,k,tot,ans,n:longint;
  b:array[1..5]of longint;
begin
assign(input,'garden.in'); assign(output,'garden.out');
reset(input); rewrite(output);
  readln(n);
  for i:=1 to n do begin
    ans:=maxlongint;
    for j:=1 to 5 do read(b[j]); readln;
    for k:=1 to 340 do begin
      tot:=0;
      for j:=1 to 4 do inc(tot,a[k][j]*b[j]);
      if tot<b[5] then continue;
      if a[k][5]<ans then ans:=a[k][5];
    end;
    if ans=maxlongint then writeln('Impossible')
    else writeln(ans);
  end;
close(input);close(output);
end.

 

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间和笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度和加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置和姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数和工具箱: 优化工具箱:用于解决运动学逆解和路径规划中的优化问题。 Simulink:可视化建模环境,适合构建和仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程和轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间和笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值