优化判断条件的例子:讨厌的青蛙

//优化判断条件的例子:讨厌的青蛙
#include<stdio.h>
#include<stdlib.h>

int r,c,n;
struct PLANT{
	int x,y;
};
PLANT plants[5001];
PLANT plant;
int myCompare(const void *ele1,const void *ele2);
int searchPath(PLANT secPlant,int dX,int dY);
int main()
{
	int i,j,dX,dY,pX,pY,steps,max=1;
	scanf("%d%d",&r,&c);
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d%d",&plants[i].x,&plants[i].y);
	qsort(plants,n,sizeof(PLANT),myCompare);
	for(i=0;i<n-2;i++)
		for(j=i+1;j<n-1;j++)
		{
			dX=plants[j].x-plants[i].x;
			dY=plants[j].y-plants[i].y;
			pX=plants[i].x-dX;
			pY=plants[i].y-dY;
			if(pX<=r&&pX>=1&&pY<=c&&pY>=1)
				continue;
			if(plants[i].x+max*dX>r) break;
			pY=plants[i].y+max*dY;
			if(pY>c||pY<1)
				continue;
			steps=searchPath(plants[j],dX,dY);
			if(steps>max)
				max=steps;
		}
	if(max==2) max=0;
	printf("%d\n",max);
	return 0;
}
int myCompare(const void *ele1,const void *ele2)
{
	PLANT *p1,*p2;
	p1=(PLANT *)ele1;
	p2=(PLANT *)ele2;
	if(p1->x==p2->x) return (p1->y-p2->y);
	return (p1->x-p2->x);
}
int searchPath(PLANT secPlant,int dX,int dY)
{
	PLANT  plant;
	int steps;
	plant.x=secPlant.x+dX;
	plant.y=secPlant.y+dY;
	steps=2;
	while(plant.x<=r&&plant.x>=1&&plant.y<=c&&plant.y>=1)
	{
		if(!bsearch(&plant,plants,n,sizeof(PLANT),myCompare))
		{
			steps=0;
			break;
		}
		plant.x+=dX;
		plant.y+=dY;
		steps++;
	}
	return steps;
}

转载于:https://www.cnblogs.com/javafly/p/6037181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值