SoloCodigo

Programación General => C/C++ => Mensaje iniciado por: danime en Lunes 11 de Diciembre de 2006, 22:52

Título: Problema Al Ejecutar
Publicado por: danime en Lunes 11 de Diciembre de 2006, 22:52
hola, ya havia preguntado hacerca del mismo programa
un caballo debe recorrer un tablero de dimension n comenzando de la fila f y columna c
...

ya arregle todos los errores que tenia

pero al ejecutarlo falla

alguien me podria decir cual es el error??
porfavorrr

necesito entregarlo hoy antes de las 24:00

el programa tiene esta libreria que hay que incluirla en la misma carpeta en donde esta el programa

/*  These are for time measures  */

/*
*  Adjust this to the clock freq. of the machine where time intervals
*  are computed.
*/
#define CLOCK 350

typedef struct {
        unsigned long hi;
        unsigned long lo;
} time_586;

#define ttime(x) \
__asm__ __volatile__("rdtsc" : "=d" (x.hi), "=a" (x.lo))

static inline double time_diff(time_586 b, time_586 a) {
        double db,da,res;
        db = (double)b.hi*(double)(1<<16)*(double)(1<<16)+(double)b.lo;
        da = (double)a.hi*(double)(1<<16)*(double)(1<<16)+(double)a.lo;
        if (db < da)
res =
((double)(1<<16)*(double)(1<<16)*(double)(1<<16)*(double)(1<<16)+db-da)
/(double)CLOCK;
   else
res = (db-da)/(double)CLOCK;
   return res;
}



           ***************************************************


AKI ESTA EL PROGRAMA!!!!!




#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "Time.h"

typedef struct {

int fila;/*fila en donde esta ubicado actualmente*/
int columna;/*columna donde esta actualmente*/
int ganador;/*opcion del movimiento ganador*/
int cuantos;/*cuantas ociones quedaron*/
int opciones[7];/*movimientos posibles que quedaron*/
}  nodo;


typedef struct {
int top, cont;
nodo *pila; }stack;


typedef struct{
int cuantos;
int mpi;/*numero elegido al azar*/
int mpj;/*opcion correspondiente al numero anterior*/
int opciones[8];}movpos;/*movimientos posibles*/


