Codeforces 123 B Squares

举报
Linux猿 发表于 2021/08/04 23:01:25 2021/08/04
【摘要】 题目链接~~> 做题感悟:昨天做的这道题,做了很久找到了一点规律,但是没A掉,看了一下官方题解果断看不懂,于是乎又开始研究题目,终于历时半天把“ 她 ”搞定了,但是官方题解还是没看懂,有看懂的大神求讲解。 解题思路:                先来一张图片(貌似有点大!) &n...

题目链接~~>

做题感悟:昨天做的这道题,做了很久找到了一点规律,但是没A掉,看了一下官方题解果断看不懂,于是乎又开始研究题目,终于历时半天把“ 她 ”搞定了,但是官方题解还是没看懂,有看懂的大神求讲解。

解题思路:

               先来一张图片(貌似有点大!)

             

            如果你画一下你会发现会是这样子的图,那么从一点走到另一点才是最少的呢 ?当然是走两者的交点,正如图中画的那样,因为如果你走交点,你可以越过两条直线, 那么这个交点数怎样算呢 ?就是途中红线和绿线经过途中蓝线的条数的最大值。

            好了,现在主要求图中红线和蓝线经过的直线条数(取最大值即可),因为红线是 45度且知道一个点,so~> 我们可以知道这条直线的方称,同理我们也可以知道绿线的直线方程,这样我们就可以求出两条直线的交点了,知道交点后求经过的直线条数就简单了,但是如果你直接用交点到某个点的距离除以宽度的话,这样是不行的,因为有可能少加 1 ,或者多加 1 ,so ~> 我们可以以一条直线为标准,这里已红线为例子讲解 (  绿线同理 ) :我们以 135 度的橙色直线为标准,如果两点分别在橙色线两侧个数就等于两点到橙色线的距离取整 + 1 ,否则两点到橙色线的距离相减的绝对值即为经过的直线的条数。

代码:


  
  1. #include<iostream>
  2. #include<sstream>
  3. #include<map>
  4. #include<cmath>
  5. #include<fstream>
  6. #include<queue>
  7. #include<vector>
  8. #include<sstream>
  9. #include<cstring>
  10. #include<cstdio>
  11. #include<stack>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<string>
  15. #include<cctype>
  16. #include<iomanip>
  17. #include<algorithm>
  18. using namespace std ;
  19. #define INT __int64
  20. const int INF = 0x3f3f3f ;
  21. const double esp = 0.0000000001 ;
  22. const double PI = acos(-1.0) ;
  23. const int mod = 1000000007 ;
  24. const INT Up = 1000000000 ;
  25. const INT Dn = -1000000000 ;
  26. const int MY = 205 ;
  27. const int MX = 205 ;
  28. int workA(double x1 ,double y1 ,double x2 ,double y2 ,double d)
  29. {
  30. double dA ,dB ;
  31. int num1 ,num2 ;
  32. dA = fabs(y1 + x1)/sqrt(2.0) ;
  33. dB = fabs(y2 + x2)/sqrt(2.0) ;
  34. num1 = (int)(dA/d + 1.0) ;
  35. num2 = (int)(dB/d + 1.0) ;
  36. if(((x1+y1 > 0)&&(x2 + y2 > 0)) || (x1 + y1 < 0 && x2 + y2 < 0)) // 同在一侧
  37. return abs(num1 - num2) ;
  38. else
  39. return abs(num1 + num2 - 1) ;
  40. }
  41. int workB(double x1 ,double y1 ,double x2 ,double y2 ,double d)
  42. {
  43. double dA ,dB ;
  44. int num1 ,num2 ;
  45. dA = fabs(y1 - x1)/sqrt(2.0) ;
  46. dB = fabs(y2 - x2)/sqrt(2.0) ;
  47. num1 = (int)(dA/d) + 1 ;
  48. num2 = (int)(dB/d) + 1 ;
  49. if(((y1 > x1)&&(y2 > x2)) || ((y1 < x1)&&(y2 < x2))) // 如果同时大于 同时小于
  50. return abs(num1 - num2) ;
  51. else
  52. return abs(num1 + num2 - 1) ;
  53. }
  54. int main()
  55. {
  56. double a ,b ,x1 ,y1 ,x2 ,y2 ,b1 ,b2 ,x ,y ;
  57. while(~scanf("%lf%lf%lf%lf%lf%lf" ,&a ,&b ,&x1 ,&y1 ,&x2 ,&y2))
  58. {
  59. if(x1 != x2 && y1-y2 == x1 - x2) // 为 45 度
  60. cout<<workA(x1 ,y1 ,x2 ,y2 ,sqrt(2.0)*a)<<endl ;
  61. else if(x1 != x2 && y1 - y2 == x2 - x1) // 为 135 度
  62. cout<<workB(x1 ,y1 ,x2 ,y2 ,sqrt(2.0)*b)<<endl ;
  63. else
  64. {
  65. b1 = y1 - x1 ;
  66. b2 = y2 + x2 ;
  67. x = (b2 - b1)/2.0 ; // 交点
  68. y = x + b1 ;
  69. cout<<max(workA(x1 ,y1 ,x ,y ,sqrt(2.0)*a) ,workB(x2 ,y2 ,x ,y ,sqrt(2.0)*b))<<endl ;
  70. }
  71. }
  72. return 0 ;
  73. }

再附上官方题解:

   

官方代码:


  
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <iostream>
  4. using namespace std;
  5. int a, b, x1, y1, x2, y2;
  6. int x, y;
  7. int main()
  8. {
  9. #ifndef ONLINE_JUDGE
  10. freopen("in", "r", stdin);
  11. freopen("out", "w", stdout);
  12. #endif
  13. cin >> a >> b >> x1 >> y1 >> x2 >> y2;
  14. x = x1; y = y1;
  15. x1 = x + y;
  16. y1 = y - x;
  17. x = x2; y = y2;
  18. x2 = x + y;
  19. y2 = y - x;
  20. a *= 2;
  21. b *= 2;
  22. x1 = x1 / a + (x1 > 0);
  23. x2 = x2 / a + (x2 > 0);
  24. y1 = y1 / b + (y1 > 0);
  25. y2 = y2 / b + (y2 > 0);
  26. cout << max(abs(y2 - y1), abs(x2 - x1)) << endl;
  27. return 0;
  28. }


 

文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nyist_zxp/article/details/39927781

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。