123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include <iostream>
- #include <stdlib.h>
-
- using namespace std ;
-
- void printArray(int *array, int size){
- for(int i = 0; i < size; i++){
- cout << array[i] << " " ;
- }
-
- }
-
- void ZeroArray(int *array, int size){
- for (int i = 0; i < size ; i++)
- array[i] = 0 ;
- }
-
- bool is3DSonar1D(int *array, int *hash, int n1, int n2, int n){
- int p2 = n1*n2 ;
- int dif ;
- int iter = 0;
- ZeroArray(hash, p2) ;
- for (int h1 = 0; h1 < n1; h1++){
- for(int h2 = 0; h2 < n2; h2++){
- if(h1 == 0 && h2 == 0)
- continue ;
- iter++ ;
- //ZeroArray(hash,p2) ;
- //cout <<"after 0 " ;printArray(hash, p2) ; cout <<endl;
- for(int i = 0; i < n1; i++){
- if(n2*i > n-1)
- break ;
- for(int j = 0; j < n2; j++){
- if(i == 0 && j == 0)
- continue ;
- if((i+h1) % n1 == 0 && (j+h2) % n2 == 0)
- continue ;
- if(n2*i+j > n-1)
- break;
- if( n2 * ((i + h1) % n1) + ((j+h2) % n2) > n - 1)
- continue ;
- dif = (array[ n2 *((i + h1) % n1) + ((j+h2) % n2)] - array[n2*i+j]);
- //cout << h1 << " " << h2 << " n " << n <<endl ;
- //cout <<i+h1<< j+h2 << i << j << array[ n2 *((i + h1) % n1) + ((j+h2) % n2)] << array[n2*i+j] << "dif" <<dif<<endl ;
- //cout <<"Pos " << n2 *((i + h1) % n1) + ((j+h2) % n2) << n2*i+j << endl ;
- if(dif < 0)
- dif = p2 + dif;
- else
- dif = dif % p2;
- if (hash[dif] == iter){
- //cout << "Eq " <<hash[dif] << iter <<endl ;
- //printArray(hash, p2) ;cout << endl ;
- return false ;
- }
- else{
- // cout << "FALSE" <<endl ;
- hash[dif]=iter ;
- }
- }
- }
- }
- }
- return true ;
- }
-
- int main(int argc, char **argv){
-
- int start, end, n1, n2, n ;
- int counter = 0 ;
- bool sonar = false ;
- start = 3;
- end = 3;
- n1 = atoi(argv[1]) ;
- n2 = atoi(argv[2]) ;
- n = n1*n2 ;
- int *array = new int[n];
- int *hash = (int *) calloc(n, sizeof(int)) ;
-
- for(int i = 0; i < n; i++)
- array[i] = -1 ;
-
- array[1] = 0;
- array[2] = atoi(argv[3]) ;
- while(end >= start){
- counter++ ;
- sonar = false ;
- if(array[end] + 1 > n-1){
- array[end] = -1;
- end-- ;
- continue ;
- }
- array[end]++;
- if(counter % 10000 == 0){
- cout << "Check Point: " ; printArray(array, end+1); cout << endl;
- counter = 0;
- }
- if (is3DSonar1D(array, hash, n1, n2, end+1)){
- //cout <<"Costas";printArray(array, end+1) ; cout <<endl ;
- sonar = true ;
- if(end+1 == n){
- cout <<"Sonar:";printArray(array, end+1);cout << endl;
- }
- }
- if(sonar && end < (n-1)){
- end++ ;
- }
-
- }
-
- return 0 ;
- }
|