Búsqueda de Sonares 3D

sonar3d.cpp 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std ;
  4. void printArray(int *array, int size){
  5. for(int i = 0; i < size; i++){
  6. cout << array[i] << " " ;
  7. }
  8. }
  9. bool is3DSonar1D(int *array, int *hash, int iter_id, int p, int n){
  10. int *hash = NULL ;
  11. int p2 = p*p ;
  12. int dif ;
  13. for (int h1 = 0; h1 < p; h1++){
  14. for(int h2 = 0; h2 < p; h2++){
  15. if(h1 == 0 && h2 == 0)
  16. continue ;
  17. //printArray(hash, p2) ;cout << endl ;
  18. for(int i = 0; i < p; i++){
  19. if(p*i > n-1)
  20. break ;
  21. for(int j = 0; j < p; j++){
  22. if(i == 0 && j == 0)
  23. continue ;
  24. if((i+h1) % p == 0 && (j+h2) % p == 0)
  25. continue ;
  26. if(p*i+j > n-1)
  27. break;
  28. if( p * ((i + h1) % p) + ((j+h2) % p) > n - 1)
  29. continue ;
  30. dif = (array[ p *((i + h1) % p) + ((j+h2) % p)] - array[p*i+j]);
  31. //cout <<i<< j<< h1 << h2 << "dif" <<dif<<endl ;
  32. if(dif < 0)
  33. dif = p2 + dif;
  34. else
  35. dif = dif % p2;
  36. if (hash[dif] ==1){
  37. // printArray(hash, p2) ;cout << endl ;
  38. return false ;
  39. }
  40. else
  41. hash[dif]=1 ;
  42. }
  43. }
  44. }
  45. }
  46. return true ;
  47. }
  48. int main(int argc, char **argv){
  49. int start, end, p, n ;
  50. p = 3 ;
  51. int counter = 0 ;
  52. bool sonar = false ;
  53. start = 3;
  54. end = 3;
  55. n = p*p ;
  56. int *array = new int[n];
  57. int *hash = (int *) calloc(p2, sizeof(int)) ;
  58. for(int i = 0; i < n; i++)
  59. array[i] = -1 ;
  60. array[1] = 0;
  61. array[2] = atoi(argv[1]) ;
  62. while(end >= start){
  63. counter++ ;
  64. sonar = false ;
  65. if(array[end] + 1 > n-1){
  66. array[end] = -1;
  67. end-- ;
  68. continue ;
  69. }
  70. array[end]++;
  71. if(counter % 2000 == 0){
  72. cout << "Check Point: " ; printArray(array, end+1); cout << endl;
  73. counter = 0;
  74. }
  75. if (is3DSonar1D(array, p, end+1)){
  76. //cout <<"Costas";printArray(array, end+1) ; cout <<endl ;
  77. sonar = true ;
  78. if(end+1 == n){
  79. cout <<"Sonar:";printArray(array, end+1);cout << endl;
  80. }
  81. }
  82. if(sonar && end < (n-1)){
  83. end++ ;
  84. }
  85. }
  86. return 0 ;
  87. }