[搜索+剪枝/]poj1054 The Troublesome Frog
来源:互联网 发布:win7虚拟机mac os x 编辑:程序博客网 时间:2024/06/05 04:57
poj1054 The Troublesome Frog
题意:
一块矩形农田,每只蛤会以不同的方式(横/竖/斜)不同的步距跳跃,如果被蛤踩到庄稼就会受损,请问个体蛤损坏庄稼的最大值思路:
两种思路:搜索+剪枝/dp+hash
枚举前两步,确定青蛙的步距(确定斜率),然后搜索,比较慢
听别人说还有一种思路是dp,应该更高效,但是没想出来……
代码1(搜索+剪枝):
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>/*TLE,然后把数据sort一下然后wa了……发现我落了一个条件,就是我们要保证青蛙能跳进去对吧*/using namespace std;const int maxn = 5000+5;struct node{ int x; int y;}E[maxn];bool f[maxn][maxn];int n, m;bool cmp(const node &x, const node &y){ if(x.y==y.y) return x.x<y.x; return x.y<y.y;}int Find(node x, node y, int dis_x, int dis_y){ int re = 0; for(int i=y.x+dis_x,j=y.y+dis_y; i<=n&&i>0 && j<=m&&j>0; i+=dis_x,j+=dis_y) if(f[i][j]) re++; else{ re=0; break;//青蛙跳必须是连续的! } return re;}int main(){ int t; while(~scanf("%d%d", &n, &m)){ scanf("%d", &t); memset(f,false,sizeof(f)); int re = 0; for(int i=0; i<t; i++){ scanf("%d%d", &E[i].x, &E[i].y); f[E[i].x][E[i].y] = true; } sort(E,E+t,cmp); for(int i=0; i<t; i++) for(int j=i+1; j<t; j++){ int dis_x = E[j].x-E[i].x; int dis_y = E[j].y-E[i].y; if((E[i].x-dis_x<=0 || E[j].x-dis_x>n) || (E[i].y-dis_y<=0 || E[j].y-dis_y>m)) re = max(Find(E[i],E[j],dis_x,dis_y),re); } if(re) printf("%d\n", re+2); else printf("0\n"); } return 0;}在AC期间我有一个问题,就是我一开始是让Find()函数中re初始化为2,然后直接输出返回值即可
但是WA了,经过排查终于找到了原因,原来是让re初始化为2,如果不能进入for循环返回值是2,但是如果不能进入for循环说明应该返回0,所以只要在for循环中加一个标记即可,Find()改成如下:
int Find(node x, node y, int dis_x, int dis_y){ int re = 2; int flag = 1; for(int i=y.x+dis_x,j=y.y+dis_y; i<=n&&i>0 && j<=m&&j>0; i+=dis_x,j+=dis_y){ flag = 0; if(f[i][j]) re++; else{ re=0; break;//青蛙跳必须是连续的! } } if(flag) return 0;//如果没进入循环,特判为0 return re;}main()函数中直接输出返回值即可
给一个测试样例:
/*4 423 13 3----0(not 2)*/
0 0
- [搜索+剪枝/]poj1054 The Troublesome Frog
- POJ1054--The Troublesome Frog
- POJ1054 The Troublesome Frog
- POJ1054-The Troublesome Frog
- POJ1054 The Troublesome Frog
- POJ1054 The Troublesome Frog [dp]
- POJ 1054 The Troublesome Frog 搜索+剪枝
- poj-1054-The Troublesome Frog-搜索+剪枝
- poj1054--The Troublesome Frog(枚举+二分)
- [poj1054] The Troublesome Frog 暴力枚举+剪支
- POJ 1054 The Troublesome Frog(dp+剪枝)
- (POJ 1054)The Troublesome Frog <暴力枚举+剪枝 || DP>
- poj 1054 The Troublesome Frog 枚举+二分搜索
- poj 1054 The Troublesome Frog
- POJ--1054--The Troublesome Frog
- POJ 1054 The Troublesome Frog
- POJ - 1054 The Troublesome Frog
- POJ 1054 The Troublesome Frog
- android fragment
- C语言经典算法11
- HTTPS和HTTP的区别
- CSS的相关新属性
- codeforces 725B Food on the Plane
- [搜索+剪枝/]poj1054 The Troublesome Frog
- CodeForces 725A Jumping Ball
- solve: faild to load SELinux policy freezing
- JavaScript 简介
- ubuntu安装Node.js和npm
- Map按键有序输出
- leetcode 438. Find All Anagrams in a String
- 作业调度之短作业优先(实验一)
- C语言经典算法12