时间限制: 1 Sec 内存限制: 128 MB
题目描述
最近Smart家闹鼠灾,弄得Smart十分恼火。为了解决老鼠的问题,Smart根据老鼠的特点想出了一个方法。假设Smart的家是一个n*n的格子,每个格子都有一定的食物,数量在0到100之间。
经过观察,老鼠的窝在(1,1)的位置,老鼠吃东西有个特点,到哪个地方,就把这个地方的食物都吃掉,而且每次都比上一次吃的食物要多,因此它们总会有个停止的地方,而且,这些老鼠一次最多可以跳m格,不过只能按x轴或y轴方向来跳。
现在,Smart给出食物的分布,他想知道一只老鼠最多可以吃到多少食物。
输入
第一行两个整数n和m,表示n*n的格子,老鼠一次最多跳m格。
接下来的n行,每行n个数,表示这个方格上的食物数量。
输出
输出一个整数,表示一只老鼠最多可以吃到的食物。
样例输入
3 1
1 2 5
10 11 6
12 12 7
样例输出
37
提示
样例解释:(1,1)→(1,2)→(1,3)→(2,3)→(2,2)→(3,2),1+2+5+6+11+12=37。
【数据范围】
30%的数据:1≤n≤20,2≤m≤4。
100%的数据:1≤n≤100,0≤m≤n。
代码:
#include<iostream>
#include<string>
#include<map>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define INF 0x3f3f3f3f
#define P pair<int,int>
using namespace std;
typedef long long ll;
const double eps=1e-6;
const int N=1e5+10;
const int maxn=2e9;
const int mod=1e9+7;
const int d[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,maxx,f[110][110],p[110][110];
void dfs(int x,int y)
{
for(int i=1;i<=m;i++)
{
for(int j=0;j<4;j++)
{
int xx=x+d[j][0]*i;
int yy=y+d[j][1]*i;
if(xx<=0||xx>n||yy<=0||yy>n) continue;
if(p[xx][yy]<=p[x][y]) continue;
if(f[xx][yy]>=f[x][y]+p[xx][yy]) continue;
f[xx][yy]=f[x][y]+p[xx][yy];
dfs(xx,yy);
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>p[i][j];
}
f[1][1]=p[1][1];
dfs(1,1);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
maxx=max(maxx,f[i][j]);
}
}
cout<<maxx;
return 0;
}