首页 > 其他 > 详细

USACO 3.2 Spinning Wheels (spin)

时间:2014-02-28 13:59:31      阅读:424      评论:0      收藏:0      [点我收藏+]
/*
Main idea
The key point of this problem is you should note the fact that at 360 second, 
all 5 wheels will go back to his initial state; see 360 * speed is a full period;
So only the time [0,360) is meanningful, and we can mimic the this process by
enumeration;

*/

/*
ID: haolink1
PROG: spin
LANG: C++
*/

//#include <iostream>
#include <fstream>

using namespace std;

class Wheel{
public:
    int speed_,w_num_;
    int w_start_[5];
    int w_end_[5];
};

Wheel wheel[5];
ifstream fin("spin.in");
ofstream cout("spin.out");
bool Check(){
    for(int angle = 0; angle < 360; angle++){
        short counter = 0;
        for(int i = 0; i < 5; i++){
            bool flag = false;
            for(int j = 0; j < wheel[i].w_num_; j++){//Note the condition w_end_[j] < w_start_[j] 
                if((angle >= wheel[i].w_start_[j] && angle <= wheel[i].w_end_[j] && wheel[i].w_end_[j] >= wheel[i].w_start_[j]) ||
                      ((angle <= wheel[i].w_end_[j] || angle >= wheel[i].w_start_[j]) && wheel[i].w_end_[j] < wheel[i].w_start_[j])){
                    flag = true; 
                } 
                if(flag){
                    counter++;
                    break;
                }
            }        
        }
        if(counter == 5){
            return true;
        }
    }
    return false;
}

void Print(int time){
    if(time == -1)
        cout << "none" << endl;
    else cout << time << endl;
}

int main(){
    int extent = 0;
    for(int i = 0; i < 5; i++){
        fin >> wheel[i].speed_;
        fin >> wheel[i].w_num_;
        for(int j = 0; j < wheel[i].w_num_; j++){
            fin >> wheel[i].w_start_[j];
            fin >> extent;
            wheel[i].w_end_[j] = wheel[i].w_start_[j] + extent;
            if(wheel[i].w_end_[j] >= 360)//note, the end angle may bigger than 360; 
                wheel[i].w_end_[j] -= 360;
        }
    }
    
    for(int t = 0; t < 360; t++){
        if(Check()){
            Print(t);
            return 0;
        }
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < wheel[i].w_num_; j++){
                wheel[i].w_start_[j]+=wheel[i].speed_;
                if(wheel[i].w_start_[j] >= 360)
                    wheel[i].w_start_[j] -= 360;
                wheel[i].w_end_[j]+=wheel[i].speed_;
                if(wheel[i].w_end_[j] >= 360)
                    wheel[i].w_end_[j] -= 360;
            } 
        }
    }
    Print(-1);
    return 0;
}

USACO 3.2 Spinning Wheels (spin),布布扣,bubuko.com

USACO 3.2 Spinning Wheels (spin)

原文:http://blog.csdn.net/damonhao/article/details/20046957

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!