理解了题意之后,这个题感觉状态转移还是挺好想的,实现起来确实有点繁琐,代码能力还有待加强,经过很长时间才发现bug。注意坐标可能是负的。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int dp[][][];
bool o[][][];
int n,ans,d;
int gcd(int a,int b)
{
return b == ?a:gcd(b,a%b);
}
int fun(int t,int x,int y,int tx,int ty,int ax,int ay)
{
int temp = ;
while(x != ax||y != ay)//开始这里写成&&
{
if(o[t+][x][y])
temp ++;
x += tx;
y += ty;
}
return temp+o[t+][x][y];
}
void judge(int t,int x,int y)
{
int i,j,g;
for(i = x-d;i <= x+d;i ++)
{
for(j = y-d;j <= y+d;j ++)
{
if((i-x)*(i-x)+(j-y)*(j-y) > d*d) continue;
if(i < ||j < ) continue;
if(i > n+||j > n+) continue;
g = gcd(abs(i-x),abs(j-y));
if(g == ) g = ;
int tx,ty;
tx = (i-x)/g;
ty = (j-y)/g;
dp[t+][i][j] = max(dp[t+][i][j],dp[t][x][y]+fun(t,x,y,tx,ty,i,j));
ans = max(dp[t+][i][j],ans);
}
}
}
int main()
{
int m,i,j,k,y,x,t;
while(scanf("%d%d%d",&n,&d,&m)!=EOF)
{
if(n == &&d == &&m == ) break;
memset(dp,,sizeof(dp));
memset(o,,sizeof(o));
for(i = ; i <= m; i ++)
{
scanf("%d%d%d",&x,&y,&t);
o[t][x+][y+] = ;
}
ans = ;
for(i = ; i <= ; i ++)
{
for(j = ; j < n+; j ++)
{
for(k = ; k < n+; k ++)
{
judge(i,j,k);
}
}
}
printf("%d\n",ans);
}
return ;
}