SHARE
TWEET

ASD Pb 2 cal

madalinaradu May 26th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*** p2
  2. *Fiind data o tabla de sah de dimensiunea nxn si un cal in coltul stanga sus al acesteia,
  3.  se cere sa se afiseze toate posibilitatile de mutare a acestei piese de sah astfel incat
  4.  sa treaca o singura data prin fiecare patrat al tablei.
  5.  O solutie va fi afisata ca o matrice nxn in care sunt numerotate sariturile calului.
  6. *Exemplu, pentru n=5, o solutie este
  7. *1 14 9 20 23
  8. *10 19 22 15 8
  9. *5 2 13 24 21
  10. *18 11 4 7 16
  11. *3 6 17 12 25
  12. ***/
  13.  
  14. /***
  15. *calul este asezat random
  16. *
  17. ***/
  18. #include<fstream>
  19. #include<iostream>
  20. #include <stdlib.h>
  21. #include<time.h>
  22. using namespace std;
  23.  
  24. const int dy[8]= {-2,-2,-1,1,2,2,1,-1};
  25. const int dx[8]= {-1,1,2,2,1,-1,-2,-2};
  26.  
  27. int a[10][10],n;
  28. ofstream f("cal.out");
  29.  
  30. void afis() {
  31.     int i,j;
  32.     for(i=1; i<=n; i++) {
  33.         for(j=1; j<=n; j++)
  34.             f<<a[i][j]<<"   ";
  35.         f<<endl;
  36.     }
  37.     f<<endl;
  38. }
  39.  
  40. int inside(int i,int j) {
  41.     return i>=1 && i<=n && j>=1 && j<=n;
  42. }
  43.  
  44. void back(int i, int j, int pas) {
  45.     int k,inou,jnou;
  46.     a[i][j]=pas;
  47.     if (pas==n*n)
  48.         afis();
  49.     else
  50.         for(k=0; k<8; k++) {
  51.             inou=i+dx[k];
  52.             jnou=j+dy[k];
  53.             if (inside(inou,jnou) && a[inou][jnou]==0)
  54.                 back(inou,jnou,pas+1);
  55.         }
  56.     a[i][j]=0;
  57. }
  58.  
  59. int main() {
  60.     cout<<"dati n: ";
  61.     cin>>n;
  62.     for(int i=1; i<=n; i++) {
  63.         for(int j=1; j<=n; j++) {
  64.             a[i][j]=0;
  65.         }
  66.     }
  67.     srand(time(NULL));
  68.     int i=1+rand()%n;
  69.     int j=1+rand()%n;
  70.     back(i,j,1);
  71.     return 0;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top