列表

详情


NC25086. [USACO 2006 Nov S]Big Square

描述

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).

输入描述

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.

输出描述

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

示例1

输入:

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

输出:

4

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 157ms, 内存消耗: 476K, 提交时间: 2019-07-04 19:59:00

#include<bits/stdc++.h>
using namespace std;
char t[110][110];
int main(){
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	cin>>t[i][j];
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	for(int i1=i+1;i1<=n;i1++)
	for(int j1=1;j1<=j;j1++){
		int i4=j-j1,j4=i1-i;
		int i2=i+i4,j2=j+j4,i3=i1+i4,j3=j1+j4;
		if(i2<1||i2>n||j2<1||j2>n||i3<1||i3>n||j3<1||j3>n||t[i][j]=='B'||t[i1][j1]=='B'||t[i2][j2]=='B'||t[i3][j3]=='B')continue;
		int s=0;
		if(t[i][j]=='J')s++;if(t[i1][j1]=='J')s++;if(t[i2][j2]=='J')s++;if(t[i3][j3]=='J')s++;
		if(s>=3)ans=max(ans,i4*i4+j4*j4);
	} 
	cout<<ans<<"\n";
	return 0;
}

上一题