POJ3251:Big Square 题解

转载请注明出处:http://blog.csdn.net/RavenChau/article/details/78023318

【原题】

Big Square
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4882 Accepted: 1000

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 withN ×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

【题意】
图中点J可围成正方形的最大面积(可多加一点J,但不能覆盖点B)
【思路】
两两J点连线,判断能否成为正方形的邻边,之后取最大值
【AC代码】
#include <cstdio>
#include <cstring>
int map[110][110];
int main() {
    int n;
    scanf("%d", &n);
    memset(map, 0, sizeof(map));
    char ano;
    scanf("%c", &ano);
    for(int i = 1; i <= n; i ++) {
        for(int j = 1; j <= n; j ++) {
            char c;
            scanf("%c", &c);
            if(c == 'J')
                map[i][j] = 1;
            else if(c == 'B')
                map[i][j] = -1;
        }
        char ano;
        scanf("%c", &ano);
    }
    int area = 0,ans = 0;
    for(int i = 1; i <= n; i ++) {
        for(int j = 1; j <= n; j ++) {
            if(map[i][j] == 1) {
                for(int x = i; x <= n; x ++) {
                    for(int y = 1; y <= n; y ++) {
                        if(map[x][y] == 1) {
                            area = (x - i) * (x - i) + (y - j) * (y - j);
                            if(area <= ans) continue;
                            if((i+j-y)>0&&(j+x-i)>0&&(x+j-y)>0&&(y+x-i)>0&&(i+j-y)<=n&&(j+x-i)<=n&&(x+j-y)<=n&&(y+x-i)<=n&&(map[i+j-y][j+x-i]+map[x+j-y][y+x-i]>0) ||
                               (i-j+y)>0&&(j-x+i)>0&&(x-j+y)>0&&(y-x+i)>0&&(i-j+y)<=n&&(j-x+i)<=n&&(x-j+y)<=n&&(y-x+i)<=n&&(map[i-j+y][j-x+i]+map[x-j+y][y-x+i]>0) ) {
                                   ans = area;
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值