Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int x[4], N = 4;
- int rel[4][4];
- int acceptable(int k, int size){
- int j;
- for(int i = 0; i<size; i++)
- for ( j = 0; j < size; j++)
- if (x[j] == k) //nu exista
- return 0;
- if (rel[k][x[j]] != 1) //prieten cu ultimul din lista
- return 0;
- return 1;// primul si ultimul om sunt vecini
- }
- int solution(int k, int i){
- return (i == N - 1 && rel[N - 1][0] == rel[0][N - 1] == 1); //4 elemente
- }
- void printsolution(){
- for (int i = 0; i < N; i++)
- printf("%d ", x[i]);
- }
- void back(int i){
- for (int k = 1; k <= N; k++)
- if (acceptable(k, N)){
- x[i] = k;
- if (solution(k, i)){
- printsolution();
- printf("\n");
- }
- else
- back(i + 1);
- x[i] = -1;
- }
- }
- int main(void){
- rel[0][1] = 1;
- rel[1][0] = 1;
- rel[1][2] = 1;
- rel[2][1] = 1;
- rel[2][3] = 1;
- rel[3][2] = 1;
- rel[3][4] = 1;
- rel[4][3] = 1;
- rel[4][0] = 1;
- rel[0][4] = 1;
- back(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement