codevs动态规划 老鼠的旅行

You are a mouse that lives in a cage in a large laboratory.

你是一只生活在笼子里的实验室老鼠。

The laboratory is composed of one rectangular grid of square cages, with a total of R rows and C columns of cages (1 ≤ R,C ≤ 25).

实验室是一个R行C列的格子矩阵(1 ≤ R,C ≤ 25). 每个格子是一个笼子. (尼玛还要我活么……)

To get your exercise, the laboratory owners allow you to move between cages.

为了让你锻炼身体,实验室管理员允许你在笼子之间移动。

You can move between cages either by moving right between two adjacent cages in the same row, or by moving down between two adjacent cages in the same column.

你只能向右和向下移动。

You cannot move diagonally, left or up.

你不能斜着移动,也不能向上和向左移动。

Your cage is in one corner of the laboratory, which has the label (1,1) (to indicate top-most row, left-most column).

你所在的笼子是实验室的左上角,标记为(1,1)

You would like to visit your brother who lives in the cage labelled (R,C) (bottom-most row, right-most column), which is in the other corner diagonally.

你想去右下角的笼子(R,C)里找你的女朋友(尼玛老鼠也有女盆友么!!!)

However, there are some cages which you cannot pass through, since they contain cats.

但是有一些笼子是不能经过的,因为里面有猫(谁说老鼠怕猫么,还有,管理员有毛病么……)

Your brother, who loves numbers, would like to know how many different paths there are between your cage and his that do not pass through any cat cage. Write a program to compute this number of cat-free paths.

你女朋友很爱数学,她想要知道有多少条不同的路径可以从你的笼子到达她的笼子。写一个程序来计算吧。(这样的女朋友不要也罢……)

分析:这一题看上去很高大上,转化一下来想其实就是统计有几次可以走到(r,c),这么以来这题就很简单了。

动态转移方程:f[i,j]:=f[i-1.j]+f[i,j-1](这个点没有猫,更新时没必要判断来的点是否有猫,因为那个点的值一定是0,即没有路径可以走到那)

const
  maxn=25;


var
  map,f:array[0..maxn,0..maxn] of longint;
  r,c,i,j,k,x,y:longint;


begin
  readln(r,c);
  readln(k);
  for i:=1 to k do
    begin
      readln(x,y);
      map[x,y]:=1;
  end;
  f[1,1]:=1;
  for i:=1 to r do
    for j:=1 to c do
      begin
        if (i=1) and (j=1) then continue;
        if map[i,j]=0 then
          f[i,j]:=f[i-1,j]+f[i,j-1];
  end;
  writeln(f[r,c]);
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值