HDU4495 Rectangle
求组成的等腰三角形面积最大值。
对此题的总结:暴力出奇迹
组成的三角形放置方式一共只有4种,用ans表示目前已知的最长三角形的边长,从上到下,从左到右枚举顶点,再枚举边长,一个重要剪枝是枚举边长l时先判断l = ans时的边能不能对称。
最终暴力只要200多ms,而时间限制为10s
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
char g[N][N];
int n, m;
int ans = ; bool judge(int x1, int y1, int x2, int y2){
while(y1 < y2){
if(g[x1][y1] != g[x2][y2]){
return false;
}
x1--;
x2++;
y1++;
y2--;
}
return true;
} bool judge2(int x1, int y1, int x2, int y2){
while(x1 < x2){
if(g[x1][y1] != g[x2][y2]){
return false;
}
x1++;
x2--;
y1++;
y2--;
}
return true;
} void solve1(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){
if(i + ans - < n && j + ans - < m){ if(!judge(i + ans - , j, i, j + ans - )){
continue;
}
for(int l = ; i + l - < n && j + l - < m; l++){
if(!judge(i + l - , j, i, j + l - )){
break;
}
ans = max(ans , l);
}
}
}
}
} void solve2(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){ if(j - ans + >= && i + ans - < n){ if(!judge2(i, j - ans + , i + ans - , j )){
continue;
} for(int l =; j - l + >= && i + l - < n; l++){
if(!judge2(i, j - l + , i + l - , j )){
break;
}
ans = max(ans , l);
}
}
}
}
} void solve3(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){
if(i - ans + >= && j + ans - < m){ if(!judge2(i - ans + , j, i , j + ans - )){
continue;
}
for(int l = ; i - l + >= && j + l - < m; l++){
if(!judge2(i - l + , j, i , j + l - )){
break;
}
ans = max(ans , l);
}
}
}
}
} void solve4(){
for(int i = ;i < n; i++){
for(int j = ; j < m; j++){
if(j - ans + >= && i - ans + >= ){ if(!judge(i, j - ans + , i - ans + , j)){
continue;
}
for(int l = ; j - l + >= && i - l + >= ; l++){
if(!judge(i, j - l + , i - l + , j)){
break;
}
ans = max(ans , l);
}
}
}
}
} int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i= ; i< n; i++){
scanf("%s", g[i]);
}
ans = ;
int mini = min(n ,m);
solve1();
if(ans != mini){
solve2();
}
if(ans != mini){
solve2();
} if(ans != mini){
solve3();
}
if(ans != mini){
solve4();
} cout<<ans * (ans + )/<<'\n';
} return ;
}
HDU4495 Rectangle的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Maximal Rectangle
很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- javascript最容易混淆的作用域、提升、闭包
一.函数作用域 1.函数作用域 就是作用域在一个“Function”里,属于这个函数的全部变量都可以在整个函数的范围内使用及复用. function foo(a) { var b = 2; funct ...
- 5.6---交换整数的奇数位和偶数位(CC150)
这道题要利用101010来&. 如下答案: public class Exchange { public int exchangeOddEven(int x) { // write code ...
- Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- java 中for each语句
[转]java foreach 使用 foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本 ...
- RRD
http://my.oschina.net/u/1458120/blog/208857
- Apache2.4.6 添加虚拟主机
apache2.4 与 apache2.2 的虚拟主机配置写法有所不同 apache2.2的写法: <VirtualHost *:80> ServerName domain.com Doc ...
- DataTemplate应用
在WPF中,决定数据外观的是DataTemplate,即DataTemplate是数据内容的表现形式,一条数据显示成什么样子,是简单的文本还是直观的图形,就是由DataTemplate决定的.下面通过 ...
- frxReport 设计 (mtm)
► 设计 frxReport frxReport 窗体上放一个 [frxReport] 的控件 双击 [frxReport]控件 进入设置模式 frxReport1.ShowReport() 方 ...
- 从json传递数据显示表格实例
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UITableView* table; ...
- Linux rpm 命令参数使用详解[介绍和应用]
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两 ...