Cod sursa(job #591704)

Utilizator rudarelLup Ionut rudarel Data 25 mai 2011 09:49:27
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.41 kb
/*
#include <ebunamata.h> // for the E buna mata messege
#include <savezidenuopui.h> // for beting u up
#include <lascavezitu.h> // asta tre sa vezi ( functie speciala OrbInGura() )
#include <sorry.h> // for sorry function
#include <kickass.h>

// algoritmul 

int main()
{
    if ( !lasCaTi-oVeziTu ) OrbInGura(); // atunci se poate deduce
    
    while ( !RuptInGura(Radu) ) 
    {
        EbunaMata(); // like u said
        SaVeziDeNuOpui(OrbInGura(Radu));
    }

    // the real smart part
    while ( !SorryDeCeMi-aScris(Radu) )  KickAss();

    if ( StillNotSorry(Radu) ) cout << "you will be tomorrow" << endl;

    return 0;
}

*/
        
#include <stdio.h>       

#define INPUT_FILE  "robot.in"     
#define OUTPUT_FILE "robot.out"    

#define DIM_M 100            
#define DIM_N 100           

#define IMPOSSIBLE_MESSAGE "IMPOSIBIL"  

typedef struct pozitie        
{
 unsigned int l, c;    
} poz;
                       
FILE* f = NULL;
unsigned int M = 0, N = 0, K = 0, R = 0;
 

 
 
int p[DIM_M][DIM_N] = {0}; 

                        
unsigned int i = 0, j = 0;   
int r[DIM_M][DIM_N] = {0}, x = 0; 


                        
unsigned char posibil = 0; 
poz d[DIM_M] = {0};  
#define d (d - 1)                 
 
unsigned int rf = 0;  

void read_data(); 
void solve();      
void write_sol();  

int main()
{
 read_data();  
 solve();      
 write_sol();  
 return 0;
}
                 
void read_data()
{
 f=fopen(INPUT_FILE, "rt");
 fscanf(f, "%u %u\n%u\n%u\n", &M, &N, &K, &R);
 for(i = 0; i < M; i++)
   for(j = 0; j < N; j++)
     fscanf(f, "%d", &p[i][j]);
 fclose(f);
}
                 
void solve()
{
 for(i = 0; i < N; i++)
 {
  r[0][i] = R + p[0][i] - K; 
 }                           
 for(i = 1; i < M; i++)   
 {
  posibil = 0;            
                          
  for(j = 0; j < N; j++) 
  {                       
                          
    x = 0;         
    if(r[i - 1][j] >= K)     
    {
      r[i][j] = r[i - 1][j];
      x = 1;
    }
    
    if(j >= 1 && r[i - 1][j - 1] >= K && r[i][j] < r[i - 1][j - 1])
    {
      r[i][j] = r[i - 1][j - 1];
      x = 1;
    }
   
    if(j + 1 < N && r[i - 1][j + 1] >= K && r[i][j] < r[i - 1][j + 1])
    {
      r[i][j] = r[i - 1][j + 1];
      x = 1;
    }

    if(x)   
    {             
     r[i][j] = r[i][j] + p[i][j] - K;
     posibil = 1;
    }
  }
  if(!posibil) return;  
 }
 for(i = 0; i < N; i++)    
   if(r[M - 1][i] >= K)   
   {                      
    rf = r[M - 1][i] - K;  
    d[1].l = M;            
    d[1].c = i + 1;        
    break;
   }
 if(i >= N)   
 {            
  posibil = 0;
  return;
 }
 for(i = 2; i <= M; i++)  
 {     
    
   x = r[d[i - 1].l - 1][d[i - 1].c - 1] + K - p[d[i - 1].l - 1][d[i - 1].c - 1];
   if(x == r[d[i - 1].l - 2][d[i - 1].c - 1])
   {
    d[i].l = d[i - 1].l - 1;
    d[i].c = d[i - 1].c;
   }
   else if(d[i - 1].c < N && x == r[d[i - 1].l - 2][d[i - 1].c])
   {
    d[i].l = d[i - 1].l - 1;
    d[i].c = d[i - 1].c + 1;
   }
   else
   {
    d[i].l = d[i - 1].l - 1;
    d[i].c = d[i - 1].c - 1;
   }
 }
}
                 
void write_sol()
{
 f = fopen(OUTPUT_FILE, "wt");  
 if(!posibil) fprintf(f, IMPOSSIBLE_MESSAGE);  
 else    
 {   
    
    
  for(i = M; i; i--) fprintf(f, "(%u, %u)\n", d[i].l, d[i].c);
  fprintf(f, "%u", rf);
 }
 fclose(f);
}