Scau 10327 Biggest Square

时间限制:1000MS  内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题   语言: G++;GCC

Description

You are given a M*M cloth with some holes on it. Your task is to find a biggest square cloth from it. The following is an example(M=5)

输入格式

The first line contains the one integer number M (1<= M<=1,000). Then M lines are following. Each line contains M
charactors which “.” means cloth and “H” means hole.

输出格式

The only line of the output contains area of the biggest square cloth mentioned above.

输入样例

5
H...H
.....
.....
.HHH.
.....

输出样例

9

作者

admin

思路:比较经典的模型了,大白上有几乎一样的原题

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1005;
char Map[N][N];
int Up[N][N], Left[N][N], Right[N][N];
int n;
void getL() {
    for(int i = 1; i <= n; ++i) {
        int lo = 0;
        for(int j = 1; j <= n; ++j) {
           if(Map[i][j] == ‘.‘) {
                Up[i][j] = Up[i - 1][j] + 1;
                Left[i][j] = max(Left[i - 1][j], lo + 1);
           }else {
                Up[i][j] = Left[i][j] = 0;
                lo = j;
           }
        }
    }
}
int ans = 0;
void getR() {
    for(int i = 1; i <= n; ++i) {
        int ro = n + 1;
        for(int j = n; j >= 1; --j) {
            if(Map[i][j] == ‘.‘) {
                Right[i][j] = min(Right[i - 1][j], ro - 1);
            }else {
                Right[i][j] = n;
                ro = j;
            }
            ans = max(ans, min(Up[i][j], Right[i][j] - Left[i][j] + 1));
        }
    }
}
void show(int a[][N]) {
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) printf("%d ", a[i][j]);
        puts("");
    }
}
int main() {
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; ++i) scanf("%s", Map[i] + 1);
        for(int i = 0; i <= n; ++i) Up[0][i] = 0;
        for(int i = 0; i <= n; ++i) Left[0][i] = 0;
        for(int i = 0; i <= n; ++i) Right[0][i] = n;
        getL();
        getR();
        printf("%d\n", ans * ans);
    }
    return 0;
}
/*
5
HH.HH
.....
.....
H...H
HHHHH
*/

时间: 2024-12-29 13:20:37

Scau 10327 Biggest Square的相关文章

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

BestCoder Round #75 - King&#39;s Phone

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

HDU 5640 King&#39;s Cake

King's Cake Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time,

HDU 5640

King's Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 188    Accepted Submission(s): 159 Problem Description It is the king's birthday before the military parade . The ministers prepared a

湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero digits in it: You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighb

[leetcode] 367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 使用二分查找寻找input

Project Euler 92:Square digit chains C++

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

LeetCode Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 思路分析:这题考察DP.缓存中间结果降低反复计算.DP方程为 if(matrix[i][j

Theatre Square 题解代码

Theatre Square CodeForces - 1A 水题水题... 最开始的程序是这个,可是AC不了我也不知道为什么......QAQ... #include <stdio.h>#include<stdlib.h>int main(){    int m,n,a,ans,tmp,k,h,tep,i,j;    scanf("%d %d %d",&m,&n,&a);    if(m%a==0)    {        if(n%a