POJ 3251 Big Square

A quite challenging problem,最终看了题解才写出来,惭愧

/*Sample Input

 

 6

 J*J***

 ******

 J***J*

 ******

 **B***

 ******

 

 Sample Output

 

 4

*/

 

这道题里用到的几种思想还是值得我回味和思考的就是:

1、将复杂的等式用#define代替,不仅简洁而且时间复杂度较低

2、需要在输入的时候加入一个scanf来接收空格键,否则会出错  

 1 //这道题目主要是给定一个最大步数,让你输出你在二维空间中可以到达的位置,最后求最大能够形成的面积
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 #define ok(a,b,c,d) (a>=0&&b>=0&&c>=0&&d>=0)
 7 char map[201][201],enter;
 8 int n,i,j,x,y,p,q,ans;
 9  
10 int main()
11 {
12     cin>>n;//输入这是n*n的正方形
13     for (i=1;i<=n;i++)
14     {
15         cin>>enter;//主要是为了吸收回车键(一开始没有注意到)
16         for (j=1;j<=n;j++)
17             cin>>map[i][j];
18     }
19     for (x=1;x<=n;x++)
20         for (y=1;y<=n;y++)
21             if (map[x][y]!='B')
22                 for (i=n;i>=1;i--)
23                     for(j=n;j>=y;j--)
24                     {
25                         p=i-x;q=j-y;
26                     if (p*p+q*q<=ans)//一开始ans为多少也不知道啊?
27                             continue;
28                     if (map[i][j]=='B'||((map[i][j]==map[x][y])&&(map[x][y]!='J')))
29                             continue;
30                     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'))
31                             ans=p*p+q*q;
32                     }
33     printf("%d",ans);
34     return 0;
35 }
36  
37  
38  
39  

 

 

 

原文地址:https://www.cnblogs.com/guohaoyu110/p/6337899.html