枚举_百炼 2812 讨厌的青蛙

 1 #define _CRT_SECURE_NO_WARNINGS  
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <stdlib.h>
 6 #include <vector>
 7 #include <map>
 8 #include <queue>
 9 #include <string>
10 #include <iostream>
11 #include <ctype.h>
12 #include <string.h>
13 #include <set>
14 #include <stack>
15 #include<functional>
16 using namespace std;
17 #define Size 5005
18 #define maxn  1<<30
19 #define minn  1e-6
20 /*
21 枚举的剪枝
22 */
23 struct E{
24     int x, y;
25 }a[Size];
26 int row, col,n;
27 int  cmp(const void*t, const void *s){
28     E *p1, *p2;
29     p1 = (E*)t;
30     p2 = (E*)s;
31     if (p1->x != p2->x) return p1->x - p2->x;
32     else return p1->y - p2->y;
33 }
34 int search(E t, int dx, int dy){
35     E temp;
36     temp.x = t.x + dx;
37     temp.y = t.y + dy; 
38     int steps = 2;
39     while (temp.x > 0 && temp.x <= row && temp.y > 0 && temp.y <= col){
40         if (!bsearch(&temp,a,n,sizeof(E),cmp)){
41             steps = 0;
42             break;
43         }
44         temp.x += dx;
45         temp.y += dy;
46         steps++;
47     }
48     return steps;
49 }
50 void solve(){
51     qsort(a, n, sizeof(E), cmp);
52     int maxi = 2, steps;
53     for (int i = 0; i < n - 1; i++)
54         for (int j = i + 1; j < n; j++){
55             E temp;
56             int dx = -a[i].x + a[j].x;
57             int dy = -a[i].y + a[j].y;
58             temp.x = a[i].x + maxi*dx;
59             temp.y = a[i].y + maxi*dy;
60             int px = a[i].x - dx;
61             int py = a[i].y - dy;
62             if (px>0&&px<=row&&py>0&&py<=col) continue;//如果上一个在稻田里面,那么说明这种情况已经枚举过了
63             if (temp.x > row) break;//如果和显存的最大一样的时候就以及超出了稻田的范围那么就 不用再判断了,肯定小于目前的答案
64             if (temp.y > col || temp.y <= 0) continue;//如果,y超出了界限肯定不是正确答案,注意,x的斜率一定是小于1的因为排序是按照x的非递减序列拍的,而y可以说是无序的,所以有斜率大于零与小于零的两种情况
65             steps = search(a[j], dx, dy);
66             if (steps > maxi) maxi = steps;
67 
68         }
69     if (maxi == 2) maxi = 0;
70     printf("%d\n", maxi);
71     return;
72 }
73 
74 int main(){
75     cin >> row >> col;
76     cin >> n;
77     for (int i = 0; i < n; i++){
78         cin >> a[i].x >> a[i].y;
79     }
80     solve();
81     system("pause");
82 }

 

posted @ 2018-01-30 23:06  MapReduce  阅读(162)  评论(0编辑  收藏  举报