Advertisement
Guest User

Angry Birds with laser beam (extended)

a guest
Nov 13th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FasterIO    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  5. #define ll          long long int
  6.  
  7. //pair<> data structure can also be used instead of struct
  8. struct point{
  9.     ll x,y;
  10. };
  11.  
  12. struct segment{
  13.     point s,e;
  14. };
  15.  
  16.  
  17. // Given three collinear points p, q, r, the function checks if
  18. // point q lies on line segment 'pr'
  19. bool onSegment(point p, point q, point r)
  20. {
  21.     if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
  22.         q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
  23.        return true;
  24.  
  25.     return false;
  26. }
  27. // To find orientation of ordered triplet (p, q, r).
  28. // The function returns following values
  29. // 0 --> p, q and r are collinear
  30. // 1 --> Clockwise
  31. // 2 --> Counterclockwise
  32. int orientation(point p, point q, point r)
  33. {
  34.     // See https://www.geeksforgeeks.org/orientation-3-ordered-points/
  35.     // for details of below formula.
  36.     ll val = (q.y - p.y) * (r.x - q.x) -
  37.               (q.x - p.x) * (r.y - q.y);
  38.  
  39.     if (val == 0) return 0;  // collinear
  40.  
  41.     return (val > 0)? 1: 2; // clock or counterclock wise
  42. }
  43.  
  44. // The function that returns true if line segment 'p1q1'
  45. // and 'p2q2' intersect.
  46. bool doIntersect(point p1, point q1, point p2, point q2)
  47. {
  48.     // Find the four orientations needed for general and
  49.     // special cases
  50.     int o1 = orientation(p1, q1, p2);
  51.     int o2 = orientation(p1, q1, q2);
  52.     int o3 = orientation(p2, q2, p1);
  53.     int o4 = orientation(p2, q2, q1);
  54.  
  55.     // General case
  56.     if (o1 != o2 && o3 != o4)
  57.         return true;
  58.  
  59.     // Special Cases
  60.     // p1, q1 and p2 are collinear and p2 lies on segment p1q1
  61.     if (o1 == 0 && onSegment(p1, p2, q1)) return true;
  62.  
  63.     // p1, q1 and q2 are collinear and q2 lies on segment p1q1
  64.     if (o2 == 0 && onSegment(p1, q2, q1)) return true;
  65.  
  66.     // p2, q2 and p1 are collinear and p1 lies on segment p2q2
  67.     if (o3 == 0 && onSegment(p2, p1, q2)) return true;
  68.  
  69.      // p2, q2 and q1 are collinear and q1 lies on segment p2q2
  70.     if (o4 == 0 && onSegment(p2, q1, q2)) return true;
  71.  
  72.     return false; // Doesn't fall in any of the above cases
  73. }
  74.  
  75. bool intersect(segment laser,vector<segment> walls){
  76.     //recieving the list of walls, and the laser beam segment
  77.     for(int i=0;i<walls.size();i++){
  78.         //checking the walls one by one
  79.         if(doIntersect(laser.s,laser.e,walls[i].s,walls[i].e))
  80.             return true;
  81.     }
  82.     return false;
  83. }
  84.  
  85. void solve(){
  86.     int t,n;
  87.     vector<segment> walls;
  88.     point pig, bird;
  89.     segment laser,wall;
  90.  
  91.     cin>>t;
  92.     while(t--){
  93.         cin>>n;
  94.         for(int i=0;i<n;i++){
  95.             cin>>wall.s.x>>wall.s.y>>wall.e.x>>wall.e.y;
  96.             walls.push_back(wall);
  97.         }
  98.         cin>>pig.x>>pig.y>>bird.x>>bird.y;
  99.         laser.s = pig;
  100.         laser.e = bird;
  101.         if(intersect(laser,walls)){
  102.             cout<<"Lose\n";
  103.         }
  104.         else{
  105.             cout<<"Win\n";
  106.         }
  107.         walls.clear();
  108.     }
  109. }
  110.  
  111. int main() {
  112.     Fin;
  113.     Fout;
  114.     FasterIO;
  115.     solve();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement