//#include <conio.h> ///////////////////////////////////////////////////////////////////////
//#include <stdio.h> ///////////////////////////////////////////////////////////////////////
#include "cielib.h"

int Entek[501], Entek2[501];
int historia[2000][2];
int hist_poz;
int d,r,k,wynik,kierunek;
int pocz,kon,propon;

double MAC_A[501][501];
double ODW_A[501][501];
double WEK_B[501];
double punkt[501];

int losuj()
{
static int a;
return (a+1)%2;
}

int main() ///<<<<<
{

d=podajD();
r=podajR();
k=podajK();

for (int nr_rown=0; nr_rown<d; nr_rown++)
   {
   int znam=0;
	hist_poz=0; pocz=0; kon=r;
   while (!znam)
      {
      //for (int i=0; i<d; i++) scanf("%ld",&Entek[i]);
      
      
      if (hist_poz==1)
      	{
      	if (losuj())
      	   {//w górę
      	   propon = (kon + historia[hist_poz][0])/2;
            hist_poz++; historia[hist_poz][0]=propon;
      	   }
      	else
      	   {//w dół
      	   propon = (historia[hist_poz][0] + pocz)/2 ;
            hist_poz++; historia[hist_poz][0]=propon;
      	   }
      	}
      else if ((hist_poz==0) || (historia[hist_poz][1]==0))
         {
         propon=(kon-pocz)/2+pocz;
         hist_poz++; historia[hist_poz][0]=propon;
         }
      else
      	{
      	if (losuj())
      	   {//w górę
      	   propon = (kon + historia[hist_poz][0])/2;
            hist_poz++; historia[hist_poz][0]=propon;
      	   }
      	else
      	   {//w dół
      	   propon = (historia[hist_poz][0] + pocz)/2 ;
            hist_poz++; historia[hist_poz][0]=propon;
      	   }
      	}
      //Ustawienie wartosci
		for (int i=0; i<nr_rown; i++) Entek[i]=r-propon;
      for (int i=nr_rown; i<d; i++) Entek[i]=propon;
      
      
//      printf("propon=%ld\n",propon); ////////////////////////////////////////////
//      for (int i=0; i<d; i++) printf("%ld ",Entek[i]); //////////////////////////
//      printf("\n"); /////////////////////////////////////////////////////////////
      
      
      if (Entek[0]>=0)
         {
         wynik=czyCieplo(Entek);
//         if (wynik) printf("BLIZEJ\n");
//         else printf("Dalej\n");
         historia[hist_poz][1]=wynik;
         
         if (hist_poz>1)
            {
            if (historia[hist_poz][0]-historia[hist_poz-1][0]>0)
               {//ROSLO
               if (wynik==1)
                  {//ROSLO i BLIŻEJ
                  pocz= (historia[hist_poz][0] + historia[hist_poz-1][0])/2;
                  }
               else
                  {//ROSLO i dalej
                  kon= (historia[hist_poz][0] + historia[hist_poz-1][0])/2 + 1;
                  }
               
               }
            else if (historia[hist_poz][0]-historia[hist_poz-1][0]<0)
               {//malalo
               if (wynik==1)
                  {//malalo i BLIŻEJ
                  kon= (historia[hist_poz][0] + historia[hist_poz-1][0])/2 + 1;
                  }
               else
                  {//malalo i dalej
                  pocz= (historia[hist_poz][0] + historia[hist_poz-1][0])/2;
                  }
               
               }
            //printf("pocz( %ld ; %ld )kon\n",pocz,kon);
            }
         
         }
/*      else
         {
         printf("Podaj pozycje Krotki:\n");
         for (int i=0; i<d; i++) scanf("%ld",&Entek[i]);
         znalazlem(Entek);
         }
  */    
      
      
      if (kon-pocz<=4)
         {
         //printf("Potrzebne ograniczenie na warunki szczegolne.\n");
         do
            {
            propon = historia[hist_poz][0] + 1;
            if (propon>r) propon=r;
            hist_poz++; historia[hist_poz][0]=propon;
            for (int i=0; i<nr_rown; i++) Entek[i]=r-propon;
            for (int i=nr_rown; i<d; i++) Entek[i]=propon;
      //printf("propon=%ld\n",propon); ////////////////////////////////////////////
      //for (int i=0; i<d; i++) printf("%ld ",Entek[i]); //////////////////////////
      //printf("\n"); /////////////////////////////////////////////////////////////
            wynik=czyCieplo(Entek);
      //if (wynik) printf("BLIZEJ\n");
      //else printf("Dalej\n");
            historia[hist_poz][1]=wynik;
            } while ((wynik) && (propon<r));
         do 
			   {
            propon = historia[hist_poz][0] - 1;
            if (propon<0) propon=0;
            hist_poz++; historia[hist_poz][0]=propon;
            for (int i=0; i<nr_rown; i++) Entek[i]=r-propon;
            for (int i=nr_rown; i<d; i++) Entek[i]=propon;
      //printf("propon=%ld\n",propon); ////////////////////////////////////////////
      //for (int i=0; i<d; i++) printf("%ld ",Entek[i]); //////////////////////////
      //printf("\n"); /////////////////////////////////////////////////////////////
            wynik=czyCieplo(Entek);
      //if (wynik) printf("BLIZEJ\n");
      //else printf("Dalej\n");
            historia[hist_poz][1]=wynik;
            } while ((wynik) && (propon>0));
         do
            {
            propon = historia[hist_poz][0] + 1;
            if (propon>r) propon=r;
            hist_poz++; historia[hist_poz][0]=propon;
            for (int i=0; i<nr_rown; i++) Entek[i]=r-propon;
            for (int i=nr_rown; i<d; i++) Entek[i]=propon;
      //printf("propon=%ld\n",propon); ////////////////////////////////////////////
      //for (int i=0; i<d; i++) printf("%ld ",Entek[i]); //////////////////////////
      //printf("\n"); /////////////////////////////////////////////////////////////
            wynik=czyCieplo(Entek);
      //if (wynik) printf("BLIZEJ\n");
      //else printf("Dalej\n");
            historia[hist_poz][1]=wynik;
            } while ((wynik) && (propon<r));
         
         znam=1;
         //Konkretny punkt:
         if ((historia[hist_poz][1]==0) && (historia[hist_poz-1][1]==0))
            {// Połówka
            for (int i=0; i<nr_rown; i++) {Entek[i]=r-historia[hist_poz][0]; Entek2[i]=r-historia[hist_poz-1][0];}
            for (int i=nr_rown; i<d; i++) {Entek[i]=historia[hist_poz][0]; Entek2[i]=historia[hist_poz-1][0];}
            for (int i=0; i<d; i++) {punkt[i+1]= ( double(Entek[i]) + double(Entek2[i]) )/2.0; }
            }
         else if ((historia[hist_poz][1]==0) && (historia[hist_poz-1][1]==1))
            {// Na linii jest przedostatni
            for (int i=0; i<nr_rown; i++) {Entek[i]=r-historia[hist_poz-1][0];}
            for (int i=nr_rown; i<d; i++) {Entek[i]=historia[hist_poz-1][0];}
            for (int i=0; i<d; i++) {punkt[i+1]= double(Entek[i]); }
            }
         else if ((historia[hist_poz][1]==1) && (historia[hist_poz-1][1]==0))
            {//Na linii jest ostatni
            for (int i=0; i<nr_rown; i++) {Entek[i]=r-historia[hist_poz][0];}
            for (int i=nr_rown; i<d; i++) {Entek[i]=historia[hist_poz][0];}
            for (int i=0; i<d; i++) {punkt[i+1]= double(Entek[i]); }
            }
         else
            {
            //printf("Czegos nie rozumiem...\n");
            //getch();
            }
         //Wpisanie do macierzy:
         for (int i=0; i<nr_rown; i++) MAC_A[nr_rown+1][i+1]=-1.0;
         for (int i=nr_rown; i<d; i++) MAC_A[nr_rown+1][i+1]=1.0;
         WEK_B[nr_rown+1] = 0;
         for (int i=0; i<d; i++) WEK_B[nr_rown+1] += MAC_A[nr_rown+1][i+1]*punkt[i+1];
         }
      
      
      
      //getch(); /////////////////////////////////////////////////////////////////////////////
      } //while (!znam)
      
   }// for nr_rown= 0 : d-1


for (int rr=1; rr<=d; rr++)
   {
   for (int i=1; i<=d; i++)
      {
      if (rr==i) ODW_A[rr][i]=1.0;
      else ODW_A[rr][i]=0.0;
      }
   }


//Dolny trójkąt:
Entek[0]=int(0.5*WEK_B[1]-0.5*WEK_B[2]+0.5);
Entek[1]=int(0.5*WEK_B[1]+0.5*WEK_B[2]+0.5);
znalazlem(Entek);


/*
printf("\n\nMAC_A:\n");
for (int rr=1; rr<=d; rr++) 
   {
   for (int i=1; i<=d; i++) printf("%.3lf  ",MAC_A[rr][i]);
   printf("\n\n");
   }
printf("\nWEK_B:\n");
for (int i=1; i<=d; i++) printf("%.3lf\n",WEK_B[i]);
*/




return 0;
}





