暑期dp46道(39)--HDOJ 2870 最大子矩阵面积...
来源:互联网 发布:java网盘系统源码 编辑:程序博客网 时间:2024/05/27 09:46
题目链接:HDOJ 2870
题意:给定一个仅由'a','b','c','w','x','y','z'组成的字符矩阵,且你可以把'w' 转成 (to)'a' 或 'b', 'x' to 'b' or 'c', 'y' to 'a' or 'c', 'z' to 'a', 'b' or 'c',求能转成的最大子矩阵面积;
题解:毫无疑问,对于'a','b','c'三种子矩阵,最大的时候一定是能转则转,总不能已经打算,所以分开考虑,每次转一种字符,求最大,然后求三个最大值的max,结束,这个矩阵求法,以前的博文里也有,那段时间 博客更新没有接上,直接补上 前面的链接:HDU 1505 题解 HDU 1506题解
此题代码:
#include<cstdio>#include<cstring>#include<string>#define debug 0#define M(a) memset(a,0,sizeof(a))#define Max(a,b) ((a>b)?a:b)const int maxn = 1000 + 5;int l[maxn][maxn], r[maxn][maxn], h[maxn][maxn], data[maxn][maxn], ans;int n, m;int dir[][4] = { { 1,4,6,7 },{ 2,4,5,7 },{ 3,5,6,7 } };//用数组记录可转的字符情况bool is(int a, int b){for (int i = 0; i<4; i++){if (dir[b - 1][i] == a)return true;}return false;}void Do(){int temp, maxn;for (int i = 1; i<4; i++){maxn = 0;M(l);M(r);M(h);for (int j = 1; j <= n; j++){for (int k = 1; k <= m; k++){if (is(data[j][k], i))//判断当前的是否可转,和以前的矩阵是否为0是一样的h[j][k] = h[j - 1][k] + 1;elseh[j][k] = 0;}}for (int j = 1; j <= n; j++)//求最大子矩阵面积{h[j][0] = h[j][m + 1] = -1;for (int k = 1; k <= m; k++){temp = k - 1;while (h[j][temp] >= h[j][k]){temp = l[j][temp];}l[j][k] = temp;}for (int k = m; k >= 1; k--){temp = k + 1;while (h[j][temp] >= h[j][k]){temp = r[j][temp];}r[j][k] = temp;}for (int k = 1; k <= m; k++){//printf("%d%d\n",r[j][k],l[j][k]);maxn = Max(maxn, (r[j][k] - l[j][k] - 1)*h[j][k]);}}ans = Max(ans, maxn);}printf("%d\n", ans);}int main(){#if debugfreopen("in.txt", "r", stdin);#endif//debugchar a;int temp;while (~scanf("%d%d", &n, &m)){ans = 0;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){while ((a = getchar()) == '\n' || a == ' ');switch (a)//字符转为整数,用于数组记录{case 'a':temp = 1; break;case 'b':temp = 2;break;case 'c':temp = 3; break;case 'w':temp = 4; break;case 'x':temp = 5; break;case 'y':temp = 6; break;case 'z':temp = 7; break;default:break;}//putchar(a);data[i][j] = temp;}}Do();}return 0;}
1 0
- 暑期dp46道(39)--HDOJ 2870 最大子矩阵面积...
- 暑期dp46道(40)--HDOJ 2830 最大子矩阵面积 额
- 暑期dp46道(9)——HDU 1505City Game 求最大子矩阵面积
- 暑期dp46道(20)HDOJ 1081 To the Max 最大连续子序列+暴力枚举
- 暑期dp46道(31)--HDOJ 1087 最大上升子序列和 dp(水)
- 暑期dp46道(38)--HDOJ 2845 Beans 最大不连续子序列和
- 暑期dp46道(27)--HDOJ 3008 Warcraft
- 暑期dp46道(36)--HDOJ 2577
- 暑期dp46道(37) HDOJ 2844 COINS
- 暑期dp46道(44)--HDOJ 1058
- 暑期dp46道(46)--HDOJ 1069
- 暑期dp46道(29) HDOJ 1503 Advanced Fruits 最长公共子序列
- 暑期dp46道(43)--HDOJ 1159 最长公共子序列(可不连续)
- 暑期dp46道(45)--HDOJ 1257 最少拦截系统 最长递增子序列
- 最大子矩阵面积
- 暑期dp46道(5)hdoj 1421 搬寝室s
- 暑期dp46道(6)抢劫Robberies ——HDOJ 2955
- 暑期dp46道(10)HDOJ 2602 Bone Collect 01背包
- 数据结构——8 单链表约瑟夫问题
- 两种格式的xml解析
- 写给像宝宝一样想要结束一段亲密关系的人
- 查找练习 hash——出现过的数字
- 利用CvVideoWriter和cvWriteFrame函数合成视频
- 暑期dp46道(39)--HDOJ 2870 最大子矩阵面积...
- Java类加载机制和反射机制
- Android数据绑定框架DataBinding,堪称解决界面逻辑的黑科技
- String常量池问题的几个例子
- 收集了 1000 个 iOS 常用的库。原文:https://github.com/iamdaiyuan/ios_top_1000
- JAVA泛型
- 欢迎使用CSDN-markdown编辑器
- HDU 1556 Color the ball(树状数组,基础,气球染色问题)
- HDU 2087 剪花布条(KMP算法)