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 = 508, 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  = 0;

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 = 0 ;i < n; i++){
        for(int j = 0; j < m; j++){
            if(i + ans -1 < n && j + ans - 1 < m){

if(!judge(i + ans - 1, j, i, j + ans - 1)){
                    continue;
                }
            for(int l =2 ; i + l -1 < n && j + l - 1 < m; l++){
                if(!judge(i + l - 1, j, i, j + l - 1)){
                    break;
                }
                ans = max(ans , l);
            }
            }
        }
    }
}

void solve2(){
    for(int i = 0 ;i < n; i++){
        for(int j = 0; j < m; j++){

if(j - ans + 1 >= 0&& i + ans - 1 < n){

if(!judge2(i, j - ans + 1, i + ans - 1, j )){
                    continue;
                }

for(int l =2; j - l + 1 >= 0&& i + l - 1 < n; l++){
                if(!judge2(i, j - l + 1, i + l - 1, j )){
                    break;
                }
                ans = max(ans , l);
            }
        }
        }
    }
}

void solve3(){
    for(int i = 0 ;i < n; i++){
        for(int j = 0; j < m; j++){
            if(i - ans + 1 >= 0&& j + ans - 1 < m){

if(!judge2(i - ans + 1, j, i , j + ans - 1)){
                    continue;
                }
            for(int l = 2; i - l + 1 >= 0&& j + l - 1 < m; l++){
                if(!judge2(i - l + 1, j, i , j + l - 1)){
                    break;
                }
                ans = max(ans , l);
            }
            }
        }
    }
}

void solve4(){
    for(int i = 0 ;i < n; i++){
        for(int j = 0; j < m; j++){
            if(j - ans + 1 >= 0&& i - ans + 1 >= 0){

if(!judge(i, j - ans + 1, i - ans + 1 , j)){
                    continue;
                }
            for(int l = 2; j - l + 1 >= 0&& i - l + 1 >= 0; l++){
                if(!judge(i, j - l + 1, i - l + 1 , j)){
                    break;
                }
                ans = max(ans , l);
            }
            }
        }
    }
}

int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i= 0 ; i< n; i++){
            scanf("%s", g[i]);
        }
        ans = 1;
        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 + 1)/2<<‘\n‘;
    }

return 0;
}

时间: 2024-08-27 14:23:12

HDU4495 Rectangle的相关文章

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

Maximal Rectangle

题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 方法 使用两个矩阵,分别记录每一行连续的1的个数以及每一列连续的1的个数. public int maximalRectangle(char[][] matrix) { int lenX = matrix.length; if (lenX == 0) { r

【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3]. The largest r

Tutorial: Generate BBox or Rectangle to locate the target obejct

1 clc;close all;clear all; 2 Img=imread('/home/wangxiao/Documents/files/Visual_Tracking/MDNet-CVPR2016/MDNet-master/attentionMap/Basketball/0001.png'); 3 if ndims(Img)==3 4 I=rgb2gray(Img); 5 else 6 I=Img; 7 end 8 I=im2bw(I,graythresh(I)); 9 [m,n]=si

CSU 1547 Rectangle(dp、01背包)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(th

84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

LeetCode 85. Maximal Rectangle

1 class Solution { 2 public: 3 int maximalRectangle(vector<vector<char>>& matrix) { 4 /** largest rectangle based solution **/ 5 if(matrix.size()<=0 || matrix[0].size()<=0) 6 return 0; 7 int m=matrix.size(); 8 int n=matrix[0].size()+

LeetCode OJ:Maximal Rectangle(最大矩形)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 求出0,1矩阵中的最大矩形区域: DP问题,可以使用数组记下当前行位置之前的所有1的个数,然后用一个三重循环来找以(i,j)为右下角的矩形的最大的面积,比较得到最大值.这样做复杂度还是比较高的.但是胜在简单,代码如下所示: 1 class Solution { 2