北大书上的花生问题 经测试 POJ上的无法通过 POJ上的题意看着难度太大 故暂不修改
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
#include<iostream>
2
#include<stdio.h>
3
#include<cmath>
4
using namespace std;
5
int a[21][21];
6
int n,m;
7
int r,rr;
8
int f()
9![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10
int max=0;
11
for(int i=1;i<=n;i++)
12
for(int j=1;j<=m;j++)
13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
if(max<a[i][j])
15![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
max=a[i][j];
17
r=i;
18
rr=j;
19
}
20
}
21
return max;
22
}
23
int main()
24![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
25![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
26
int htime;
27
int l,ll,t,tt;
28
scanf("%d%d%d",&n,&m,&htime);
29
for(int i=1;i<=n;i++)
30
for(int j=1;j<=m;j++)
31![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
scanf("%d",&a[i][j]);
33
}
34
int m=1;
35
int sum=0;
36
while(1)
37![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
f();
39
if(f()==0)
40![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
printf("%d\n",sum);
42
break;
43
}
44
l=r;
45
ll=rr;
46
if(m==1)
47![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
if(ll*2<=htime)
49![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{sum+=a[l][ll];
50
htime=htime-ll;
51
a[l][ll]=0;}
52
else
53![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
printf("%d\n",sum);
55
break;
56
}
57
}
58
else
59![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
if((fabs(l-t)+fabs(ll-tt)+t)<=htime)
61![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
sum+=a[l][ll];
63
htime=htime-fabs(l-t)+fabs(ll-tt);
64
a[l][ll]=0;
65
}
66
else
67![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
printf("%d\n",sum);
69
break;
70
}
71
}
72
}
73
return 0;
74
}