代码如下:
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 5 void solve(); 6 int L,sx,sy,px,py; 7 8 int main() 9 { 10 while(scanf("%d",&L),L) 11 { 12 scanf("%d%d%d%d",&sx,&sy,&px,&py); 13 14 if(py > L || px > L || py < -L || px < -L) 15 { 16 printf("Out Of Range\n"); 17 continue; 18 } 19 20 solve(); 21 } 22 23 return 0; 24 } 25 26 void solve() 27 { 28 int a = px - sx; 29 int b = py - sy; 30 31 if(a == 0) 32 { 33 if(abs(b) <= 1) 34 { 35 printf("Yes\n"); 36 } 37 else 38 { 39 printf("No\n"); 40 } 41 42 return; 43 } 44 45 if(b == 0) 46 { 47 if(abs(a) <= 1) 48 { 49 printf("Yes\n"); 50 } 51 else 52 { 53 printf("No\n"); 54 } 55 56 return; 57 } 58 59 if(abs(a) == 1 && abs(b) == 1) 60 { 61 printf("Yes\n"); 62 return; 63 } 64 65 int minn = std::min(sx,px); 66 int maxm = std::max(sx,px); 67 68 for(int i = minn + 1;i < maxm;i ++) 69 { 70 int temp = b * i + a * sy - b * sx; 71 if(temp % a == 0) 72 { 73 if(temp / a < std::max(sy,py) && temp / a > std::min(sy,py)) 74 { 75 printf("No\n"); 76 return; 77 } 78 } 79 } 80 81 printf("Yes\n"); 82 }