Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int stan(int *A, int *B, int *C, int atop , int btop ,int ctop);
- int zmiana(int *A, int*B, int *C, int atop, int btop, int ctop, int n, int max);
- int main()
- {
- int kroki=0;
- int n,i,*A,*B,*C,atop,btop,ctop;
- int max=1;
- while(n!=0)
- {
- max=1;
- printf("\nPodaj liczbe dyskow (wpisz 0 aby zakonczyc program): ");
- scanf("%d",&n);
- for(i=0; i<n; i++) max*=2;
- max=max-1;
- printf("\nMaksymalna liczba ruchow: %d",max);
- A= (int *)malloc(sizeof(int)*(n+1));
- B= (int *)malloc(sizeof(int)*(n+1));
- C= (int *)malloc(sizeof(int)*(n+1));
- A[0]=B[0]=C[0]=0;
- atop=btop=ctop=0;
- for(i=n; i>0; i--)
- {
- atop++;
- A[0]++;
- A[atop]=i;
- B[atop]=0;
- C[atop]=0;
- }
- stan(A,B,C,atop,btop,ctop);
- zmiana(A,B,C,atop,btop,ctop,n,max);
- printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- }
- printf("~~~~~~Filip Szarejko~~~~~~~~\n");
- printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- system("pause");
- return 0;
- }
- stan(int *A, int *B, int *C, int atop , int btop ,int ctop)
- {
- int i;
- if(A[0]==0) printf("\nPusty A");
- else
- {
- printf("\nKijek A :\n");
- for (i=atop; i>=1; i--) printf(" %d\n",A[i]);
- }
- if(B[0]==0) printf("\nPusty B");
- else
- {
- printf("\nKijek B: \n");
- for (i=btop; i>=1; i--) printf(" %d\n",B[i]);
- }
- if(C[0]==0) printf("\nPusty C\n");
- else
- {
- printf("\nKijek C: \n");
- for (i=ctop; i>=1; i--) printf(" %d\n",C[i]);
- }
- }
- int zmiana(int *A, int*B, int *C, int atop, int btop, int ctop, int n, int max)
- {
- int kroki=0,ruch=0;
- do
- {
- ruch+=1;
- kroki=(kroki%3)+1;
- switch(kroki)
- {
- case 1://ruch kijkow A i C
- printf("\n%d.) Ruch",ruch);
- if(ruch%2 ==1)//Ruch dysku 1
- {
- if(A[atop]==1)
- {
- ctop++;
- C[ctop]=A[atop];
- A[atop]=0;
- atop--;
- C[0]++;
- A[0]--;
- printf("\ndysk %d: A->C",C[ctop]);
- } //i jest na A
- else if(C[ctop]==1)//1 jest na b
- {
- atop++;
- A[atop]=C[ctop];
- C[ctop]=0;
- ctop--;
- A[0]++;
- C[0]--;
- printf("\ndysk %d: C->A",A[atop]);
- }
- }//ruch dysku 1
- else if(ruch %2 ==0)
- {
- if(ctop !=0 && A[atop]>C[ctop])
- { //C[0]--;
- atop++;
- A[atop]=C[ctop];
- C[ctop]=0;
- ctop--;
- C[0]--;
- A[0]++;
- printf("\ndysk %d: C->A",A[atop]);
- }
- else if (ctop ==0)
- {
- ctop++;
- C[ctop]=A[atop];
- A[atop]=0;
- atop--;
- C[0]++;
- A[0]--;
- printf("\n dysk %d:A->C",C[ctop]);
- }
- else if(atop !=0 && C[ctop]>A[atop])
- {
- ctop++;
- C[ctop]=A[atop];
- A[atop]=0;
- atop--;
- C[0]++;
- A[0]--;
- printf("\ndysk %d: A->C",C[ctop]);
- }
- else if(atop==0)
- {
- atop++;
- A[atop]=C[ctop];
- C[ctop]=0;
- ctop--;
- C[0]--;
- A[0]++;
- printf("\ndysk %d: C->A",A[atop]);
- }
- }//ruch innych dyskow
- break;
- case 2://ruch kijkow A i B
- printf("\n%d.) Ruch",ruch);
- if(ruch%2 ==1)//move of dysk 1
- {
- if(A[atop]==1)
- {
- btop++;
- B[btop]=A[atop];
- A[atop]=0;
- atop--;
- B[0]++;
- A[0]--;
- printf("\ndysk %d: A->B",B[btop]);
- } //i jest na A
- else if(B[btop]==1)//1 jest na b
- {
- atop++;
- A[atop]=B[btop];
- B[btop]=0;
- btop--;
- A[0]++;
- B[0]--;
- printf("\ndysk %d: B->A",A[atop]);
- }
- }//ruch dysku 1
- else if(ruch %2 ==0)
- {
- if(btop !=0 && A[atop]>B[btop])
- { //B[0]--;
- atop++;
- A[atop]=B[btop];
- B[btop]=0;
- btop--;
- B[0]--;
- A[0]++;
- printf("\ndysk %d: B->A",A[atop]);
- }
- else if (btop ==0)
- {
- btop++;
- B[btop]=A[atop];
- A[atop]=0;
- atop--;
- B[0]++;
- A[0]--;
- printf("\n dysk %d:A->B",B[btop]);
- }
- else if(atop !=0 && B[btop]>A[atop])
- {
- btop++;
- B[btop]=A[atop];
- A[atop]=0;
- atop--;
- B[0]++;
- A[0]--;
- printf("\ndysk %d: A->B",B[btop]);
- }
- else if(atop==0)
- {
- atop++;
- A[atop]=B[btop];
- B[btop]=0;
- btop--;
- B[0]--;
- A[0]++;
- printf("\ndysk %d: B->A",A[atop]);
- }
- }//ruch innych dyskow
- break;
- case 3://ruch kijkow C i B
- printf("\n%d.) Ruch",ruch);
- if(ruch%2 ==1)//move of dysk 1
- {
- if(C[ctop]==1)
- {
- btop++;
- B[btop]=C[ctop];
- C[ctop]=0;
- ctop--;
- B[0]++;
- C[0]--;
- printf("\ndysk %d: C->B",B[btop]);
- } //i jest na C
- else if(B[btop]==1)
- { //1 jest b
- ctop++;
- C[ctop]=B[btop];
- B[btop]=0;
- btop--;
- C[0]++;
- B[0]--;
- printf("\ndysk %d: B->C",C[ctop]);
- }
- }//ruch dysku 1
- else if(ruch %2 ==0)
- {
- if(btop !=0 && C[ctop]>B[btop])
- {//B[0]--;
- ctop++;
- C[ctop]=B[btop];
- B[btop]=0;
- btop--;
- B[0]--;
- C[0]++;
- printf("\ndysk %d: B->C",C[ctop]);
- }
- else if (btop ==0)
- {
- btop++;
- B[btop]=C[ctop];
- C[ctop]=0;
- ctop--;
- B[0]++;
- C[0]--;
- printf("\n dysk %d:C->B",B[btop]);
- }
- else if(ctop !=0 && B[btop]>C[ctop])
- {
- btop++;
- B[btop]=C[ctop];
- C[ctop]=0;
- ctop--;
- B[0]++;
- C[0]--;
- printf("\ndysk %d: C->B",B[btop]);
- }
- else if(ctop==0)
- {
- ctop++;
- C[ctop]=B[btop];
- B[btop]=0;
- btop--;
- B[0]--;
- C[0]++;
- printf("\ndysk %d: B->C",C[ctop]);
- }
- }//ruch innych dyskow
- break;
- default:
- printf("Blad!");
- }
- }
- while((ruch <=max) && (C[0]!=n) );
- printf("\n");
- stan(A,B,C,atop,btop,ctop);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement