Búsqueda de Sonares 3D

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. void ZeroArray(int *array, int size){
  10. for (int i = 0; i < size ; i++)
  11. array[i] = 0 ;
  12. }
  13. bool is3DSonar1D(int *array, int *hash, int n1, int n2, int n){
  14. int p2 = n1*n2 ;
  15. int dif ;
  16. int iter = 0;
  17. ZeroArray(hash, p2) ;
  18. for (int h1 = 0; h1 < n1; h1++){
  19. for(int h2 = 0; h2 < n2; h2++){
  20. if(h1 == 0 && h2 == 0)
  21. continue ;
  22. iter++ ;
  23. //ZeroArray(hash,p2) ;
  24. //cout <<"after 0 " ;printArray(hash, p2) ; cout <<endl;
  25. for(int i = 0; i < n1; i++){
  26. if(n2*i > n-1)
  27. break ;
  28. for(int j = 0; j < n2; j++){
  29. if(i == 0 && j == 0)
  30. continue ;
  31. if((i+h1) % n1 == 0 && (j+h2) % n2 == 0)
  32. continue ;
  33. if(n2*i+j > n-1)
  34. break;
  35. if( n2 * ((i + h1) % n1) + ((j+h2) % n2) > n - 1)
  36. continue ;
  37. dif = (array[ n2 *((i + h1) % n1) + ((j+h2) % n2)] - array[n2*i+j]);
  38. //cout << h1 << " " << h2 << " n " << n <<endl ;
  39. //cout <<i+h1<< j+h2 << i << j << array[ n2 *((i + h1) % n1) + ((j+h2) % n2)] << array[n2*i+j] << "dif" <<dif<<endl ;
  40. //cout <<"Pos " << n2 *((i + h1) % n1) + ((j+h2) % n2) << n2*i+j << endl ;
  41. if(dif < 0)
  42. dif = p2 + dif;
  43. else
  44. dif = dif % p2;
  45. if (hash[dif] == iter){
  46. //cout << "Eq " <<hash[dif] << iter <<endl ;
  47. //printArray(hash, p2) ;cout << endl ;
  48. return false ;
  49. }
  50. else{
  51. // cout << "FALSE" <<endl ;
  52. hash[dif]=iter ;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. return true ;
  59. }
  60. int main(int argc, char **argv){
  61. int start, end, n1, n2, n ;
  62. int counter = 0 ;
  63. bool sonar = false ;
  64. start = 3;
  65. end = 3;
  66. n1 = atoi(argv[1]) ;
  67. n2 = atoi(argv[2]) ;
  68. n = n1*n2 ;
  69. int *array = new int[n];
  70. int *hash = (int *) calloc(n, sizeof(int)) ;
  71. for(int i = 0; i < n; i++)
  72. array[i] = -1 ;
  73. array[1] = 0;
  74. array[2] = atoi(argv[3]) ;
  75. while(end >= start){
  76. counter++ ;
  77. sonar = false ;
  78. if(array[end] + 1 > n-1){
  79. array[end] = -1;
  80. end-- ;
  81. continue ;
  82. }
  83. array[end]++;
  84. if(counter % 10000 == 0){
  85. cout << "Check Point: " ; printArray(array, end+1); cout << endl;
  86. counter = 0;
  87. }
  88. if (is3DSonar1D(array, hash, n1, n2, end+1)){
  89. //cout <<"Costas";printArray(array, end+1) ; cout <<endl ;
  90. sonar = true ;
  91. if(end+1 == n){
  92. cout <<"Sonar:";printArray(array, end+1);cout << endl;
  93. }
  94. }
  95. if(sonar && end < (n-1)){
  96. end++ ;
  97. }
  98. }
  99. return 0 ;
  100. }