HDU-1505-City Game——算法笔记

    xiaoxiao2023-11-15  146

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1505
    Problem Description:

    Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in. Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you’re building stands is 3$. Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.

    Input:

    The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are: R – reserved unit F – free unit In the end of each area description there is a separating line.

    Output:

    For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.

    Sample Input:
    Sample Output:

    这个题目是 HDU-1506 的升级版,但是把 HDU-1506 理解了,这题也就不难了。这是 HDU-1506 的题解:   https://blog.csdn.net/weixin_43207025/article/details/90548845 把这个字符矩阵换一种矩阵来表示,使得换后的矩阵的每一行和 HDU-1506 输入数据类似。矩阵转换方法:   如果字符是 “F”,每一行各列的值等于上一行对应列的值加一,即 dp[i][j] = dp[i - 1][j] + 1; 如果是 “R”,dp[i][j] = 0; 这样,就得到一个二维的 dp 矩阵,这个二维 dp 矩阵的每一行的数据就相当于 HDU-1506 的输入数据。也就是说 dp[i][j] 的值就表示矩形的高度。对应在矩阵中刚好符合(是不是很巧妙)。然后,就可以遍历每一行,套用 HDU-1506 的算法模板就行了。

    通过的参考代码:

    #include <iostream> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; #define INF -0x3f3f3f3f #define ll long long #define clean(arrays) memset(arrays, 0, sizeof(arrays)) ll max_area; ll dp[1005][1005] = {0}; ll lefts[1005], rights[1005]; //lefts 存储连续超过该位置的高度的 最左边下标 // rights 存储连续超过该位置的高度的 最右边下标 int main() { int k; cin >> k; while (k--) { int m, n, t; cin >> m >> n; clean(dp); clean(lefts); clean(rights); max_area = INF; // 把字符矩阵转换成数字 for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { char str; cin >> str; if (str == 'F') dp[i][j] = dp[i - 1][j] + 1; else dp[i][j] = 0; } } for (int i = 1; i <= m; i++) // 遍历 dp 矩阵的每一行 { // 接下来就和 HDU-1506 一样了 for (int j = 1; j <= n; j++) lefts[j] = rights[j] = j; for (int j = 1; j <= n; j++) { t = j; while (t > 1 && dp[i][j] <= dp[i][t - 1]) t = lefts[t - 1]; lefts[j] = t; } for (int j = n - 1; j >= 1; j--) { t = j; while (t < n && dp[i][j] <= dp[i][t + 1]) t = rights[t + 1]; rights[j] = t; } for (int j = 1; j <= n; j++) { max_area = max((rights[j] - lefts[j] + 1) * dp[i][j] , max_area); } } cout << max_area * 3 << endl; // 在最后输出 最大值 } return 0; }
    最新回复(0)