您的位置:首页 > 编程语言

POJ 1925 别人家的代码是厉害啊~

2014-03-23 00:48 218 查看
TTTT,T死我了,这么水的DP。。。。

原来是。。。!(看了别人家的代码才知道!!而且看了好多遍。。)坐标乘的时候会超INT 啊。。

好吧。。。

这题思路:

首先,蜘蛛侠Y坐标不会变。

然后从一个点通过一个柱子到另一个点,就是 x 坐标对柱子做一次轴对称。

枚举的时候,从每个柱子哪里开始枚举,直到前面的点不能转移到该柱子。

从别人家的代码。。。还学到。。。要给数组赋值为 0x3f3f3f3f 的时候可以直接 memset(a,0x3f,sizeof(a))

#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>

using namespace std;

#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define MII map<int,int>::iterator
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(),x.end()
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ROOT 0,n-1,1
#define PB push_back
#define FOR(a,b,c) for(int a=b;a<c;a++)
#define MOD 1000000007
#define keyTree (ch[ ch[root][1] ][0])
#define MAX 1111111

int dp[MAX];
ll X[10000],Y[10000];
ll y;
int main()
{
int cas;
READ;
scanf("%d",&cas);
while(cas--)
{
//fill(dp,dp+MAX,INF);
MS(dp,0x3f);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld%lld",&X[i],&Y[i]);
y=Y[0];
dp[X[0]]=0;
for(int i=1;i<n;i++)
{
for(ll x=X[i]-1;x>=X[0];x--)
{
ll dx=x-X[i];
ll dy=y-Y[i];
if(dx*dx+dy*dy>Y[i]*Y[i])
break;
if(dp[x]==INF)
continue;
int tar=2*X[i]-x;
if(tar<X[n-1])
dp[tar]=min(dp[tar],dp[x]+1);
else
dp[X[n-1]]=min(dp[X[n-1]],dp[x]+1);
}
}
if(dp[X[n-1]]==INF)
puts("-1");
else
cout<<dp[X[n-1]]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: