您的位置:首页 > 其它

poj 1054 技巧枚举

2016-06-18 23:14 337 查看
</pre><pre name="code" class="cpp">#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
const int M = 5100;
typedef struct{
int x;
int y;
}Node;
Node node[M];
int n,r,c,ans=2;
bool operator <(const Node &a,const Node &b)
{
if(a.x==b.x) return a.y<b.y;

return a.x<b.x;
}
int Search(Node p,int dx,int dy) //从第二个点开始找path
{
int k,step=2;
Node t;
t.x=p.x+dx;
t.y=p.y+dy;
while(t.x>=1&&t.x<=r&&t.y>=1&&t.y<=c)
{

if(binary_search(node,node+n,t)) //判断下一步是否合法
{
step++;
t.x+=dx;
t.y+=dy;
}
else
{
step=0;
break;
}
}
return step;
}

int main()
{
cin>>r>>c>>n;
for(int i=0;i<n;i++)
{
cin>>node[i].x>>node[i].y;

}
sort(node,node+n); // 按x从小到大排序

for(int i=0;i<n-1;i++) // 枚举前两步 -> 就能确定 方向 和 步长
{
for(int j=i+1;j<n;j++)
{
int dx=(node[j].x-node[i].x);
int dy=(node[j].y-node[i].y); //

int px=node[i].x-dx; //前一个点
int py=node[i].y-dy;

if(px>=1&&px<=r&&py>=1&&py<=c)
{
continue;   	 //如果第一个点的前一个点在Paddy内 则说明当前步长太小  用一步从圈外跳不到第一点
}
//dx为正:, starting outside the paddy on one side and ending outside the paddy on the other side
if(dx&&node[i].x+dx*(ans-1)>r) 	//最优解cut (从第一点hop ans-1步在x方向上越界)
{
break; // x递增 后面的点肯定也不行
}
if(node[i].y+dy*ans>c||node[i].y+dy*ans<1) // y方向越界
{
continue;
}
ans=max(ans,Search(node[j],dx,dy));
}
}
if(ans<=2)
{
ans=0;
}
cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: