Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.
/**Bismillahir Rahmanir Rahim**/ #include <stdio.h> #include <math.h> int main() { int tst, i; scanf("%d", &tst); for(i=1; i<=tst; i++) { int r1,c1,r2,c2,r,c; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); if((r1%2==1 && c1%2==0) || (r1%2==0 && c1%2==1)) { if((r2%2==0 && c2%2==0) || (r2%2==1 && c2%2==1)) printf("Case %d: impossible\n", i); else { r = abs(r1-r2); c = abs(c1-c2); if(r==c) printf("Case %d: 1\n", i); else printf("Case %d: 2\n", i); } } else if((r1%2==1 && c1%2==1) || (r1%2==0 && c1%2==0)) { if((r2%2==1 && c2%2==0) || (r2%2==0 && c2%2==1)) printf("Case %d: impossible\n", i); else { r = abs(r1-r2); c = abs(c1-c2); if(r==c) printf("Case %d: 1\n", i); else printf("Case %d: 2\n", i); } } } return 0; }
This comment has been removed by the author.
ReplyDeletewhy we have to mod r1 and c1 by 2?
ReplyDelete#include
ReplyDeleteusing namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;cin>>t;
int c = 1;
while(t--)
{
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
printf("Case %d: ",c);c++;
int x,y;
x = (abs(x1-x2));
y = (abs(y1-y2));
if(x%2==y%2){
if(x==y){
puts("1");
}
else puts("2");
}
else{
puts("impossible");
}
}
}