Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/05/2013, 08:45
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 2 meses
Puntos: 1360
Respuesta: Problema en algoritmo recursivo!!!

Código C++:
Ver original
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <string.h>
  6. #include <vector>
  7. #include <queue>
  8.  
  9. using namespace std;
  10. int n , dp [1000][1000], x, y, xx, yy;
  11. bool mark [1000][1000];
  12.  
  13. struct Punto
  14. {
  15.     int x;
  16.     int y;
  17. };
  18.  
  19. Punto inicio, fin , aux;
  20. queue < Punto > lista;
  21. int direcciones[3][2] = { {0,1}, {1,0}, {1,1} };
  22.  
  23. void BFS()
  24. {
  25.    
  26.     if( !lista.empty() )
  27.     {
  28.         aux = lista.front();
  29.         lista.pop();
  30.         x = aux.x;
  31.         y = aux.y;
  32.         for(int i=0 ; i<3 ; i++)
  33.         {
  34.             xx = direcciones[i][0]+x;
  35.             yy = direcciones[i][1]+y;
  36.             if( xx>0 && xx <= n && yy > 0 && yy <= n )
  37.             {
  38.                 dp[xx][yy] += dp[x][y];
  39.                 if(!mark[xx][yy])
  40.                 {
  41.                     mark[xx][yy] = true;
  42.                     aux.x = xx;
  43.                     aux.y = yy;
  44.                     lista.push( aux );
  45.                 }
  46.             }
  47.         }
  48.         BFS();
  49.     }
  50. }
  51.  
  52. int main()
  53. {    
  54.     cin>>n;
  55.     cin>>x>>y;
  56.     inicio.x = x;
  57.     inicio.y = y;
  58.     cin>>x>>y;
  59.     fin.x = x;
  60.     fin.y = y;
  61.  
  62.     lista.push(inicio);
  63.     dp[ inicio.x ][ inicio.y ] = 1;
  64.     BFS();
  65.     cout<< dp[fin.x][fin.y] <<endl;
  66.     return 0;
  67. }