Point Of Impact BILLRD SOLUTION Code Chef

Point Of Impact BILLRD SOLUTION Code Chef

You are playing a Billiards-like game on an N×NN×N table, which has its four corners at the points {(0,0),(0,N),(N,0),{(0,0),(0,N),(N,0), and (N,N)}(N,N)}. You start from a coordinate (x,y)(x,y), (0<x<N,0<y<N)(0<x<N,0<y<N) and shoot the ball at an angle 45∘45∘ with the horizontal. On hitting the sides, the ball continues to move with the same velocity and ensuring that the angle of incidence is equal to the angle of reflection with the normal, i.e, it is reflected with zero frictional loss. On hitting either of the four corners, the ball stops there and doesn’t move any further.

Find the coordinates of the point of collision, when the ball hits the sides for the KthKth time. If the ball stops before hitting the sides KK times, find the coordinates of the corner point where the ball stopped instead.

Input:

  • The first line of the input contains an integer TT, the number of testcases.
  • Each testcase contains a single line of input, which has four space separated integers – NN, KK, xx, yy, denoting the size of the board, the number of collisions to report the answer for, and the starting coordinates.

Output:

For each testcase, print the coordinates of the ball when it hits the sides for the KthKth time, or the coordinates of the corner point if it stopped earlier.

Constraints

  • 1≤T≤1051≤T≤105
  • 2≤N≤1092≤N≤109
  • 1≤K≤1091≤K≤109

Subtasks

  • 3030 points : Sum of KK over all test cases ≤107≤107
  • 7070 points : Original constraints.

Sample Input:

2
5 5 4 4
5 2 3 1

Sample Output:

5 5
3 5

Explanation:

  • Sample Case 11 :

We are given a 55 by 55 board. We shoot the ball from coordinates (4,4)(4,4), and we need to find its coordinates after it has collided with sides 55 times. However, after shooting, the ball goes directly to the corner (5,5)(5,5), and stops there. So we report the coordinates (5,5)(5,5).

fig1
  • Sample Case 22 :

We are given a 55 by 55 board. We shoot the ball from the coordinates (3,1)(3,1), and we need to find its coordinates after it has collided with the sides twice. After shooting, it first hits the right side at (5,3)(5,3), and then the top side at (3,5)(3,5). So, we report (3,5)(3,5).

fig1

Solution

Program Python:

for t in range(int(input())):
    n,k,x,y=map(int,input().split())
    if x==y:
        print(n,n)
    else:
        cor=[]
        if x<y:
            cor=[[x+n-y,n],[n,n-y+x],[y-x,0],[0,y-x]]
        else:
            cor=[[n,y+n-x],[y+n-x,n],[0,x-y],[x-y,0]]
        t=cor[(k-1)%4]
        print(t[0],t[1])

Program Java:

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner (System.in);
		int T= sc.nextInt();
		int res[][]=new int[T][2];
		for (int t=0;t<T;t++ )
		{
		    int n = sc.nextInt();
		    int k = sc.nextInt();
		    int x = sc.nextInt();
		    int y=sc.nextInt();
		    
		    int p[][]=new int[4][4];
		    if(x>y)
		    {
		        p[0][0]=n;
		        p[0][1]=-x+n+y;
		        
		        p[1][0]=-x+n+y;
		        p[1][1]=n;
		        
		        p[2][0]=0;
		        p[2][1]=x-y;
		        
		        p[3][0]=x-y;
		        p[3][1]=0;
		    }
		    else
		    {
		         p[0][1]=n;
		        p[0][0]=x+n-y;
		        
		        p[1][1]=x+n-y;
		        p[1][0]=n;
		        
		        p[2][1]=0;
		        p[2][0]=y-x;
		        
		        p[3][1]=y-x;
		        p[3][0]=0;
		    }
		    if(x==y)
		    {
		        x=n;
		        y=n;
		    }
		    else{
		        k%=4;
		        if(k==0)
		        k=3;
		        else
		        k--;
		        x=p[k][0];
		        y=p[k][1];
		    }
		      res[t][0]=x;
		        res[t][1]=y;
		}
		for (int i=0;i<T;i++)
		{
		    System.out.println(res[i][0] + " " + res[i][1]);
		}
	}
}

Program C++:

#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin>>t;
    while(t--)
    {
        int N;
        int K;
        int x;
        int y;
        cin>>N;
        cin>>K;
        cin>>x;
        cin>>y;

        if(x!=y)
        {

                if(x>y)
                {
                     int a[8];
                a[0]=N,a[1]=N-x+y;
                a[2]=a[1],a[3]=a[0];
                a[4]=0,a[5]=N-a[2];
                a[6]=N-a[2],a[7]=0;
                if(K%4==1)
                cout<<a[0]<<" "<<a[1]<<endl;
                else if(K%4==2)
                cout<<a[2]<<" "<<a[3]<<endl;
                else if(K%4==3)
                cout<<a[4]<<" "<<a[5]<<endl;
                else if(K%4==0)
                cout<<a[6]<<" "<<a[7]<<endl;
                }

                else
                {
                    int a[8];
                a[0]=N-y+x,a[1]=N;
                a[2]=a[1],a[3]=a[0];
                a[4]=N-a[3],a[5]=0;
                a[6]=0,a[7]=N-a[3];
                if(K%4==1)
                cout<<a[0]<<" "<<a[1]<<endl;
                else if(K%4==2)
                cout<<a[2]<<" "<<a[3]<<endl;
                else if(K%4==3)
                cout<<a[4]<<" "<<a[5]<<endl;
                else if(K%4==0)
                cout<<a[6]<<" "<<a[7]<<endl;
                }

            
        }

        else
        {
            cout<<N<<" "<<N<<endl;
        }
        

       
    }
    return 0;
}

January Long Challenge 2021

Leave a Comment