ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1029. Ministry

I got crash(access violation) in test14,please help me!!!
Posted by guojinyu 30 Jul 2011 20:42
I got crash in test14,please help me!!!
this is my program:
#include <iostream>
#include <cstdio>
using namespace std;
int n,m,xia=0;
int fare[110][510];
int dp[110][510],jilu[70000];
char pre[110][510];
void init()
{
  int i,j;
  scanf("%d %d",&n,&m);
  for(i=0;i<=n+1;i++)
    for(j=0;j<=m+1;j++)
    {
      fare[i][j]=2000000000;
      dp[i][j]=2000000000;
    }
  for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
      scanf("%d",&fare[i][j]);
  for(i=1;i<=m;i++)
    dp[1][i]=fare[1][i];
}
void print(int i,int j)
{
  int k,ti,tj;
  while(1)
  {
    ++xia;
    jilu[xia]=j;
    ti=i;
    tj=j;
    if(pre[i][j]=='B')
      ti-=1;
    if(pre[i][j]=='L')
      tj-=1;
    if(pre[i][j]=='R')
      tj+=1;
    if(i==1)
      break;
    i=ti;
    j=tj;
  }
  for(k=xia;k>=1;k--)
    printf("%d\n",jilu[k]);
}
void solve()
{
  int i,j,end,temp=2000000000;
  for(i=2;i<=n;i++)
  {
    for(j=1;j<=m;j++)
    {
      if(dp[i-1][j]<=dp[i][j-1])
        if(dp[i-1][j]+fare[i][j]<dp[i][j])
        {
          dp[i][j]=dp[i-1][j]+fare[i][j];
          pre[i][j]='B';
        }
      if(dp[i-1][j]>dp[i][j-1])
        if(dp[i][j-1]+fare[i][j]<dp[i][j])
        {
          dp[i][j]=dp[i][j-1]+fare[i][j];
          pre[i][j]='L';
        }
      if(dp[i][j]>dp[i][j+1]+fare[i][j])
      {
        dp[i][j]=dp[i][j+1]+fare[i][j];
        pre[i][j]='R';
      }
    }
    for(j=m;j>=1;j--)
    {
      if(dp[i-1][j]<=dp[i][j-1])
        if(dp[i-1][j]+fare[i][j]<dp[i][j])
        {
          dp[i][j]=dp[i-1][j]+fare[i][j];
          pre[i][j]='B';
        }
      if(dp[i-1][j]>dp[i][j-1])
        if(dp[i][j-1]+fare[i][j]<dp[i][j])
        {
          dp[i][j]=dp[i][j-1]+fare[i][j];
          pre[i][j]='L';
        }
      if(dp[i][j]>dp[i][j+1]+fare[i][j])
      {
        dp[i][j]=dp[i][j+1]+fare[i][j];
        pre[i][j]='R';
      }
    }
  }
  for(i=1;i<=m;i++)
    if(dp[n][i]<temp)
    {
      temp=dp[n][i];
      end=i;
    }
  print(n,end);
}
int main()
{
  init();
  solve();
  return(0);
}