Usaco spinning wheels solution report

Source: Internet
Author: User

The question is hard to understand... Although I think English is good... Also do not understand the questions of the students can look at the interpretation of http://www.byvoid.com/blog/usaco-323-spinning-wheels/ here

At the same time, the practice there is also very desirable, simple and clear, for 0 ~ 359 judge each wheel one by one from each angle (of course, to be specific to each gap of each wheel ).

Great gods can always write simple and efficient code at http://belbesy.wordpress.com/2012/08/14/usaco -3-2-3-spinning-wheels /. I am carefully considering whether I want to change my skills.

My practices are different from what I see. The overall angle is used. First, a complete circle is used, and then each area of the wheel is subtracted one by one. The rest is the transparent area, if this area does not overlap all the gaps on a certain wheel, the second will not pass through.

It is reasonable to say that this method is faster than each other. But it is a lot of trouble to write. I don't know whether the edges on the circle are intersecting or not. My code is a correct method, but it is cumbersome to consider whether two sides pass 360 degrees one by one.

/*ID: thestor1LANG: C++TASK: spin*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <cassert>#include <string>#include <algorithm>using namespace std;struct Wedge{int angle, extent;};struct Wheel{int speed;int nWedge;vector<Wedge> wedges;};bool check(const Wheel* const wheels, int iWheel, const int nWheel, const int intersectAngle, const int intersectExtent){if(iWheel >= nWheel){return true;}for(int i = 0; i < wheels[iWheel].nWedge; ++i){int iangle = wheels[iWheel].wedges[i].angle;int iextent = wheels[iWheel].wedges[i].extent;int resAngle; int resExtent;if(iextent == 359){resAngle = intersectAngle;resExtent = intersectExtent;}else if(intersectExtent == 359){resAngle = iangle;resExtent = iextent;}else{int intersectEnd = intersectAngle + intersectExtent;int iend = iangle + iextent;if(intersectEnd >= 360){intersectEnd -= 360;if(iend >= 360){iend -= 360;resAngle = max(iangle, intersectAngle);resExtent = (min(iend, intersectEnd) + 360 - resAngle) % 360;}else{if(intersectEnd < iangle){if(intersectAngle > iend){continue;}resAngle = intersectAngle;resExtent = iend - resAngle;}else{if(check(wheels, iWheel + 1, nWheel, iangle, intersectEnd - iangle)){return true;}if(intersectAngle > iend){continue;}resAngle = intersectAngle;resExtent = iend - resAngle;}}}else{if(iend >= 360){iend -= 360;if(iend < intersectAngle){if(iangle > intersectEnd){continue;}resAngle = iangle;resExtent = intersectEnd - resAngle;}else{if(check(wheels, iWheel + 1, nWheel, intersectAngle, iend - intersectAngle)){return true;}if(iangle > intersectEnd){continue;}resAngle = iangle;resExtent = intersectEnd - resAngle;}}else{if(iangle > intersectEnd || iend < intersectAngle){continue;}resAngle = max(iangle, intersectAngle);resExtent = min(iend, intersectEnd) - resAngle;}}}if(check(wheels, iWheel + 1, nWheel, resAngle, resExtent)){return true;}}return false;}bool check(Wheel *wheels, const int nWheel){return check(wheels, 0, nWheel, 0, 359);}int main(){FILE *fin  = fopen ("spin.in", "r");FILE *fout = fopen ("spin.out", "w");//freopen("log.txt", "w", stdout);const int nWheel = 5;Wheel wheels[nWheel];for(int i = 0; i < nWheel; ++i){fscanf(fin, "%d", &wheels[i].speed);fscanf(fin, "%d", &wheels[i].nWedge);for(int j = 0; j < wheels[i].nWedge; ++j){Wedge wedge;//int angle, extent;fscanf(fin, "%d%d", &wedge.angle, &wedge.extent);wheels[i].wedges.push_back(wedge);}}int t = 360; for(t = 0; t < 360; ++t){if(check(wheels, nWheel)){break;}for(int i = 0; i < nWheel; ++i){for(int j = 0; j < wheels[i].nWedge; ++j){wheels[i].wedges[j].angle += wheels[i].speed;wheels[i].wedges[j].angle %= 360;}}}if(t < 360){fprintf(fout, "%d\n", t);}else{fprintf(fout, "none\n", t);}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.