757 - Gone Fishing
Posted: Wed Nov 06, 2002 5:15 am
I dont know what's wrong with my code. I am using dynamic programming. Upon submission I get runtime error, invalid memory reference. But I think I allocated enough memory. Can anyone tell me what's wrong or perhaps provide some good test data? Thanks.
[cpp]
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define maxn 30
#define maxt 200
int a[maxn][maxt],l[maxn][maxt],s[maxn][maxt],p[maxn][maxt];
int t[maxn],f[maxn],d[maxn];
int tt[maxn][maxn];
int n,h;
void input(){
int i;
cin >> h;
for (i=0;i<n;i++)
cin >> f;
for (i=0;i<n;i++)
cin >> d;
for (i=0;i<n-1;i++)
cin >> t;
}
void process(){
int i,j,k;
h*=12;
// time from one lake to another
for (i=0;i<n;i++){
k=0;
for (j=i;j<n-1;j++){
k+=t[j];
tt[j+1]=k;
}
}
k=f[0];
memset(a,0,sizeof(a));
memset(l,0,sizeof(l));
int prevt,ct,v,ct2;
l[0][0]=f[0];
for (i=0;i<n;i++){
// move here from previous lake
for (ct=h;ct>=0;ct--){
for (j=0;j<i;j++){
prevt=ct-tt[j]-1;
if (prevt<0) continue;
// move here from a previous lake
if (a[j][prevt]+f>a[ct]){
a[ct]=a[j][prevt]+f;
l[ct]=max(0,f[i]-d[i]);
p[i][ct]=j;
s[i][ct]=ct-1;
// from current position
for (ct2=ct+1;ct2<=h;ct2++){
v=min(d[i],l[i][ct2-1]);
if (a[i][ct2-1]+l[i][ct2-1]>a[i][ct2]){
a[i][ct2]=a[i][ct2-1]+l[i][ct2-1];
l[i][ct2]=l[i][ct2-1]-v;
p[i][ct2]=j;
s[i][ct2]=ct-1;
}
}
}
}
}
// from current position
if (i==0)
for (ct2=1;ct2<=h;ct2++){
v=min(d[i],l[i][ct2-1]);
if (a[i][ct2-1]+l[i][ct2-1]>a[i][ct2]){
a[i][ct2]=a[i][ct2-1]+l[i][ct2-1];
l[i][ct2]=l[i][ct2-1]-v;
p[i][ct2]=i;
s[i][ct2]=s[i][ct2-1];
}
}
}
// debug
for (j=0;j<=h;j++){
break;
cout << j << ": ";
for (i=0;i<n;i++){
cout << a[i][j] << "/" << l[i][j] << "/" << p[i][j] << "/"<<s[i][j] << " ";;
}
cout << endl;
}
//cout << endl;
// find best
int best=-1,ci,cj;
for (i=0;i<n;i++){
if (best<a[i][h])
{
ci=i;
best=a[i][h];
}
}
int ts[maxn],total=best,prev;
memset(ts,0,sizeof(ts));
cj=h;
k=l[ci][cj];
// find time spent in each lake
while (1==1){
ts[ci]=cj-s[ci][cj];
if (ci==0) break;
prev=p[ci][cj];
cj=s[ci][cj]-tt[prev][ci];
ci=prev;
}
// output
for (i=0;i<n-1;i++){
cout << 5*ts[i] << ", ";
}
cout << 5*ts[n-1] ;
cout << endl;
cout << "Number of fish expected: " << best << endl;
}
void main(){
int nn=0;
int f=1;
while (cin>>n){
if(!n) break;
if (f)f=0;else cout << endl;
input();
process();
}
}
[/cpp]
[cpp]
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define maxn 30
#define maxt 200
int a[maxn][maxt],l[maxn][maxt],s[maxn][maxt],p[maxn][maxt];
int t[maxn],f[maxn],d[maxn];
int tt[maxn][maxn];
int n,h;
void input(){
int i;
cin >> h;
for (i=0;i<n;i++)
cin >> f;
for (i=0;i<n;i++)
cin >> d;
for (i=0;i<n-1;i++)
cin >> t;
}
void process(){
int i,j,k;
h*=12;
// time from one lake to another
for (i=0;i<n;i++){
k=0;
for (j=i;j<n-1;j++){
k+=t[j];
tt[j+1]=k;
}
}
k=f[0];
memset(a,0,sizeof(a));
memset(l,0,sizeof(l));
int prevt,ct,v,ct2;
l[0][0]=f[0];
for (i=0;i<n;i++){
// move here from previous lake
for (ct=h;ct>=0;ct--){
for (j=0;j<i;j++){
prevt=ct-tt[j]-1;
if (prevt<0) continue;
// move here from a previous lake
if (a[j][prevt]+f>a[ct]){
a[ct]=a[j][prevt]+f;
l[ct]=max(0,f[i]-d[i]);
p[i][ct]=j;
s[i][ct]=ct-1;
// from current position
for (ct2=ct+1;ct2<=h;ct2++){
v=min(d[i],l[i][ct2-1]);
if (a[i][ct2-1]+l[i][ct2-1]>a[i][ct2]){
a[i][ct2]=a[i][ct2-1]+l[i][ct2-1];
l[i][ct2]=l[i][ct2-1]-v;
p[i][ct2]=j;
s[i][ct2]=ct-1;
}
}
}
}
}
// from current position
if (i==0)
for (ct2=1;ct2<=h;ct2++){
v=min(d[i],l[i][ct2-1]);
if (a[i][ct2-1]+l[i][ct2-1]>a[i][ct2]){
a[i][ct2]=a[i][ct2-1]+l[i][ct2-1];
l[i][ct2]=l[i][ct2-1]-v;
p[i][ct2]=i;
s[i][ct2]=s[i][ct2-1];
}
}
}
// debug
for (j=0;j<=h;j++){
break;
cout << j << ": ";
for (i=0;i<n;i++){
cout << a[i][j] << "/" << l[i][j] << "/" << p[i][j] << "/"<<s[i][j] << " ";;
}
cout << endl;
}
//cout << endl;
// find best
int best=-1,ci,cj;
for (i=0;i<n;i++){
if (best<a[i][h])
{
ci=i;
best=a[i][h];
}
}
int ts[maxn],total=best,prev;
memset(ts,0,sizeof(ts));
cj=h;
k=l[ci][cj];
// find time spent in each lake
while (1==1){
ts[ci]=cj-s[ci][cj];
if (ci==0) break;
prev=p[ci][cj];
cj=s[ci][cj]-tt[prev][ci];
ci=prev;
}
// output
for (i=0;i<n-1;i++){
cout << 5*ts[i] << ", ";
}
cout << 5*ts[n-1] ;
cout << endl;
cout << "Number of fish expected: " << best << endl;
}
void main(){
int nn=0;
int f=1;
while (cin>>n){
if(!n) break;
if (f)f=0;else cout << endl;
input();
process();
}
}
[/cpp]