Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Problema con programa sencillo-intermedio [Problema de las 8 reinas]

Estas en el tema de Problema con programa sencillo-intermedio [Problema de las 8 reinas] en el foro de C/C++ en Foros del Web. Bueno, al ejecutarlo sale impreso el "tablero" y la única O (reina) que aparece sale en la esquina superior izquierda... bueno, les dejo el código ...
  #1 (permalink)  
Antiguo 09/12/2012, 23:39
 
Fecha de Ingreso: abril-2011
Mensajes: 224
Antigüedad: 13 años
Puntos: 8
Exclamación Problema con programa sencillo-intermedio [Problema de las 8 reinas]

Bueno, al ejecutarlo sale impreso el "tablero" y la única O (reina) que aparece sale en la esquina superior izquierda... bueno, les dejo el código fuente, a ver si alguien encuentra algún error.

Código C++:
Ver original
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. int heuristica(int[][8], int&, int&);
  6. bool probar(int, int, int[][8]);
  7. int cuantos(int, int, int[][8]);
  8. void ponerReina(int, int, int[][8]);
  9.  
  10. int main()
  11. {
  12.   int tablero[8][8] = {};
  13.   int fila;
  14.   int columna;
  15.   int a, b;
  16.  
  17.   while(heuristica(tablero, fila, columna) != -1)
  18.   {
  19.     ponerReina(fila, columna, tablero);
  20.   }
  21.  
  22.   for(int x = 0; x < 8; x++)
  23.   {
  24.     for(int y = 0; y < 8; y++)
  25.     {
  26.       if(tablero[x][y] == 2)
  27.     cout << "O";
  28.       else
  29.     cout << "X";
  30.     }
  31.     cout << endl;
  32.   }
  33.  
  34.   cout << endl;
  35.   return 0;
  36. }
  37.  
  38. int heuristica(int tablero[][8], int &fila, int &columna)
  39. {
  40.   int mejor[] = {-1, 0, 0};
  41.   int actual;
  42.  
  43.   for(int x = 0; x < 8; x++)
  44.   {
  45.     for(int y = 0; y < 8; y++)
  46.     {
  47.       if(tablero[x][y] == 0)
  48.       {
  49.     if(probar(x, y, tablero))
  50.     {
  51.       if(cuantos(x, y, tablero) < mejor[0] || mejor[0] == -1)
  52.       {
  53.         mejor[0] = cuantos(x, y, tablero);
  54.         mejor[1] = x;
  55.         mejor[2] = y;
  56.       }
  57.     }
  58.       }
  59.     }
  60.   }
  61.  
  62.   fila = mejor[1];
  63.   columna = mejor[2];
  64.   return mejor[0];
  65. }
  66.  
  67. bool probar(int x, int y, int tablero[][8])
  68. {
  69.   int r = x;
  70.   int s = y;
  71.  
  72.   if(tablero[x][0] != 0 || tablero[x][1] != 0 || tablero[x][2] != 0 || tablero[x][3] != 0 ||
  73.     tablero[x][4] != 0 || tablero[x][5] != 0 || tablero[x][6] != 0 || tablero[x][7] != 0)
  74.       return false;
  75.  
  76.   if(tablero[0][y] != 0 || tablero[1][y] != 0 || tablero[2][y] != 0 || tablero[3][y] != 0 ||
  77.     tablero[4][y] != 0 || tablero[5][y] != 0 || tablero[6][y] != 0 || tablero[7][y] != 0)
  78.       return false;
  79.  
  80.   // Diagonal \ hacia arriba
  81.   r--;
  82.   s--;
  83.  
  84.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  85.   {
  86.     if(tablero[r][s] != 0)
  87.       return false;
  88.     r--;
  89.     s--;
  90.   }
  91.  
  92.   // Diagonal \ hacia abajo
  93.  
  94.   r = x+1;
  95.   s = y+1;
  96.  
  97.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  98.   {
  99.     if(tablero[r][s] != 0)
  100.       return false;
  101.     r++;
  102.     s++;
  103.   }
  104.  
  105.   // Diagonal / hacia arriba
  106.  
  107.   r = x-1;
  108.   s = y+1;
  109.  
  110.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  111.   {
  112.     if(tablero[x][y] != 0)
  113.       return false;
  114.    
  115.     r--;
  116.     s++;
  117.   }
  118.  
  119.   // Diagonal / hacia abajo
  120.  
  121.   r = x+1;
  122.   s = y-1;
  123.  
  124.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  125.   {
  126.     if(tablero[x][y] != 0)
  127.       return false;
  128.    
  129.     r++;
  130.     s--;
  131.   }
  132.  
  133.   return true;
  134. }
  135.  
  136. int cuantos(int x, int y, int tablero[][8])
  137. {
  138.   int r = x;
  139.   int s = y;
  140.   int total = 0;
  141.  
  142.   for(int i = 0; i < 8; i++)
  143.   {
  144.     if(tablero[x][i] == 0)
  145.       total++;
  146.   }
  147.  
  148.   for(int i = 0; i < 8; i++)
  149.   {
  150.     if(tablero[i][y] == 0)
  151.       total++;
  152.   }
  153.  
  154.   // Diagonal \ hacia arriba
  155.   r--;
  156.   s--;
  157.  
  158.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  159.   {
  160.     if(tablero[r][s] == 0)
  161.       total++;
  162.     r--;
  163.     s--;
  164.   }
  165.  
  166.   // Diagonal \ hacia abajo
  167.  
  168.   r = x+1;
  169.   s = y+1;
  170.  
  171.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  172.   {
  173.     if(tablero[r][s] == 0)
  174.       total++;
  175.     r++;
  176.     s++;
  177.   }
  178.  
  179.   // Diagonal / hacia arriba
  180.  
  181.   r = x-1;
  182.   s = y+1;
  183.  
  184.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  185.   {
  186.     if(tablero[x][y] == 0)
  187.       total++;
  188.    
  189.     r--;
  190.     s++;
  191.   }
  192.  
  193.   // Diagonal / hacia abajo
  194.  
  195.   r = x+1;
  196.   s = y-1;
  197.  
  198.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  199.   {
  200.     if(tablero[x][y] == 0)
  201.       total++;
  202.    
  203.     r++;
  204.     s--;
  205.   }
  206.  
  207.   return total-1;
  208. }
  209.  
  210. void ponerReina(int x, int y, int tablero[][8])
  211. {
  212.   int r = x;
  213.   int s = y;
  214.  
  215.   tablero[x][y] = 2;
  216.  
  217.   for(int i = 0; i < 8; i++)
  218.   {
  219.     if(tablero[x][i] == 0)
  220.       tablero[x][i] = 1;
  221.   }
  222.  
  223.   for(int i = 0; i < 8; i++)
  224.   {
  225.     if(tablero[i][y] == 0)
  226.       tablero[i][y] = 1;
  227.   }
  228.  
  229.   // Diagonal \ hacia arriba
  230.   r--;
  231.   s--;
  232.  
  233.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  234.   {
  235.     tablero[r][s] = 1;
  236.     r--;
  237.     s--;
  238.   }
  239.  
  240.   // Diagonal \ hacia abajo
  241.  
  242.   r = x+1;
  243.   s = y+1;
  244.  
  245.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  246.   {
  247.     tablero[r][s] = 1;
  248.     r++;
  249.     s++;
  250.   }
  251.  
  252.   // Diagonal / hacia arriba
  253.  
  254.   r = x-1;
  255.   s = y+1;
  256.  
  257.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  258.   {
  259.     tablero[r][s] = 1;  
  260.     r--;
  261.     s++;
  262.   }
  263.  
  264.   // Diagonal / hacia abajo
  265.  
  266.   r = x+1;
  267.   s = y-1;
  268.  
  269.   while(r >= 0  && r <= 7 && s >= 0 && s <= 7)
  270.   {
  271.     tablero[r][s] = 1;    
  272.     r++;
  273.     s--;
  274.   }
  275. }

Creo que el error está en la función heuristica, pero aún no lo encuentro.

Saludos.

Última edición por reethok; 09/12/2012 a las 23:49

Etiquetas: int, programa
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:50.