USACO Spinning Wheels

1、这是一道模拟题,麻烦在于判断是否输出none。仔细观察会发现转360次后所有轮子回到原位,故只需枚举0到359即可。

/*
ID:mrxy564
PROG:spin
LANG:C++
*/
#include<cstdio>
using namespace std;
int a[5][11],b[5];
void change(int d,int &l,int &r){
    l=(l+d)%360;
 r=(r+d)%360;
}
bool judge(int x,int y,int num){
    if(x>y){
     if(num>=x&&num<=359||num>=0&&num<=y) return true;
 }else{
     if(num>=x&&num<=y) return true;
 }
 return false;
}
int main(){
 freopen("spin.in","r",stdin);
 freopen("spin.out","w",stdout);
    for(int i=0;i<5;i++){
  scanf("%d%d",&b[i],&a[i][0]);
  for(int j=1;j<=a[i][0];j++){
   scanf("%d%d",&a[i][2*j-1],&a[i][2*j]);
   a[i][2*j-1]-=b[i];a[i][2*j]=a[i][2*j-1]+a[i][2*j];
  }
 }
 bool flag=false;
 for(int t=0;t<=359;t++){
     for(int i=0;i<5;i++)
   for(int j=1;j<=a[i][0];j++)
    change(b[i],a[i][2*j-1],a[i][2*j]);
  bool flag1;
  for(int k=0;k<=359;k++){
   for(int i=0;i<5;i++){
    flag1=false;
    for(int j=1;j<=a[i][0];j++){
     if(judge(a[i][j*2-1],a[i][j*2],k)){
      flag1=true;
      break;
     }
    }
    if(!flag1) break;
   }
      if(flag1){
      printf("%d\n",t);
         flag=true;
         break;
   }
  }
  if(flag) break;
 }
 if(!flag) printf("none\n");
 return 0;
}

官方题解:

The key observation for this problem is that after 360 seconds, the wheels have returned to their original locations, so if the wheels don't line up in 360 seconds, they will never line up.

To determine if there is a location through which a light can be shine, mark, for each wheel, which angles between 0 and 359 a light can be shone through. If any location gets marked for all the wheels, then a light can be shone through the entire system. Otherwise, no light can be shone through all the wheels.

#include <stdio.h>
#include <assert.h>
#include <string.h>

int speed[5];      /* speed of each wheel */
int wedgest[5][5]; /* start of each wedge (-1 == no wedge) */
int wedglen[5][5]; /* length of each wedge */

int pos[5];        /* angular position of each wheel */
int t;             /* time (in seconds) since start */

/* (light[deg] >> wid) & 0x1 is true if and only if there
   is a wedge in wheel wid that a light can shine through at
   angle deg */
int light[360];    
 
/* mark all the degrees we can see through wheel w */
void mark_light(int w)
 {
  int lv, lv2; /* loop variables */
  int wpos; /* wedge position */

  for (lv = 0; lv < 5; lv++)
   {
    if (wedglen[w][lv] < 0) /* no more wedges for this wheel */
      break;

    /* start of wedge */
    wpos = (pos[w] + wedgest[w][lv]) % 360;

    for (lv2 = 0; lv2 <= wedglen[w][lv]; lv2++)
     { /* throughout extent of wedge */
      light[wpos] |= (1 << w); /* mark as hole in wheel */
      wpos = (wpos + 1) % 360; /* go to the next degree */
     }
   }
 }

int main(int argc, char **argv)
 {
  FILE *fp;
  FILE *fout;
  int w, f;
  int lv, lv2;

  fp = fopen("spin.in", "r");
  fout = fopen("spin.out", "w");
  assert(fp);
  assert(fout);
  
  /* read in the data */
  for (lv = 0; lv < 5; lv++)
   {
    fscanf (fp, "%d %d", &speed[lv], &w);
    for (lv2 = 0; lv2 < w; lv2++)
      fscanf (fp, "%d %d", &wedgest[lv][lv2], &wedglen[lv][lv2]);

    /* mark the rest of the wedges as not existing for this wheel */
    for (; lv2 < 5; lv2++)
      wedglen[lv][lv2] = -1;
   }

  f = 0;
  while (t < 360) /* for each time step */
   {
    memset(light, 0, sizeof(light));

    /* mark the degrees we can see through each wheel */
    for (lv = 0; lv < 5; lv++)
      mark_light(lv);

    for (lv = 0; lv < 360; lv++)
      if (light[lv] == 31) /* we can shine a light through all five wheels */
        f = 1;

    if (f) break; /* we found a match! */

    /* make a time step */
    t++;
    for (lv = 0; lv < 5; lv++)
      pos[lv] = (pos[lv] + speed[lv]) % 360;
   }

  /* after 360 time steps, all the wheels have returned to their
     original location */
  if (t >= 360) fprintf (fout, "none\n");
  else fprintf (fout, "%i\n", t);

  return 0;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值