poj3251 & usaco 月赛 2006 Big Square 题解
2014-04-10 16:16
148 查看
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23361659
【原题】
Big Square
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
Sample Output
Source
USACO 2006 November Silver
【题意】
给定N*N的图,包括"B","*","J"三种情况。你要选择4个'J'点或者是3个'J'点和一个'*’点作为正方形的四个顶点。求正方形的最大面积。
【分析】网上说是二分图或是枚举对角线(事后查的O(∩_∩)O~~)。然而我已开始不知情,想用低空飞行法。每次枚举两个顶点,通过计算算出另外两个并判断。效率是N^4。
【原程序】
【漫长的改进之旅】感谢HHD大牛的辛勤调试!使我原来7.47s的程序变成了1.73s。
【注意一】把函数ok改成了define。
【注意二】如果p*p+q*q小于ans,就没必要进行判断了,所以可以预先判断(关键)
【注意三】加上一些神奇的东西可以使第四重循环减少一半。
【注意四】倒着枚举第二个点。
【结果】poj上1391ms过了。
【代码】
【原题】
Big Square
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2569 | Accepted: 624 |
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
【题意】
给定N*N的图,包括"B","*","J"三种情况。你要选择4个'J'点或者是3个'J'点和一个'*’点作为正方形的四个顶点。求正方形的最大面积。
【分析】网上说是二分图或是枚举对角线(事后查的O(∩_∩)O~~)。然而我已开始不知情,想用低空飞行法。每次枚举两个顶点,通过计算算出另外两个并判断。效率是N^4。
【原程序】
#include<cstdio> #include<algorithm> char map[201][201],enter; int n,i,j,x,y,p,q,ans; using namespace std; inline int ok(int o1,int o2,int o3,int o4) { return (o1>=0&&o2>=0&&o3>=0&&o4>=0); } int main() { scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%c",&enter); for (j=1;j<=n;j++) scanf("%c",&map[i][j]); } for (x=1;x<=n;x++) for (y=1;y<=n;y++) if (map[x][y]=='J') for (i=1;i<=n;i++) for(j=1;j<=n;j++) if (map[i][j]=='*'||map[i][j]=='J') { p=i-x;q=j-y; if (ok(x-q,i-q,y+p,j+p)&&(map[x-q][y+p]=='J')&&(map[i-q][j+p]=='J')) ans=max(ans,p*p+q*q); if (ok(y-p,j-p,i+q,x+q)&&(map[x+q][y-p]=='J')&&(map[i+q][j-p]=='J')) ans=max(ans,p*p+q*q); } printf("%d",ans); return 0; }
【漫长的改进之旅】感谢HHD大牛的辛勤调试!使我原来7.47s的程序变成了1.73s。
【注意一】把函数ok改成了define。
【注意二】如果p*p+q*q小于ans,就没必要进行判断了,所以可以预先判断(关键)
【注意三】加上一些神奇的东西可以使第四重循环减少一半。
【注意四】倒着枚举第二个点。
【结果】poj上1391ms过了。
【代码】
#include<cstdio> #include<algorithm> char map[201][201],enter; int n,i,j,x,y,p,q,ans; using namespace std; int main() { scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%c",&enter); for (j=1;j<=n;j++) scanf("%c",&map[i][j]); } for (x=1;x<=n;x++) for (y=1;y<=n;y++) if (map[x][y]!='B') for (i=n;i>=1;i--) for(j=n;j>=y;j--) { p=i-x;q=j-y; if (p*p+q*q<=ans) continue; if (map[i][j]=='B'||((map[i][j]==map[x][y])&&(map[x][y]!='J'))) continue; #define ok(a,b,c,d) (a>=0&&b>=0&&c>=0&&d>=0) if (ok(x-q,i-q,y+p,j+p)&&(map[x-q][y+p]=='J')&&(map[i-q][j+p]=='J')||ok(y-p,j-p,i+q,x+q)&&(map[x+q][y-p]=='J')&&(map[i+q][j-p]=='J')) ans=p*p+q*q; } printf("%d",ans); return 0; }
相关文章推荐
- poj 3186 && bzoj 1652: [Usaco2006 Feb]Treats for the Cows(DP)
- 1661: [Usaco2006 Nov]Big Square 巨大正方形
- |BZOJ 1661|暴力|[Usaco2006 Nov]Big Square 巨大正方形
- bzoj1662[Usaco2006 Nov]Round Numbers 圆环数&&bzojAc数100达成!
- bzoj 1661 [Usaco2006 Nov]Big Square 巨大正方形
- bzoj 1661: [Usaco2006 Nov]Big Square 巨大正方形 (枚举)
- bzoj 1661: [Usaco2006 Nov]Big Square 巨大正方形【枚举】
- bzoj 1661: [Usaco2006 Nov]Big Square 巨大正方形 解题报告
- bzoj:1661 [Usaco2006 Nov]Big Square 巨大正方形
- bzoj 1661: [Usaco2006 Nov]Big Square 巨大正方形(枚举)
- 【BZOJ】1661: [Usaco2006 Nov]Big Square 巨大正方形(暴力)
- 51nod 1428 && bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- USACO&nbsp;1.1&nbsp;破碎的项链
- USACO&nbsp;2.4&nbsp;分数化小数
- BZOJ 1715 [Usaco2006 Dec]Wormholes 虫洞 SPFA
- 1659: [Usaco2006 Mar]Lights Out 关灯
- 【poj3259】Wormholes 【USACO 2006 December Gold】
- C++&Python——【USACO 5.4.1】——Canada Tour
- 对偶图 && 【BZOJ】1001: [BeiJing2006]狼抓兔子(对偶图+最短路)
- [USACO2006NOV] Roadblocks