Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------------------------------------------------------------
- // Meditor Digital Strategies since 1989.
- // www.meditor.hu * meditor_at_meditor_dot_hu
- // This file : queenNsa.c
- // Created by: Végvári Lajos
- // Date : 2017-sep-07 + 09:24:57
- //----------------------------------------------------------------------------------------------
- // >> Sol omnibus lucet
- //----------------------------------------------------------------------------------------------
- #define NUMBER_OF_QUEENS 8
- //----------------------------------------------------------------------------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- //----------------------------------------------------------------------------------------------
- int PermuArray[NUMBER_OF_QUEENS];
- unsigned long long int PermuCount;
- unsigned long long int DancingQueen;
- //----------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------
- unsigned int IsItDancingQueen(void)
- {unsigned int ok;
- int i,j;
- int dif_ndx,dif_permu;
- ok=0;
- i=0;while(i<NUMBER_OF_QUEENS)
- {j=i+1;while(j<NUMBER_OF_QUEENS)
- {
- dif_ndx = i-j;
- dif_permu = PermuArray[i]-PermuArray[j];
- if(abs(dif_ndx)==abs(dif_permu))goto iidq_ext;
- j++;
- }
- i++;
- }ok++;
- iidq_ext:;
- return(ok);
- }
- //----------------------------------------------------------------------------------------------
- void PermuCheck(void)
- {unsigned int i;
- if(IsItDancingQueen())
- {
- i=0;while(i<NUMBER_OF_QUEENS){printf("%3d",PermuArray[i]);i++;}printf("\n");
- DancingQueen++;
- }
- PermuCount++;
- }
- //----------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------
- void RS_permutation(unsigned int first_ndx)
- {unsigned int i;
- unsigned int tmpui;
- if(first_ndx==NUMBER_OF_QUEENS)PermuCheck();
- else
- {i=first_ndx;while(i<NUMBER_OF_QUEENS)
- {
- // swap
- tmpui=PermuArray[first_ndx];PermuArray[first_ndx]=PermuArray[i];PermuArray[i]=tmpui;
- RS_permutation(first_ndx+1);
- //swap
- tmpui=PermuArray[first_ndx];PermuArray[first_ndx]=PermuArray[i];PermuArray[i]=tmpui;
- i++;
- }
- }
- }
- //----------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------
- void queen_exit(void)
- {
- exit(0);
- }
- //----------------------------------------------------------------------------------------------
- void IntBreak(void)
- {char msg[]="! ^C detected: user interrupt.\n";
- printf(msg);
- queen_exit();
- }
- //----------------------------------------------------------------------------------------------
- void TermBreak(void)
- {char msg[]=" - TERM signal detected.\n";
- printf(msg);
- queen_exit();
- }
- //----------------------------------------------------------------------------------------------
- void NormalExit(void)
- {char msg[]=" - Normal shutdown.\n";
- printf(msg);
- queen_exit();
- }
- //----------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------
- int main (int argc,char *argv[])
- {unsigned int i;
- signal(SIGINT,(void*)&IntBreak); // set signals
- signal(SIGTERM,(void*)&TermBreak);
- PermuCount=DancingQueen=0;
- i=0;while(i<NUMBER_OF_QUEENS){PermuArray[i]=i;i++;}
- RS_permutation(0);
- NormalExit();
- return(0);
- }
- //----------------------------------------------------------------------------------------------
- // End of file.
- //----------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement