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; }
相关文章推荐
- PCL 1.7.2 All-in-one Installer MSVC2012 x64
- 从getmemery()函数看内存管理、函数传参等一系列问题
- shell登录模式及其相应配置文件(转)
- Gprinter热敏打印机光栅位图点阵数据解析工具
- 点击listView中的图片,在HorizontalScrollView显示出来,在点击item,图片消失
- VLAN基本通信原理
- Linux 常用到的功能及命令-FAQ
- C语言常见错误
- [lintcode the-smallest-difference]最小差(python)
- 正则表达式理解
- UIview需要知道的一些事情:setNeedsDisplay、setNeedsLayout
- LeetCode
- VM 安装red hat 5 解决中文显示乱码 和 切换中文输入法
- 数据结构(括号序列,线段树||点分治,堆):ZJOI 2007 捉迷藏
- C语言 百炼成钢21
- [Lintcode two-sum]两数之和(python,双指针)
- ios学习路线—iOS高级(NSThread)
- mysql(一)
- Java enum的用法详解
- codeforces 681D(Gifts by the List)