Problem C - Snakes

Problem C - Snakes

时间: 1ms        内存:128M

描述:

Problem C - Snakes

Buffalo Bill wishes to cross a 1000x1000 square field. A number of snakes are on the field at various positions, and each snake can strike a particular distance in any direction. Can Bill make the trip without being bitten?

Assume that the southwest corner of the field is at (0,0) and the northwest corner at (0,1000). The input consists of a line containing n <= 1000, the number of snakes. A line follows for each snake, containing three real numbers: the (x,y) location of the snake and its strike distance. The snake will bite anything that passes closer than this distance from its location.

Bill must enter the field somewhere between the southwest and northwest corner and must leave somewhere between the southeast and northeast corners.

If Bill can complete the trip, give coordinates at which he may enter and leave the field. If Bill may enter and leave at several places, give the most northerly. If there is no such pair of positions, print "Bill will be bitten."

输入:

输出:

示例输入:

3
500 500 499
0 0 999
1000 1000 200

示例输出:

Bill enters at (0.00, 1000.00) and leaves at (1000.00, 800.00).

提示:

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

/* use of random() portable among:

   Solaris, SunOS, DECUnix, Linux

*/

#include <stdio.h>
#include <stdlib.h>

char jane[200], john[200];
int janei, johni;

main(){
   int i,j;
   gets(jane);
   gets(john);
   for (j=janei=johni=0;j<1000 && strlen(jane)&&strlen(john);j++){
      if (jane[janei] == john[johni]) {
         if (random()/141%2 == 0) { /* jane wins */
            printf("Snap! for Jane: ");
            for (i=strlen(jane);i>janei;i--) jane[i+johni+1] = jane[i];
            strncpy(jane+janei+1,john,johni+1);
            strcpy(john,john+johni+1);
            janei = janei + johni + 2;
            johni = 0;
            for (i=janei-1;i>=0;i--) printf("%c",jane[i]);
            printf("\n");
         }else{  /* john wins */ 
            printf("Snap! for John: ");
            for (i=strlen(john);i>johni;i--) john[i+janei+1] = john[i];
            strncpy(john+johni+1,jane,janei+1);
            strcpy(jane,jane+janei+1);
            johni = johni + janei + 2;
            janei = 0;
            for (i=johni-1;i>=0;i--) printf("%c",john[i]);
            printf("\n");
         }
      }else{
         janei++; johni++;
      }
      if (!jane[janei]) janei = 0;
      if (!john[johni]) johni = 0;
   }
   if (!strlen(jane)) printf("John wins.\n");
   else if (!strlen(john)) printf("Jane wins.\n");
   else printf("No winner after %d turns\n",j);
}

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

/* use of random() portable among:

   Solaris, SunOS, DECUnix, Linux

*/

#include <stdio.h>
#include <stdlib.h>

char jane[200], john[200];
int janei, johni;

main(){
   int i,j;
   gets(jane);
   gets(john);
   for (j=janei=johni=0;j<1000 && strlen(jane)&&strlen(john);j++){
      if (jane[janei] == john[johni]) {
         if (random()/141%2 == 0) { /* jane wins */
            printf("Snap! for Jane: ");
            for (i=strlen(jane);i>janei;i--) jane[i+johni+1] = jane[i];
            strncpy(jane+janei+1,john,johni+1);
            strcpy(john,john+johni+1);
            janei = janei + johni + 2;
            johni = 0;
            for (i=janei-1;i>=0;i--) printf("%c",jane[i]);
            printf("\n");
         }else{  /* john wins */ 
            printf("Snap! for John: ");
            for (i=strlen(john);i>johni;i--) john[i+janei+1] = john[i];
            strncpy(john+johni+1,jane,janei+1);
            strcpy(jane,jane+janei+1);
            johni = johni + janei + 2;
            janei = 0;
            for (i=johni-1;i>=0;i--) printf("%c",john[i]);
            printf("\n");
         }
      }else{
         janei++; johni++;
      }
      if (!jane[janei]) janei = 0;
      if (!john[johni]) johni = 0;
   }
   if (!strlen(jane)) printf("John wins.\n");
   else if (!strlen(john)) printf("Jane wins.\n");
   else printf("No winner after %d turns\n",j);
}

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

点赞

发表评论

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