1. 程式人生 > >枚舉_百煉 2812 討厭的青蛙

枚舉_百煉 2812 討厭的青蛙

fine 出了 crt fun steps warn -a 百煉 struct

 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 }

枚舉_百煉 2812 討厭的青蛙