USACO Spinning Wheels 解题报告

2024-03-29 08:32

本文主要是介绍USACO Spinning Wheels 解题报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这道题的题目很难懂。。。虽然我自认为英语还不错。。。同样看不懂题目的同学可以看看这里的解释http://www.byvoid.com/blog/usaco-323-spinning-wheels/

同时那里的做法也非常可取,简单明了,对0~359每个角度对每个轮子逐一判断(当然,要具体到每个轮子的每个缺口)。

大神总能写出简单高效的代码http://belbesy.wordpress.com/2012/08/14/usaco-3-2-3-spinning-wheels/。我在慎重考虑自己是不是改行搬砖去。

我的做法和我看到的做法都不一样。用的是整体的角度,先是一个完整的圆,然后逐一减去每个轮子透不过的区域,剩下的就是能透光的区域,如果这个区域和某一个轮子的所有缺口都不重叠,则这一秒没有光通过。

照理说我这种方法相对于每个角度单独考虑会快一些。但写起来麻烦很多,圆上的边是否相交及相交的区域我不知道有没有简洁高效的方法。我的代码中是一个正确的方法,但是根据两条边是否通过360度逐一分情况考虑的,比较繁琐。

/*
ID: thestor1
LANG: 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;
}


这篇关于USACO Spinning Wheels 解题报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/858110

相关文章

【专题】2024飞行汽车技术全景报告合集PDF分享(附原数据表)

原文链接: https://tecdat.cn/?p=37628 6月16日,小鹏汇天旅航者X2在北京大兴国际机场临空经济区完成首飞,这也是小鹏汇天的产品在京津冀地区进行的首次飞行。小鹏汇天方面还表示,公司准备量产,并计划今年四季度开启预售小鹏汇天分体式飞行汽车,探索分体式飞行汽车城际通勤。阅读原文,获取专题报告合集全文,解锁文末271份飞行汽车相关行业研究报告。 据悉,业内人士对飞行汽车行业

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

usaco 1.3 Calf Flac(暴搜)

思路是暴搜。 需要注意的地方是输入的方法,以及输出时的换行。 代码: /*ID: who jayLANG: C++TASK: calfflac*/#include<stdio.h>#include<string.h>#include<math.h>int main(){freopen("calfflac.in","r",stdin);freopen("calfflac.ou

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

usaco 1.2 Palindromic Squares(进制转化)

考察进制转化 注意一些细节就可以了 直接上代码: /*ID: who jayLANG: C++TASK: palsquare*/#include<stdio.h>int x[20],xlen,y[20],ylen,B;void change(int n){int m;m=n;xlen=0;while(m){x[++xlen]=m%B;m/=B;}m=n*n;ylen=0;whi

usaco 1.2 Name That Number(数字字母转化)

巧妙的利用code[b[0]-'A'] 将字符ABC...Z转换为数字 需要注意的是重新开一个数组 c [ ] 存储字符串 应人为的在末尾附上 ‘ \ 0 ’ 详见代码: /*ID: who jayLANG: C++TASK: namenum*/#include<stdio.h>#include<string.h>int main(){FILE *fin = fopen (

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b