POJ3251 Big Square水题

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3251 Big Square水题相关的知识,希望对你有一定的参考价值。

Big Square
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6173 Accepted: 1188

Description

Farmer John’s cows have entered into a competition with Farmer Bob’s cows. They have drawn lines on the field so it is a square grid with N × N points (2 ≤ N ≤ 100), and each cow of the two herds has positioned herself exactly on a gridpoint. Of course, no two cows can stand in the same gridpoint. The goal of each herd is to form the largest square (not necessarily parallel to the gridlines) whose corners are formed by cows from that herd.

All the cows have been placed except for Farmer John’s cow Bessie. Determine the area of the largest square that Farmer John’s cows can form once Bessie is placed on the field (the largest square might not necessarily contain Bessie).

Input

Line 1: A single integer, N
Lines 2…N+1: Line i+1 describes line i of the field with N characters. The characters are: ‘J’ for a Farmer John cow, ‘B’ for a Farmer Bob cow, and ‘*’ for an unoccupied square. There will always be at least one unoccupied gridpoint.
Output

Line 1: The area of the largest square that Farmer John’s cows can form, or 0 if they cannot form any square.

Sample Input

6
J*J***
******
J***J*
******
**B***
******

Sample Output

4

Source

USACO 2006 November Silver

问题链接POJ3251 Big Square
问题简述:给定N*N矩阵,其元素包括’B’、’*‘和’J’三种。选择4个’J’点或者是3个’J’点和一个’*'点作为正方形的四个顶点。求正方形的最大面积。
问题分析:简单题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ3251 Big Square */

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 200;
char b[N + 1][N + 1];

inline bool judge(int a, int b, int c, int d)
{
    return a >= 0 && b >= 0 && c >= 0 && d >= 0;
}

int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    for (int i = 1; i <= n; i++)
        scanf("%s", b[i] + 1);

    int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (b[i][j] != 'B')
                for (int k = n; k >= 1; k--)
                    for(int l = n; l >= j; l--) {
                        int p = k - i;
                        int q = l - j;
                        if (p * p + q * q <= ans) continue;
                        if (b[k][l] == 'B' || (b[k][l] == b[i][j] && b[i][j] != 'J'))
                            continue;
                        if ((b[i - q][j + p] == 'J' && b[k - q][l + p] == 'J' && judge(i - q, k - q, j + p, l + p)) ||
                                (b[i + q][j - p] == 'J' && b[k + q][l - p] == 'J' && judge(j - p, l - p, k + q, i + q)))
                            ans = p * p + q * q;
                    }

  printf("%d\\n", ans);

  return 0;
}

以上是关于POJ3251 Big Square水题的主要内容,如果未能解决你的问题,请参考以下文章

Theatre Square 题解代码

CodeForces 711B Chris and Magic Square (暴力,水题)

UVA11470 Square Sums水题

bzoj1661[Usaco2006 Nov]Big Square 巨大正方形*

HDU1398:Square Coins(DP水题)

A. Theatre Square 1000 / 水题