UVALive 4954 Lawn mower (简单模拟题)

本文介绍了一种算法,用于判断特定宽度的割草机能否通过给定的横纵坐标覆盖75*100的区域。算法首先对坐标进行排序,然后检查起始位置的有效性,接着遍历所有坐标确保连续覆盖。

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=16406


题目大意:

    给定一块75*100的区域,用一个宽度为w的割草机去割。给定一些横着的中间位置,竖着的中间位置。问这些操作是否能够横着覆盖整块区域,并且竖着覆盖整个区域。可以的话,输出"YES",不能的话输出“NO”。


解题:

    先将这些中间位置排序,看能否连上,不能连上直接break。同时最后看起始位置是否小于等于0,最后的位置是否大于等于75/100。


代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <set>
#define eps 1e-9
using namespace std;
int main()
{
	int x,y;
	double w,hen[1010],shu[1010],maxh,maxs,tmp;
	bool flag;
	while(scanf("%d%d%lf",&x,&y,&w))
	{
		if(x==0&&y==0)break;
        for(int i=0;i<x;i++)
			scanf("%lf",&shu[i]);
		for(int i=0;i<y;i++)
			scanf("%lf",&hen[i]);
		sort(hen,hen+y);
		sort(shu,shu+x);
		maxs=shu[0]+w/2;
		maxh=hen[0]+w/2;
        if(hen[0]-w/2<=0.0)flag=true;
		else flag=false;
		if(flag&&(shu[0]-w/2)<=0.0)flag=true;
		else flag=false;
		if(flag)
		{
           if(x>1)
		   {
			   for(int i=1;i<x;i++)
			   {
                   tmp=shu[i]-w/2;
				   if(tmp-maxs<=0.0)
					   maxs=shu[i]+w/2;
				   else 
				   {
					   flag=false;
					   break;
				   }
			   }
		   }
		   if(flag&&y>1)
		   {
			   for(int i=1;i<y;i++)
			   {
				   tmp=hen[i]-w/2;
				   if(tmp-maxh<=0.0)
					   maxh=hen[i]+w/2;
				   else
				   {
					   flag=false;
					   break;
				   }
			   }
		   }
		   if(flag)
		   {
			   if((maxs-75.0>=0.0)&&(maxh-100.0>=0.0))
				   flag=true;
			   else flag=false;
		   }
		}
		if(flag)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值