Problem D: Knight's Trip

Problem D: Knight's Trip

时间: 1ms        内存:128M

描述:

Problem D: Knight's Trip

In chess, each move of a knight consists of moving by two squares horizontally and one square vertically, or by one square horizontally and two squares vertically. A knight making one move from location (0,0) of an infinite chess board would end up at one of the following eight locations: (1,2), (-1,2), (1,-2), (-1,-2), (2,1), (-2,1), (2,-1), (-2,-1).

Starting from location (0,0), what is the minimum number of moves required for a knight to get to some other arbitrary location (x,y)?

输入:

Each line of input contains two integers x and y, each with absolute value at most one billion. The integers designate a location (x,y) on the infinite chess board. The final line contains the word END.

输出:

For each location in the input, output a line containing one integer, the minimum number of moves required for a knight to move from (0,0) to (x, y).

示例输入:

1 2
2 4
END

示例输出:

1
2

提示:

参考答案(内存最优[788]):

#include <stdio.h>
#include <math.h>

int n;
double x[1000], y[1000], r[1000];
int top[1000];

int possible = 1;
double ne = 1000, nw = 1000;

main(){
   int i,j,k;
   scanf("%d",&n);
   for (i=0;i<n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&r[i]);
   for (i=0;i<n;i++) if (y[i]+r[i] > 1000) visit(i);
   /*
   if (possible) printf(
     "Bill enters at (0.00, %0.2lf) and leaves at (1000.00, %0.2lf).\n",nw,ne);
   else printf("Bill will be bitten.\n");
   */
   if (possible) printf(
     "0.00 %0.2lf 1000.00 %0.2lf\n",nw,ne);
   else printf("IMPOSSIBLE\n");
}

visit(int i){
   int j,k;
   double yy;
   if (top[i]++) return;

   for (j=0;j<n;j++) {
      if (hypot(x[j]-x[i],y[j]-y[i]) < r[i]+r[j]) visit(j);
   }
   if (y[i]-r[i] < 0) possible = 0;
   if (x[i]-r[i] < 0) {
      yy = y[i] - sqrt(r[i]*r[i] - x[i]*x[i]);
      if (yy < nw) nw = yy;
   }
   if (x[i]+r[i] > 1000) {
      yy = y[i] - sqrt(r[i]*r[i] - (1000-x[i])*(1000-x[i]));
      if (yy < ne) ne = yy;
   }
}

参考答案(时间最优[32]):

#include <stdio.h>
#include <math.h>

int n;
double x[1000], y[1000], r[1000];
int top[1000];

int possible = 1;
double ne = 1000, nw = 1000;

main(){
   int i,j,k;
   scanf("%d",&n);
   for (i=0;i<n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&r[i]);
   for (i=0;i<n;i++) if (y[i]+r[i] > 1000) visit(i);
   /*
   if (possible) printf(
     "Bill enters at (0.00, %0.2lf) and leaves at (1000.00, %0.2lf).\n",nw,ne);
   else printf("Bill will be bitten.\n");
   */
   if (possible) printf(
     "0.00 %0.2lf 1000.00 %0.2lf\n",nw,ne);
   else printf("IMPOSSIBLE\n");
}

visit(int i){
   int j,k;
   double yy;
   if (top[i]++) return;

   for (j=0;j<n;j++) {
      if (hypot(x[j]-x[i],y[j]-y[i]) < r[i]+r[j]) visit(j);
   }
   if (y[i]-r[i] < 0) possible = 0;
   if (x[i]-r[i] < 0) {
      yy = y[i] - sqrt(r[i]*r[i] - x[i]*x[i]);
      if (yy < nw) nw = yy;
   }
   if (x[i]+r[i] > 1000) {
      yy = y[i] - sqrt(r[i]*r[i] - (1000-x[i])*(1000-x[i]));
      if (yy < ne) ne = yy;
   }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注