acm ----枚举 ----problem 5
fuck。。。。打错一个变量名害我在这题上用了n长时间,我就搞不懂了。。怎么debug都不会么????!!!!!
tmd。。。好好分析这种题目。。然后做一篇关于debug的总结。
holy shit!!!
#include <cstdlib> #include <iostream> /** * PROBLEM 5 讨厌的青蛙 */ using namespace std; /** * this struct is used to store trampled rice *------------------------------------------------ * enumerate each pair of points in all points */ struct Plant { int x; int y; }; int r,c,n; Plant plants[5001]; int searchpath(Plant select,int dx,int dy); int cmp(const void *ele1, const void *ele2); int main(int argc, char *argv[]) { int dX, dY; int pX, pY; int steps; int max; max=2; scanf("%d%d",&r,&c); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&plants[i].x,&plants[i].y); } qsort(plants,n,sizeof(Plant),cmp);// STL sort the input ascending order for(int i=0;i<n-2;i++) //at least 3 point in a line, so maximum i is n-3 { for(int j=i+1;j<n-1;j++) { //plant[i] is the starting point within the matrix cout<<plants[i].x<<plants[i].y<<endl; dX=plants[j].x-plants[i].x; dY=plants[j].y-plants[i].y; //!!!!!!!fuck!!!!!!!!!mlgb的写错了。。 pX=plants[i].x-dX;// the frog's starting point is out of border pY=plants[i].y-dY; if(pX<=r&&pX>=1&&pY>=1&&pY<=c)//if the frog's starting point is NOT out { continue; } //of border, then continue if(plants[i].x+dX*max>r) { break; } pY=plants[i].y+dY*max; if(pY>c||pY<1) { continue; } steps=searchpath(plants[j],dX,dY); //after "steps", overstep the border if(steps>max) { max=steps; } } } if(max==2) max=0; printf("%d\n",max); system("PAUSE"); return EXIT_SUCCESS; } int cmp(const void *ele1, const void *ele2) //comparision function { Plant *p1, *p2; p1=(Plant *)ele1; p2=(Plant *)ele2; if(p1->x==p2->x) return (p1->y-p2->y); return (p1->x-p2->x); } int searchpath(Plant select,int dx,int dy) { Plant plant; int step; step=2; plant.x=select.x+dx; plant.y=select.y+dy; while(plant.x<=r&&plant.x>=1&&plant.y<=c&&plant.y>=1) { if(!bsearch(&plant,plants,n,sizeof(Plant),cmp))//binary search STL, find the plant { step=0; //the path is not available break; } plant.x+=dx; plant.y+=dy; step++; } return step; }
杯具啊。。。我真是差好多好多。。就这现在这鸟样就别说自己是学计算机的了。。