Sun. Jul 3rd, 2022

# Three Way Communications Codechef Solution

#### Bydivya katoch

Nov 6, 2021

The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they cannot communicate directly.

The Chef invested in top-of-the-line transceivers which have a few advanced features. One is that even if two people cannot talk directly because they are out of range, if there is another transceiver that is close enough to both, then the two transceivers can still communicate with each other using the third transceiver as an intermediate device.

There has been a minor emergency in the Chef’s restaurant and he needs to communicate with both the head server and the sous-chef right away. Help the Chef determine if it is possible for all three people to communicate with each other, even if two must communicate through the third because they are too far apart.

### Input

The first line contains a single positive integer T ≤ 100 indicating the number of test cases to follow. The first line of each test case contains a positive integer R ≤ 1,000 indicating that two transceivers can communicate directly without an intermediate transceiver if they are at most R meters away from each other. The remaining three lines of the test case describe the current locations of the Chef, the head server, and the sous-chef, respectively. Each such line contains two integers X,Y (at most 10,000 in absolute value) indicating that the respective person is located at position X,Y.

### Output

For each test case you are to output a single line containing a single string. If it is possible for all three to communicate then you should output “yes”. Otherwise, you should output “no”.

To be clear, we say that two transceivers are close enough to communicate directly if the length of the straight line connecting their X,Y coordinates is at most R.

### Example

```Input:
3
1
0 1
0 0
1 0
2
0 1
0 0
1 0
2
0 0
0 2
2 1

Output:
yes
yes
no```

## Three Way Communications   – CodeChef Solution in JAVA

```import java.util.Scanner;

class COMM3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int r = sc.nextInt();
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int x3 = sc.nextInt();
int y3 = sc.nextInt();
double dist12 = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2));
double dist13 = Math.sqrt(Math.pow(x3-x1, 2) + Math.pow(y3-y1, 2));
double dist23 = Math.sqrt(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2));
if(dist12 > r && (dist13 > r || dist23 > r)) {
System.out.println("no");
continue;
}
if(dist13 > r && (dist12 > r || dist23 > r)) {
System.out.println("no");
continue;
}
if(dist23 > r && (dist12 > r || dist13 > r)) {
System.out.println("no");
continue;
}
System.out.println("yes");
}
}
}
```

## Three Way Communications – CodeChef Solution in CPP

```#include<bits/stdc++.h>
using namespace std;

#define                  ll long long
#define SHIVAM           ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define CHAUHAN          int t;cin>>t;while(t--)
#define pb               push_back
#define ff               first
#define ss               second
#define MOD              1000000007
#define pob              pop_back
#define mp               make_pair
#define vi               vector<ll int>
#define mii              map<ll int,ll int>
#define all(x)          (x).begin(), (x).end()
#define precision(a)    cout << setprecision(a) << fixed
#define square(x)       ((x)*(x))
#define cube(x)         ((x)*(x)*(x))
#define SORT_A(v)                                                                \
{ std::sort((v).begin(), (v).end()); }
#define SORT_D(v)                                                                \
{ std::sort((v).rbegin(), (v).rend()); }

bool is_prime(ll int n)
{
if(n<=1)
return false;
for(int i=2;i<n;n++)
if(n%i==0)
return false;

return true;
}
ll int gcd(ll int a,ll int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
/*             LCM= a*b/gcd                */
string sort_string(string str)
{
SORT_A(str);
return str;
}
void set_precision(double n,int x)
{
cout<<fixed<<showpoint;
cout<<setprecision(x);
}
ll int power(ll int x,ll int y)
{
ll int res=1;
while(y!=0)
{
if(y%2==1)
{
res=res*x;
}
y=y>>1;// y=y/2
x=(x*x);
}
return res;
}
bool isPerfectSquare(double x)
{
if(x>=0)
{
ll sr=sqrt(x);
return(sr*sr==x);
}
else
{
return false;
}
}

ll int gcd_of_array(ll int arr[],ll int size)
{
ll int gcd=arr[0];
for(int i=1;i<size;i++)
{
gcd=__gcd(gcd,arr[i]);
}
return gcd;
}
bool isPowerOfTwo(int n)
{
if(n==0)
{
return false;
}
return (ceil(log2(n))==floor(log2(n)));
}
bool isPalindrome(string str)
{
int l= 0;
int h = str.length() - 1;
while (h > l)
{
if (str[l++] != str[h--])
{
return false;
}
}
return true;
}
ll fastModuloExponentiation(ll a,ll b,ll M)
{
ll result=1;
while(b>0){
if(b&1){
result=(result*a)%M;}

a=(a*a)%M;

b=b>>1;
}
return result;

}
/* -----------------------------------------------------------------------------*/
int sDistance(int x1,int y1,int x2,int y2)
{
return((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void solve()
{
int r;
cin>>r;
int x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int c=0;
if(sDistance(x1,y1,x2,y2)<=r*r)
{
c++;
}
if(sDistance(x2,y2,x3,y3)<=r*r)
{
c++;
}
if(sDistance(x1,y1,x3,y3)<=r*r)
{
c++;
}
if(c>=2)
{
cout<<"yes"<<"\n";
}
else
{
cout<<"no"<<"\n";
}
}
int main()
{
SHIVAM
CHAUHAN{
solve();
}

}

```

## Three Way Communications -CodeChef Solution in Python

```import math
def dist(x1,y1,x2,y2,x3,y3):
AB = math.sqrt((abs(x2-x1))**2 + (abs(y2-y1))**2)
BC = math.sqrt((abs(x2-x3))**2 + (abs(y2-y3))**2)
AC = math.sqrt((abs(x3-x1))**2 + (abs(y3-y1))**2)
return([AB,BC,AC])
n= int(input())
for _ in range(n):
r = int(input())
x1,y1 = map(int,input().split())
x2,y2 = map(int,input().split())
x3,y3 = map(int,input().split())
AB,BC,AC = dist(x1,y1,x2,y2,x3,y3)
if((AB<=r and BC<=r) or AC<=r):
print("yes")
else:
print("no")
```

Disclaimer: The above Problem (Three Way Communications) is generated by CodeChef but the solution is provided by Codeworld19.This tutorial is only for Educational and Learning purpose.