QueLink : CodeChef: Practical coding for everyone
Explanation: As in question it is clearly mentioned that in each row and in each column there is only a single block, (cell which we cannot cross) , so now how many different scenarios are possible,
we cannot reach our destination if we have all blocks arranged (as in question says every row or column contain a single block) , on a diagonal.
In the Below image we can clearly see that how many different diagonals can be possible, each diagonal colored with the same color.
So just each diagonal coordinate present in given blocks, if yes that means we cannot reach our destination, means that diagonal will block our whole path to reach the destination.
lli tt =1;
cin>>tt;
while(tt--)
{
lli n;
cin>>n;
set<pair<int,int>>st;
loop(i,n)
{
lli x,y;
cin>>x>>y;
st.insert({x,y});
}
bool f = false;
for(int i=1;i<=n;i++)
{
bool flag=false;
lli row = i;
lli col = 1;
while(row>=1)
{
if(st.find({row,col})==st.end())
{
flag=true;
break;
}
row--;
col++;
}
if(flag==false){
f=true;
break;
}
}
for(int j=1;j<n;j++)
{
bool flag=false;
lli row = n;
lli col = j;
while(col<=n)
{
if(st.find({row,col})==st.end())
{
flag=true;
break;
}
row--;
col++;
}
if(flag==false){
f=true;
break;
}
}
if(f)
print("NO");
else
print("YES");
}
Solution link : CodeChef: Practical coding for everyone