POJ 1054 The Troublesome Frog(枚举+剪枝)

题目链接

题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数。

思路 : 暴力了一下,顺便剪剪枝就可以过。。。。

 1 //POJ1054
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <iostream>
 5 #include <algorithm>
 6
 7 using namespace std ;
 8
 9 struct node{
10 int row,col ;
11 }plant[5010];
12 int ab[5010][5010] ;
13 int r,c,n ;
14
15 bool cmp(const node a,const node b)
16 {
17     if(a.row == b.row)
18         return a.col < b.col ;
19     return a.row < b.row ;
20 }
21 bool judge(int x,int y)
22 {
23     if(x > 0 && x <= r && y > 0 && y <= c) return true ;
24     return false ;
25 }
26
27 int xxxx(int x,int y,int dx,int dy)
28 {
29     int cnt = 0  ;
30     while(judge(x,y))
31     {
32         if(!ab[x][y]) return 0 ;//会出现交叉路径所得
33         cnt ++ ;
34         x += dx ;
35         y += dy ;
36     }
37     return cnt ;
38 }
39
40 int main()
41 {
42     scanf("%d %d %d",&r,&c,&n) ;
43     memset(ab,0,sizeof(ab)) ;
44     for(int i = 0 ; i < n ; i++)
45     {
46         scanf("%d %d",&plant[i].row,&plant[i].col) ;
47         ab[plant[i].row][plant[i].col]  = 1 ;
48     }
49     sort(plant,plant+n,cmp) ;
50     int cnt = 2 ;
51     for(int i = 0 ; i < n ; i++)
52     {
53         for(int j = i+1 ; j < n ; j++)
54         {
55             int dx = plant[j].row-plant[i].row ;
56             int dy = plant[j].col-plant[i].col ;
57             if(plant[i].row + cnt *dx > r || cnt*dy + plant[i].col>c) continue ;//青蛙的起点肯定在稻田外;
58             if(judge(plant[i].row-dx,plant[i].col-dy)) continue ;//该点的直线上的脚印一定要大于上一次的点数;
59             if(!judge(plant[i].row + cnt*dx,plant[i].col+cnt * dy)) continue ;//多于上一点的基础上点要在稻田内;
60             cnt = max(cnt,xxxx(plant[i].row,plant[i].col,dx,dy)) ;
61         }
62     }
63     if(cnt < 3)
64         printf("0\n") ;
65     else printf("%d\n",cnt) ;
66     return 0 ;
67 }

POJ 1054 The Troublesome Frog(枚举+剪枝),布布扣,bubuko.com

时间: 2024-08-24 16:52:24

POJ 1054 The Troublesome Frog(枚举+剪枝)的相关文章

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

POJ - 1054 The Troublesome Frog

题意:给你个矩阵,里面有n个标记的点,许多只青蛙在上面跳,每次跳的距离都是一样的且轨迹是直线,目标是从一边跳到另一边,求最多步数的青蛙 思路:排序后,枚举判断 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 5050; struct point{ int x,y; void i

POJ - 1054 The Troublesome Frog 模拟 枚举优化。

题意:有个R*C的格网.上面有若干个点,这些点可以连成一些直线,满足:这些点在直线上均匀排布(也就是间隔相等),直线的两段穿过网格(也就是第一个,最后一个在网格的边界附近) 求某条直线上最多的点数 题解:先排序,再任取两个水稻,算出之间的dx,dy,然后就能推出前后的水稻坐标,用如果满足路径上一直有水稻(用binarysearch),且第一个点与最后一个点在水稻外面,就是一个可行的解,维护其最大值. 注意一些判断. ac代码: #include<iostream> #include<al

(中等) POJ 1054 The Troublesome Frog,记忆化搜索。

Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, because the frogs jump through your rice paddy at night, flattening rice plants. In the morning, after noting which plants hav

[poj1054] The Troublesome Frog 暴力枚举+剪支

The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 11339 Accepted: 3383 Case Time Limit: 500MS Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, b

poj 1699 Best Sequence (搜索技巧 剪枝 dfs)

题目链接 题意:给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因. 分析:先计算出add数组,再dfs枚举. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio> 6 #include <vector> 7 #include <al

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

POJ 1753 Flip Game (DFS + 枚举)

题目:http://poj.org/problem?id=1753 这个题在开始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每个棋子翻一遍,然后顺利的过了,所以也就没有深究. 省赛前一次做PC2遇到了几乎一模一样的题,只不过是把棋盘的界限由4X4改为了5X5,然后一直跑不出结果来,但是当时崔老湿那个队过了,在最后总结的时候,崔老湿就说和这个题一样,不过要枚举第一行进行优化. 我以为就是恢复第一行然后第二行以此类推,不过手推一下结果是6不是4,就知道这个有问题. 问了崔老湿,问了+

POJ 1161 Walls(最短路+枚举)

POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了最外面的一个),现在,如果某几个小镇上的人想要聚会,为选择哪个区域为聚会地点,可以使他们所有人总共需要穿过的墙数最小,题目上有说明,不在某个点上聚会(聚会点在某个多边形内部),行进过程中不穿过图中的点(也就是除出发点外的其他小镇). 输入第1行代表m(2<=M<=200)个区域 第2行代表n(3&