void init_stack( stack *a, int n)
{
int i,j;
a->top=-1;
a->pila=calloc(n*n,sizeof(nodo));/*inicia el stack con memoria n*n */
 
for(i=0; i<(n*n); i++)
 { for(j=0; j<8; j++)
  a->pila.opciones[j]= 0;}
}



 void opc (int f, int c, movpos *pos, int **m )
{
int opi,opj=0;
 int mov[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};/*formula para calcular movimientos*/
   for(opi=0; opi<8; opi++)
   {
     if(  m[f+1+mov[opi][0]][c+1+mov[opi][1]]==0)
             {
           pos->opciones[opj]=opi+1;
           opj++;
            pos->cuantos++;}
    }
}


  void push(stack*s,movpos *q,int **m)
{
 int pk;
 int mov[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
  s->top=s->top+1;
  s->pila[s->top].fila =s->pila[s->top-1].fila +mov[q->mpj-1][0];
  s->pila[s->top].columna=s->pila[s->top-1].columna +mov[q->mpj-1][1];
 
  s->pila[s->top].ganador=q->mpj;
  s->pila[s->top].cuantos=q->cuantos-1;

    for(pk=0;pk<q->cuantos;pk++)
   {
    int l=0;

       if(pk==q->mpi)
           pk++;
       else
        { s->pila[s->top].opciones[l]=q->opciones[pk];
         l++;
         } }
          s->cont++;
      m[s->pila[s->top].fila+1][s->pila[s->top].columna+1]=s->cont;   }



  void pop(stack*s,movpos*q, int ** B)
{
          movpos *p;
    int popk;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
   b[s->pila[s->top].fila+1][s->pila[s->top].columna+1]=0;
   s->top=s->top-1;
                                                 
   opc(s->pila[s->top].fila,s->pila[s->top].columna, q,B);

   p->cuantos=q->cuantos-1;

   p->mpi=p->mpj=0;

       for( popk=0;popk< p->cuantos;popk++)
      {  int l=0;

          if(popk==s->pila[s->top+1].ganador)
         popk++;
      else
      { p->opciones[l]=q->opciones[popk];
         l++;
         }
      }
   q =p;


     }



int main (int dim, int row, int column, int vs)
{
int i, j,k;

double Etime, Esec;
time_586 inicio, fin;


if(dim>4)
{if((row<=dim  &&  row<0 ) && (column<=dim && column>0))
  { if((vs=='V')||(vs=='S'))
   { stack *horse;
      int ** matriz;
      movpos *q;

init_stack(horse,dim);
 
   matriz= calloc(dim+4, sizeof(int *));
   for(i=0; i<dim+4; i++)
      matriz=calloc(dim+4, sizeof(int) );

    for(i=0; i<dim+4;i++)
      { matriz[0]=matriz[1]=matriz[0]=matriz[1]=matriz[dim+2]=1;
       matriz[dim+3]=matriz[dim+3]=matriz[dim+2]=1;
       }
  q->cuantos=q->mpi=q->mpj=0;
  for(i=0; i<7; i++)
      q->opciones=0;

 horse->top=horse->top+1;
 horse->pila[horse->top].ganador=0;
 horse->pila[horse->top].cuantos=0;
 horse->pila[horse->top].fila=row;
 horse->pila[horse->top].columna=column;
 horse->cont++;

  matriz[row+1][column+1]=1;
do{
    ttime(inicio);
    q->cuantos=q->mpi=q->mpj=0;
       for(i=0; i<7; i++)
              q->opciones=0;

 opc(horse->pila[horse->top].fila, horse->pila[horse->top].columna, q, matriz);

           if(q->cuantos==0)
      {
           while(q->cuantos==0)
                     pop(horse,q,matriz);
         
         q->mpi=rand()%q->cuantos;
         q->mpj=q->opciones[q->mpi];
                push(horse, q, matriz);
            
      }
           else
          {
               if(q->cuantos==1)
                    q->opciones[0]=q->mpj;
               if(q->cuantos>1)
            {
             srand(time(NULL));
             q->mpi=rand()%q->cuantos;
             q->mpj=q->opciones[q->mpi];
            }

        push(horse, q, matriz);
          }



    } while(horse->cont==dim*dim);


             ttime(fin);
         Etime = time_diff(fin,inicio) ;
         Esec = Etime / 1000000;

             if(vs=='S')
          printf("%f",Esec);
            if(vs=='V')
      {
           stack *b;
         init_stack(b,dim);
           j=0;     
          for(i=(dim*dim)-1;i>-1;i--)
                { horse->pila.fila=b->pila[j].fila;
             horse->pila.columna=b->pila[j].columna;
              horse->pila.ganador=b->pila[j].ganador;
             horse->pila.cuantos=b->pila[j].cuantos;
                    for(k=0;k<8;k++)
                     horse->pila.opciones[k]=b->pila[j].opciones[k];
           j++;   }

           printf("%f", Esec);                 
           for( i=(dim*dim)-1;i>-1;i--)
          printf("%d %d %d\n",b->pila.ganador,b->pila.fila,b->pila.columna);

         }   }
}return 1;}
        printf("Error"); }












ojalá me puedan ayudar

 :hola:  :hola:
Título: Re: Problema Al Ejecutar
Publicado por: Eternal Idol en Lunes 11 de Diciembre de 2006, 23:24
Depuralo.
Título: Re: Problema Al Ejecutar
Publicado por: danime en Lunes 11 de Diciembre de 2006, 23:28
le pongo depurar y se abre la pantalla y se cierra altiro...

== no c para que sirve hacer eso
pero lo hice
Título: Re: Problema Al Ejecutar
Publicado por: Eternal Idol en Lunes 11 de Diciembre de 2006, 23:43
No lo hiciste y sirve para ejecutar tu programa linea por linea, e incluso instruccion por instruccion de ensamblador, para saber entre otras cosas donde se te esta colgando.
Título: Re: Problema Al Ejecutar
Publicado por: Mollense en Martes 12 de Diciembre de 2006, 05:25
Citar
Depuralo
Citar
no c para que sirve hacer eso
pero lo hice
 :lol:

Bueno, yo lo compilé violentamente sin ponerme a pensar mucho. no se que puedo haber modificado. Controlalo vos. Solamente te digo que borre unas definiciones y agregué algunos cast's (¿Se dice asi muchachos?). Nada mas.

Código: Text
  1. #define CLOCK 350
  2.  
  3. typedef struct {
  4.   unsigned long hi;
  5.   unsigned long lo;
  6. } time_586;
  7.  
  8. #define ttime(x)&#092;
  9. // Violentamente borre estas definiciones
  10.  
  11. static inline double time_diff(time_586 b, time_586 a) {
  12.   double db,da,res;
  13.   db = (double)b.hi*(double)(1&#60;&#60;16)*(double)(1&#60;&#60;16)+(double)b.lo;
  14.   da = (double)a.hi*(double)(1&#60;&#60;16)*(double)(1&#60;&#60;16)+(double)a.lo;
  15.   if (db &#60; da)
  16.     res =((double)(1&#60;&#60;16)*(double)(1&#60;&#60;16)*(double)(1&#60;&#60;16)*(double)(1&#60;&#60;16)+db-da)/(double)CLOCK;
  17.   else
  18.     res = (db-da)/(double)CLOCK;
  19.   return res;
  20. }
  21.  
  22.  
  23.  
  24. //***************************************************
  25.  
  26.  
  27.  
  28.  
  29. #include &#60;stdlib.h&#62;
  30. #include &#60;stdio.h&#62;
  31. #include &#60;string.h&#62;
  32. #include &#60;time.h&#62;
  33. #include &#34;Time.h&#34;
  34.  
  35. typedef struct {
  36.   int fila;/*fila en donde esta ubicado actualmente*/
  37.   int columna;/*columna donde esta actualmente*/
  38.   int ganador;/*opcion del movimiento ganador*/
  39.   int cuantos;/*cuantas ociones quedaron*/
  40.   int opciones[7];/*movimientos posibles que quedaron*/
  41. } nodo;
  42.  
  43. typedef struct {
  44.   int top, cont;
  45.   nodo *pila;
  46. }stack;
  47.  
  48.  
  49. typedef struct{
  50.   int cuantos;
  51.   int mpi;/*numero elegido al azar*/
  52.   int mpj;/*opcion correspondiente al numero anterior*/
  53.   int opciones[8];
  54. }movpos;/*movimientos posibles*/
  55.  
  56. void init_stack( stack *a, int n){
  57.   int i,j;
  58.   a-&#62;top=-1;
  59.   a-&#62;pila=(nodo *)calloc(n*n,sizeof(nodo));/*inicia el stack con memoria n*n */
  60.  
  61.   for(i=0; i&#60;(n*n); i++){
  62.     for(j=0; j&#60;8; j++)
  63.       a-&#62;pila[i].opciones[j]= 0;
  64.   }
  65. }
  66.  
  67. void opc (int f, int c, movpos *pos, int **m ){
  68.   int opi,opj=0;
  69.   int mov[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};/*formula para calcular movimientos*/
  70.   for(opi=0; opi&#60;8; opi++){
  71.     if( m[f+1+mov[opi][0]][c+1+mov[opi][1]]==0){
  72.       pos-&#62;opciones[opj]=opi+1;
  73.       opj++;
  74.       pos-&#62;cuantos++;
  75.     }
  76.   }
  77. }
  78.  
  79. void push(stack*s,movpos *q,int **m){
  80.   int pk;
  81.   int mov[8][2]={
  82.     {-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}
  83.   };
  84.   s-&#62;top=s-&#62;top+1;
  85.   s-&#62;pila[s-&#62;top].fila =s-&#62;pila[s-&#62;top-1].fila +mov[q-&#62;mpj-1][0];
  86.   s-&#62;pila[s-&#62;top].columna=s-&#62;pila[s-&#62;top-1].columna +mov[q-&#62;mpj-1][1];
  87.  
  88.   s-&#62;pila[s-&#62;top].ganador=q-&#62;mpj;
  89.   s-&#62;pila[s-&#62;top].cuantos=q-&#62;cuantos-1;
  90.  
  91.   for(pk=0;pk&#60;q-&#62;cuantos;pk++){
  92.     int l=0;
  93.  
  94.     if(pk==q-&#62;mpi)pk++;
  95.     else{
  96.       s-&#62;pila[s-&#62;top].opciones[l]=q-&#62;opciones[pk];
  97.       l++;
  98.     }
  99.   }
  100.   s-&#62;cont++;
  101.   m[s-&#62;pila[s-&#62;top].fila+1][s-&#62;pila[s-&#62;top].columna+1]=s-&#62;cont;
  102. }
  103.  
  104. void pop(stack*s,movpos*q, int ** B){
  105.   movpos *p;
  106.   int popk;
  107.  
  108.   B[s-&#62;pila[s-&#62;top].fila+1][s-&#62;pila[s-&#62;top].columna+1]=0;
  109.   s-&#62;top=s-&#62;top-1;
  110.   opc(s-&#62;pila[s-&#62;top].fila,s-&#62;pila[s-&#62;top].columna, q,B);
  111.   p-&#62;cuantos=q-&#62;cuantos-1;
  112.   p-&#62;mpi=p-&#62;mpj=0;
  113.   for( popk=0;popk&#60; p-&#62;cuantos;popk++){
  114.     int l=0;
  115.  
  116.     if(popk==s-&#62;pila[s-&#62;top+1].ganador)popk++;
  117.     else{
  118.       p-&#62;opciones[l]=q-&#62;opciones[popk];
  119.       l++;
  120.     }
  121.   }
  122.   q=p;
  123. }
  124.  
  125. int main (int dim, int row, int column, int vs){
  126.   int i, j,k;
  127.  
  128.   double Etime, Esec;
  129.   time_586 inicio, fin;
  130.  
  131.   if(dim&#62;4){
  132.     if((row&#60;=dim && row&#60;0 ) && (column&#60;=dim && column&#62;0)){
  133.       if((vs=='V')||(vs=='S')){
  134.         stack *horse;
  135.         int ** matriz;
  136.         movpos *q;
  137.  
  138.         init_stack(horse,dim);
  139.  
  140.         matriz=(int **) calloc(dim+4, sizeof(int *));
  141.         for(i=0; i&#60;dim+4; i++)matriz[i]=(int *)calloc(dim+4, sizeof(int) );
  142.  
  143.         for(i=0; i&#60;dim+4;i++){
  144.           matriz[i][0]=matriz[i][1]=matriz[0][i]=matriz[1][i]=matriz[i][dim+2]=1;
  145.  
  146.           matriz[i][dim+3]=matriz[dim+3][i]=matriz[dim+2][i]=1;
  147.         }
  148.         q-&#62;cuantos=q-&#62;mpi=q-&#62;mpj=0;
  149.         for(i=0; i&#60;7; i++)q-&#62;opciones[i]=0;
  150.  
  151.         horse-&#62;top=horse-&#62;top+1;
  152.         horse-&#62;pila[horse-&#62;top].ganador=0;
  153.         horse-&#62;pila[horse-&#62;top].cuantos=0;
  154.         horse-&#62;pila[horse-&#62;top].fila=row;
  155.         horse-&#62;pila[horse-&#62;top].columna=column;
  156.         horse-&#62;cont++;
  157.  
  158.         matriz[row+1][column+1]=1;
  159.         do{
  160.           ttime(inicio);
  161.           q-&#62;cuantos=q-&#62;mpi=q-&#62;mpj=0;
  162.           for(i=0; i&#60;7; i++)q-&#62;opciones[i]=0;
  163.  
  164.           opc(horse-&#62;pila[horse-&#62;top].fila, horse-&#62;pila[horse-&#62;top].columna, q, matriz);
  165.  
  166.           if(q-&#62;cuantos==0){
  167.             while(q-&#62;cuantos==0)
  168.               pop(horse,q,matriz);
  169.  
  170.             q-&#62;mpi=rand()%q-&#62;cuantos;
  171.             q-&#62;mpj=q-&#62;opciones[q-&#62;mpi];
  172.             push(horse, q, matriz);
  173.  
  174.           }
  175.           else{
  176.             if(q-&#62;cuantos==1)q-&#62;opciones[0]=q-&#62;mpj;
  177.             if(q-&#62;cuantos&#62;1){
  178.               srand(time(NULL));
  179.               q-&#62;mpi=rand()%q-&#62;cuantos;
  180.               q-&#62;mpj=q-&#62;opciones[q-&#62;mpi];
  181.             }
  182.  
  183.             push(horse, q, matriz);
  184.           }
  185.         }while(horse-&#62;cont==dim*dim);
  186.  
  187.         ttime(fin);
  188.         Etime = time_diff(fin,inicio)&#59;
  189.         Esec = Etime / 1000000;
  190.  
  191.         if(vs=='S')printf(&#34;%f&#34;,Esec);
  192.         if(vs=='V'){
  193.           stack *b;
  194.           init_stack(b,dim);
  195.           j=0;
  196.           for(i=(dim*dim)-1;i&#62;-1;i--){
  197.             horse-&#62;pila[i].fila=b-&#62;pila[j].fila;
  198.             horse-&#62;pila[i].columna=b-&#62;pila[j].columna;
  199.             horse-&#62;pila[i].ganador=b-&#62;pila[j].ganador;
  200.             horse-&#62;pila[i].cuantos=b-&#62;pila[j].cuantos;
  201.             for(k=0;k&#60;8;k++)horse-&#62;pila[i].opciones[k]=b-&#62;pila[j].opciones[k];
  202.             j++;
  203.           }
  204.  
  205.           printf(&#34;%f&#34;, Esec);
  206.           for( i=(dim*dim)-1;i&#62;-1;i--)printf(&#34;%d %d %d&#092;n&#34;,b-&#62;pila[i].ganador,b-&#62;pila[i].fila,b-&#62;pila[i].columna);
  207.         }
  208.       }
  209.     }
  210.     return 1;
  211.   }
  212.   printf(&#34;Error&#34;);
  213.   getchar();
  214. }
  215.