Round C APAC Test 2017 Problem B. Monster Path

Problem B. Safe Squares


Codejamon trainers are actively looking for monsters, but if you are not a trainer, these monsters could be really dangerous for you. You might want to find safe places that do not have any monsters!

Consider our world as a grid, and some of the cells have been occupied by monsters. We define a  safe square  as a grid-aligned  D  ×  D  square of grid cells (with  ≥ 1) that does not contain any monsters. Your task is to find out how many safe squares (of any size) we have in the entire world.

Input

The first line of the input gives the number of test cases,  T T  test cases follow. Each test case starts with a line with three integers,  R C , and  K . The grid has  R  rows and  C columns, and contains  K  monsters.  K  more lines follow; each contains two integers  Ri  and Ci , indicating the row and column that the i-th monster is in. (Rows are numbered from top to bottom, starting from 0; columns are numbered from left to right, starting from 0.)

Output

For each test case, output one line containing  Case #x: y , where  x  is the test case number (starting from 1) and  y  is the the total number of safe zones for this test case.

Limits

1 ≤ T ≤ 20.

(RiCi) ≠ (RjCj) for i ≠ j. (No two monsters are in the same grid cell.)

0 ≤ Ri < R, i from 1 to K

0 ≤ Ci < C, i from 1 to K

Small dataset

1 ≤ R ≤ 10.

1 ≤ C ≤ 10.

0 ≤ K ≤ 10.

Large dataset

1 ≤ R ≤ 3000.

1 ≤ C ≤ 3000.

0 ≤ K ≤ 3000.

Sample


Input 
 

Output 
 
2
3 3 1
2 1
4 11 12
0 1
0 3
0 4
0 10
1 0
1 9
2 0
2 4
2 9
2 10
3 4
3 10
Case #1: 10
Case #2: 51

The grid of sample case #1 is:

0 0 0
0 0 0
0 1 0

Here, 0 represents a cell with no monster, and 1 represents a cell with a monster. It has 10 safe squares: 8 1x1 and 2 2x2.

The grid of sample case #2 is:

0 1 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1 0
1 0 0 0 1 0 0 0 0 1 1
0 0 0 0 1 0 0 0 0 0 1

Note that sample case #2 will only appear in the Large dataset. It has 51 safe squares: 32 1x1, 13 2x2, 5 3x3, and 1 4x4.

S[i][j] represents size of the square sub-matrix with all 1s including M[i][j] where M[i][j] is the rightmost and bottommost entry in sub-matrix.


Solution

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;

const int MAXN = 3000;
bool grid[MAXN][MAXN];
ll dp[MAXN][MAXN];

ll get_res(int R, int C)
{
    ll res = 0;

    for (int i = 0; i < R; i++)
    {
        dp[i][0] = !grid[i][0];
        res += dp[i][0];
    }

    for (int j = 1; j < C; j++)
    {
        dp[0][j] = !grid[0][j];
        res += dp[0][j];
    }

    for (int i = 1; i < R; i++)
        for (int j = 1; j < C; j++)
        {
            if (!grid[i][j])
            {
                dp[i][j] = min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1])) + 1;
                res += dp[i][j];
            }
            else
                dp[i][j] = 0;
        }

    return res;
}

int main(int argc, char* argv[])
{
#ifndef TEST
    if (argc != 2)
    {
        cout << "Invalid input" << endl;
        return 1;
    }

    string input = argv[1];
    string output = input.substr(0, input.length() - 2) + "out";
    freopen(input.c_str(), "r", stdin);
    freopen(output.c_str(), "w", stdout);
#endif

    int T, R, C, K, Ri, Ci;
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        memset(grid, 0, MAXN * MAXN * sizeof(bool));
        memset(dp, 0, MAXN * MAXN * sizeof(ll));

        cin >> R >> C >> K;
        for (int j = 0; j < K; j++)
        {
            cin >> Ri >> Ci;
            grid[Ri][Ci] = 1;
        }

        ll res = get_res(R, C);
        cout << "Case #" << i << ": " << res << endl;
    }

    fclose(stdin);
    fclose(stdout);

    return 0;
}

Note

dp[i][j] represents width of the maximum square sub-matrix with all 0s including grid[i][j] where grid[i][j] is the rightmost and bottommost entry in sub-matrix.


Reference

https://code.google.com/codejam/contest/6274486/dashboard#s=p1

http://codeforces.com/blog/entry/47185


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EnjoyCodingAndGame

愿我的知识,成为您的财富!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值