标签:
Description
Input
Output
Sample Input
6 7 14 2 1 6 6 4 2 2 5 2 6 2 7 3 4 6 1 6 2 2 3 6 3 6 4 6 5 6 7
Sample Output
7
Source
1 //By LYLtim 2 //2015.2.3 3 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 class Plant 9 { 10 public: 11 int x, y; 12 bool operator< (const Plant& p2) const { // binary_search 3rd argument const T& val 13 if (x == p2.x) 14 return y < p2.y; 15 return x < p2.x; 16 } 17 }; 18 19 int r, c, n; 20 Plant plants[5000]; 21 22 int searchPath(const Plant secPlant, const int dx, const int dy){ 23 Plant tmpPlant; 24 int steps = 2; 25 tmpPlant.x = secPlant.x + dx; 26 tmpPlant.y = secPlant.y + dy; 27 while( (tmpPlant.x <= r) && (tmpPlant.y >= 1 ) && (tmpPlant.y <= c) ) { 28 if (!binary_search(plants, plants + n, tmpPlant)) { 29 //每一步都必须踩倒水稻才算合理, 否则这就不是一条行走路径 30 steps = 0; 31 break; 32 } 33 steps++; 34 35 //print 36 // cout << tmpPlant.x << "," << tmpPlant.y << endl; 37 38 tmpPlant.x += dx; 39 tmpPlant.y += dy; 40 } 41 return steps; 42 } 43 44 int main() 45 { 46 int dx, dy, px, py, max = 2, steps; 47 cin >> r >> c; 48 cin >> n; 49 for (int i = 0; i < n; i++) 50 cin >> plants[i].x >> plants[i].y; 51 sort(plants, plants + n); 52 53 //print plants 54 // for (int i = 0; i < n; i++) 55 // cout << plants[i].x << " " <<plants[i].y << endl; 56 57 for (int i = 0; i < n-2; i++) 58 for (int j = i+1; j < n-1; j++) { 59 dx = plants[j].x - plants[i].x; 60 dy = plants[j].y - plants[i].y; 61 px = plants[i].x - dx; 62 py = plants[i].y - dy; 63 if ( (px>=1) && (py>=1) && (py<=c)) 64 continue; 65 if (plants[i].x + (max-1)*dx > r) 66 break; 67 py = plants[i].y + (max-1)*dy; 68 if ( (py<1) || (py>c) ) 69 continue; 70 71 //print 72 // cout << "p1:" << plants[i].x << "," << plants[i].y << " p2:"<<plants[j].x << "," << plants[j].y << " dx,dy: " << dx << "," << dy << endl; 73 74 steps = searchPath(plants[j], dx, dy); 75 76 //print steps 77 // cout << "steps:" <<steps << endl << endl; 78 79 if (steps > max) 80 max = steps; 81 } 82 if (max == 2) 83 max = 0; 84 cout << max; 85 return 0; 86 }
IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)
标签:
原文地址:http://www.cnblogs.com/LYLtim/p/4271343.html