whats wrong with this code?
#include
#include<bits/stdc++.h>
using namespace std;
main()
{
int t,k=1;
cin>>t;
while(t–)
{
int i,x=0,y=0,m,n;
int l,rx,ry;
cin>>m>>n>>rx>>ry;
cin>>l;
char str[l];
//for(i=0;i<l;i++)
cin>>str;
for(i=0;i<l;i++)
{
if(str[i]==‘U’)
y++;
else if(str[i]==‘D’)
y–;
else if(str[i]==‘L’)
x–;
else if(str[i]==‘R’)
x++;
}
if(x==m || x<0 || y<0 || y==n)
cout<<"Case "<<k<<": DANGER"<<endl;
else if(x==rx && y==ry)
cout<<"Case "<<k<<": REACHED"<<endl;
else
cout<<"Case "<<k<<": SOMEWHERE"<<endl;
k++;
}
}
corack
November 20, 2020, 2:16pm
2
Bro first of all next time when you ask any question, please attach the link to the problem so that if anyone is willing to answer you can reach the problem statement.
Now ,
wrong : if(x==m || x<0 || y<0 || y==n)
correct: if(x>m||x<0||y<<0||y>n) //given in problem statemen
corack
November 20, 2020, 3:00pm
4
sorry there is a typo
in if statement
y<<0 change it to y<0. it was by mistake.
corack
November 20, 2020, 3:01pm
5
here is your coe with after the changes and it is correct.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,k=1;
cin>>t;
while(t–)
{
int i,x=0,y=0,m,n;
int l,rx,ry;
cin>>m>>n>>rx>>ry;
cin>>l;
char str[l];
for(i=0;i<l;i++)
cin>>str[i];
for(i=0;i<l;i++)
{
if(str[i]==‘U’)
y++;
else if(str[i]==‘D’)
y–;
else if(str[i]==‘L’)
x–;
else if(str[i]==‘R’)
x++;
}
if(x>m || x<0 || y<0 || y>n)
cout<<"Case "<<k<<": DANGER"<<endl;
else if(x==rx && y==ry)
cout<<"Case "<<k<<": REACHED"<<endl;
else
cout<<"Case "<<k<<": SOMEWHERE"<<endl;
k++;
}
return 0;
}