Round C APAC Test 2017 Problem B. Safe Squares (C++)

Problem

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 D ≥ 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.

(Ri, Ci) ≠ (Rj, Cj) 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.

分析:

  • 想到用动态规划来做。开一个数组dp[R+1][C+1],其中dp[i][j]表示原矩阵中[0][0]到[i-1][j-1]范围内的safe square个数。
  • 显然,当矩阵[i-1][j-1]位置有monster时:
    dp[i][j]=dp[i-1][j+dp[i][j-1]-dp[i][j] 其中要减去重复计算的部分
  • 然而,当矩阵[i-1][j-1]位置无monster,即为安全的时候,就要考虑更多了:
    因为由于[i-1][j-1]位置是安全的,导致产生了新的safe square。这时候我用另外的两个矩阵来存储了需要的信息,即square[R+1][C+1]及stretch[R+1][C+1]。

    - square[i][j]表示:原矩阵中[0][0]到[i-1][j-1]范围内包含[i-1][j-1]位置的safe square个数
    - stretch的每个元素是一个pair,stretch[i][j].first(second)表示位置[i-1][j-1]及其左边(上边)紧邻的安全的位置个数
    

    这两个矩阵可以帮助计算[i-1][j-1]位置的加入而新产生的safe square个数,即为

    min(min(stretch[i][j].first, stretch[i][j].second), square[i - 1][j - 1] + 1)

  • 最后返回dp[R][C]即可

这里我的方法开了很多数组,占用了不少空间。肯定还有更好的方法解决这个问题。

代码:Github

#include <iostream>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <map>
#include <fstream>
#include <bitset>

using namespace std;

typedef long long ll;

int T;
int R, C, K;

int main() {
    //open files
    ifstream f_input;
    ofstream f_output;

    f_input.open("B-large-practice.in");
    f_output.open("out_put");

    f_input >> T;
    for (int tt = 1; tt <= T; tt++) {
        f_input >> R >> C >> K;
        vector<vector<char>> grid(R + 1, vector<char>(C + 1, 'O'));
        vector<vector<ll>> dp(R + 1, vector<ll>(C + 1, 0));
        vector<vector<ll>> square(R + 1, vector<ll>(C + 1, 0));
        vector< vector< pair<ll, ll> > > stretch(R + 1, vector< pair<ll, ll> >(C + 1, make_pair(0, 0)));
        for (int i = 0; i < K; i++) {
            int a, b;
            f_input >> a >> b;
            grid[a][b] = 'X';
        }
        for (int i = 1; i <= R; i++) {
            for (int j = 1; j <= C; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
                if (grid[i - 1][j - 1] == 'X') {
                    stretch[i][j] = make_pair(0, 0);
                    square[i][j] = 0;
                }
                else {
                    stretch[i][j] = make_pair(stretch[i][j - 1].first + 1, stretch[i - 1][j].second + 1);
                    square[i][j] = min(min(stretch[i][j].first, stretch[i][j].second), square[i - 1][j - 1] + 1);
                    dp[i][j] += square[i][j];
                }
            }
        }
        f_output << "Case #" << tt << ": " << dp[R][C] << endl;
        cout << tt << " finished" << endl;
    }

    f_input.close();
    f_output.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值