【NOIP提高组】矩阵

Description

在麦克雷的面前出现了一个有n*m个格子的矩阵,每个格子用“.”或“#”表示,“.”表示这个格子可以放东西,“#”则表示这个格子不能放东西。现在他拿着一条1×2大小的木棒,好奇的他想知道对于一些子矩阵,有多少种放木棒的方案。

Solution

因为棍子是1×2的,所以很容易就能发现,两个被分割的块,除了跨越两个块摆放木棍的方案数会对答案有影响的,否则两块是互不干扰的,那么用矩阵前缀和就能随便解决,只需要再减去边缘跨块的方案数就可以啦!

Code

var
    c:array[0..501,0..501] of char;
    f,h,l:array[0..501,0..501] of int64;
    n,m,i,j,q,x1,x2,y1,y2:longint;
    ans:int64;
begin
    readln(n,m);
    for i:=0 to n do c[i,0]:='#';
    for j:=0 to m do c[0,m]:='#';
    for i:=0 to n do c[i,m+1]:='#';
    for j:=0 to m do c[n+1,m]:='#';
    for i:=1 to n do
    begin
        for j:=1 to m do
        begin
            read(c[i,j]);
            h[i,j]:=h[i,j-1];l[i,j]:=l[i-1,j];
            if c[i,j]='#' then continue;
            if c[i-1,j]='.' then inc(h[i,j]);
            if c[i,j-1]='.' then inc(l[i,j]);
        end;
        readln;
    end;
    for i:=1 to n do
        for j:=1 to m do
        begin
            f[i,j]:=f[i-1,j]+f[i,j-1]-f[i-1,j-1];
            if c[i,j]='#' then continue;
            if c[i-1,j]='.' then inc(f[i,j]);
            if c[i,j-1]='.' then inc(f[i,j]);
        end;
    readln(q);
    for i:=1 to q do
    begin
        readln(x1,y1,x2,y2);
        ans:=f[x2,y2]-f[x2,y1-1]-f[x1-1,y2]+f[x1-1,y1-1];
        ans:=ans-h[x1,y2]+h[x1,y1-1];
        ans:=ans-l[x2,y1]+l[x1-1,y1];
        writeln(ans);
    end;
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值