poj 1054 搜索

刚没看清题目,跳必须是从外面跳进来,然后跳出去的,一直WA不止

谁说是dp的,完全扯淡

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 5050
struct point
{
	int r,c;
}p[N];
bool mat[N][N];
int r,c,n;
int ans;
bool cmp(point left,point right)
{
	return left.r==right.r?left.c<right.c:left.r<right.r;
}
inline bool inrange(int rr,int cc)
{
	return rr>=1 && rr<=r && cc>=1 && cc<=c;
}

int jump(int st,int ed)
{
	int res=0;
	int dr=p[ed].r-p[st].r;
	int dc=p[ed].c-p[st].c;
	int tr=p[st].r;
	int tc=p[st].c;
	if(!inrange(tr+ans*dr,tc+ans*dc))
		return 0;
	if(inrange(tr-dr,tc-dc))
		return 0;
	while(inrange(tr,tc))
	{
		if(mat[tr][tc])
		{
			tr+=dr;
			tc+=dc;
			res++;
		}
		else return 0;
	}
	return res;
}
int main ()
{
	while(scanf("%d%d",&r,&c)!=EOF)
	{
		memset(mat,0,sizeof(mat));
		scanf("%d",&n);
		for(int i=1;i<=n;++i)
		{
			scanf("%d%d",&p[i].r,&p[i].c);
			mat[p[i].r][p[i].c]=1;
		}
		sort(p+1,p+1+n,cmp);
		ans=0;
		for(int i=1;i<=n;++i)
		{
			for(int j=i+1;j<=n;++j)
				ans=max(ans,jump(i,j));
		}
		if(ans<3)
			printf("0\n");
		else printf("%d\n",ans);
	}
	return 0;
}



你可能感兴趣的:(poj 1054 搜索)