成长轨迹55 【ACM算法之路 百炼poj.grids.cn】【枚举】【2812:恼人的青蛙】

 

题目http://poj.grids.cn/practice/2812

打错字母的伤不起啊。。。
【wa代码】

 1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 int r,c,n;
5 struct point
6 {
7 int x;
8 int y;
9 }plants[5050];
10
11 int cmp(const void *a,const void *b)
12 {
13 point * a1=(struct point*)a;
14 point * b1=(struct point*)b;
15 if(a1->x==b1->x)
16 return (a1->y - b1->y);
17 else
18 return (a1->x - b1->x);
19 }
20
21 int deal(point p,int dx,int dy)
22 {
23 point temp;
24 temp.x=p.x+dx;
25 temp.y=p.y+dy;
26 int step=2;
27
28 //之前下面思路错了
29 //如果在地图中找不到明明在地图中应该出现的点
30 //那么就不是符合要求路径了,那就不应该有长度
31 while(temp.x>0&&temp.x<=r&&temp.y>0&&temp.y<=c)
32 {
33 if(!bsearch(&temp,plants,n,sizeof(point),cmp))
34 {
35 step=0;
36 break;
37 }
38 step++;
39 temp.x+=dx;
40 temp.y+=dy;
41
42 }
43 //if(step==2) 这里不用做处理,反正后面出来会判断
44 //step=0;
45 return step;
46 }
47 int main()
48 {
49 scanf("%d %d",&r,&c);
50 //【注意这里各个格子不是从0开始的,也不是在r-1和c-1结束】
51 int max=2;
52 scanf("%d",&n);
53 for(int i=0;i<n;i++)
54 scanf("%d %d",&plants[i].x,&plants[i].y);
55 qsort(plants,n,sizeof(point),cmp);
56 for(int i=0;i<n-1;i++)
57 for(int j=i+1;j<n;j++)
58 {
59 int dx,dy;
60 dx=plants[j].x-plants[i].x;
61 dy=plants[j].y-plants[i].y;
62 point p;
63 p.x=plants[i].x-dx;
64 p.y=plants[i].y-dy;
65 if(p.x>=1&&p.x<=r&&p.y>=1&&p.y<=r)
66 continue;
67 p.x=plants[i].x+max*dx;
68 p.y=plants[i].y+max*dy;
69 if(p.x>r||p.x<=0||p.y>c||p.y<=0)
70 continue;
71
72 int step=deal(plants[j],dx,dy);
73 if(step>max)
74 max=step;
75 }
76 if(max==2)
77 max=0;
78 printf("%d",max);
79 return 0;
80 }

 

【ac代码】

 1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 int r,c,n;
5 struct point
6 {
7 int x;
8 int y;
9 }plants[5050];
10
11 int cmp(const void *a,const void *b)
12 {
13 point * a1=(struct point*)a;
14 point * b1=(struct point*)b;
15 if(a1->x==b1->x)
16 return (a1->y - b1->y);
17 else
18 return (a1->x - b1->x);
19 }
20
21 int deal(point p,int dx,int dy)
22 {
23 point temp;
24 temp.x=p.x+dx;
25 temp.y=p.y+dy;
26 int step=2;
27
28 //之前下面思路错了
29 //如果在地图中找不到明明在地图中应该出现的点
30 //那么就不是符合要求路径了,那就不应该有长度
31 while(temp.x>0&&temp.x<=r&&temp.y>0&&temp.y<=c)
32 {
33 if(!bsearch(&temp,plants,n,sizeof(point),cmp))
34 {
35 step=0;
36 break;
37 }
38 step++;
39 temp.x+=dx;
40 temp.y+=dy;
41
42 }
43 //if(step==2) 这里不用做处理,反正后面出来会判断
44 //step=0;
45 return step;
46 }
47 int main()
48 {
49 scanf("%d %d",&r,&c);
50 //【注意这里各个格子不是从0开始的,也不是在r-1和c-1结束,而分别是1、r和c】
51 int max=2;
52 scanf("%d",&n);
53 for(int i=0;i<n;i++)
54 scanf("%d %d",&plants[i].x,&plants[i].y);
55 qsort(plants,n,sizeof(point),cmp);
56 for(int i=0;i<n-1;i++)
57 for(int j=i+1;j<n;j++)
58 {
59 int dx,dy;
60 dx=plants[j].x-plants[i].x;
61 dy=plants[j].y-plants[i].y;
62 point p;
63 p.x=plants[i].x-dx;
64 p.y=plants[i].y-dy;
65 if(p.x>=1&&p.x<=r&&p.y>=1&&p.y<=c)//【这里怎么打成p.y<=r了。。】
66 continue;
67 p.x=plants[i].x+max*dx;
68 p.y=plants[i].y+max*dy;
69 if(p.x>r||p.x<=0||p.y>c||p.y<=0)
70 continue;
71
72 int step=deal(plants[j],dx,dy);
73 if(step>max)
74 max=step;
75 }
76 if(max==2)
77 max=0;
78 printf("%d\n",max);
79 return 0;
80 }

 

posted @ 2012-02-22 17:42  MooreZHENG  阅读(280)  评论(0编辑  收藏  举报