有点像那道monkey and banana ,要把图给放弃掉的。输入时过滤掉cheese小于(0,0)起点的部分,然后对cheese从大到小排序(这样(0,0)点就被放到了最后一个),m[i]表示以grid[i].x,grid[i].y为起点最多能吃到的cheese量,m[n](n是cheese大于起点的grid的数量)就是结果了。
有个让人火大的地方,就是fatmouse居然笨到不会拐弯,FT!
Accepted | 1107 | C | 00:03.58 | 660K |
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAXN 100
struct s
{
int cheese,x,y;
}grid[MAXN * MAXN];
int cmp(const void* a,const void* b)
{
return ((struct s*)b)->cheese - ((struct s*)a)-> cheese;
}
void solve(int n,int k)
{
int i,j;
int m[MAXN * MAXN],cnt = 1 ;
scanf("%d",&grid[0 ].cheese);
grid[0].x = grid[0].y = 0 ;
for (i = 1; i < n * n; i++ )
{
int temp;
scanf("%d",& temp);
if (temp > grid[0 ].cheese)
{
grid[cnt].x = i % n;
grid[cnt].y = i / n;
grid[cnt].cheese = temp;
cnt++ ;
}
}
if (k == 0) { printf("%d ",grid[0].cheese); return ; }
qsort(grid,cnt,sizeof(grid[0 ]),cmp);
for (i = 0; i < cnt; i++ )
{
m[i] = grid[i].cheese;
for (j = 0; j < i; j++ )
{
if (grid[i].cheese < grid[j].cheese && abs(grid[j].x - grid[i].x) <= k && abs(grid[j].y - grid[i].y) == 0 &&
m[i] < m[j] + grid[i].cheese)
m[i] = m[j] + grid[i].cheese;
if (grid[i].cheese < grid[j].cheese && abs(grid[j].x - grid[i].x) == 0 && abs(grid[j].y - grid[i].y) <= k &&
m[i] < m[j] + grid[i].cheese)
m[i] = m[j] + grid[i].cheese;
}
}
printf("%d/n",m[cnt - 1 ]);
}
void main()
{
int n,k;
#ifndef ONLINE_JUDGE
freopen("1107.txt","r" ,stdin);
#endif
while(scanf("%d%d",&n,&k) != EOF && !(k == -1 && n == -1 ))
solve(n,k);
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAXN 100
struct s
{
int cheese,x,y;
}grid[MAXN * MAXN];
int cmp(const void* a,const void* b)
{
return ((struct s*)b)->cheese - ((struct s*)a)-> cheese;
}
void solve(int n,int k)
{
int i,j;
int m[MAXN * MAXN],cnt = 1 ;
scanf("%d",&grid[0 ].cheese);
grid[0].x = grid[0].y = 0 ;
for (i = 1; i < n * n; i++ )
{
int temp;
scanf("%d",& temp);
if (temp > grid[0 ].cheese)
{
grid[cnt].x = i % n;
grid[cnt].y = i / n;
grid[cnt].cheese = temp;
cnt++ ;
}
}
if (k == 0) { printf("%d ",grid[0].cheese); return ; }
qsort(grid,cnt,sizeof(grid[0 ]),cmp);
for (i = 0; i < cnt; i++ )
{
m[i] = grid[i].cheese;
for (j = 0; j < i; j++ )
{
if (grid[i].cheese < grid[j].cheese && abs(grid[j].x - grid[i].x) <= k && abs(grid[j].y - grid[i].y) == 0 &&
m[i] < m[j] + grid[i].cheese)
m[i] = m[j] + grid[i].cheese;
if (grid[i].cheese < grid[j].cheese && abs(grid[j].x - grid[i].x) == 0 && abs(grid[j].y - grid[i].y) <= k &&
m[i] < m[j] + grid[i].cheese)
m[i] = m[j] + grid[i].cheese;
}
}
printf("%d/n",m[cnt - 1 ]);
}
void main()
{
int n,k;
#ifndef ONLINE_JUDGE
freopen("1107.txt","r" ,stdin);
#endif
while(scanf("%d%d",&n,&k) != EOF && !(k == -1 && n == -1 ))
solve(n,k);
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}