Advertisement
Five_NT

[C++]Generare cicluri intre oricare 2 vf.

Dec 2nd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream f("citire.in");
  7. int a[100][100], i, j, n, p, q, sol, x[100], k;
  8.  
  9. void citire()
  10. {
  11.     f>>n;
  12.     for(i=1; i<=n; i++)
  13.         for(j=1; j<=n; j++)
  14.             f>>a[i][j];
  15. }
  16.  
  17. int cont(int k)
  18. {
  19.     for(int i=1; i<k; i++)
  20.     {
  21.         if(x[i] == x[k])
  22.             return 0;
  23.         if(k > 1)
  24.             if(a[x[k]][x[k-1]] == 0)
  25.                 return 0;
  26.     }
  27.     return 1;
  28. }
  29.  
  30. void afis(int k)
  31. {
  32.     cout<<"Sol nr "<<sol+1<<": ";
  33.     for(int i=1; i<=k; i++)
  34.         cout<<x[i]<<" ";
  35.     cout<<x[1];
  36.     cout<<'\n';
  37.     sol++;
  38. }
  39.  
  40. void back()
  41. {
  42.     int k=2; x[k]=0;
  43.     do
  44.     {
  45.         while(x[k] < n)
  46.         {
  47.             x[k]++;
  48.             if(cont(k))
  49.                 if(k >= 3 && a[x[1]][x[k]] == 1)
  50.                     afis(k);
  51.                 else
  52.                 {
  53.                     k++;
  54.                     x[k]=0;
  55.                 }
  56.         }
  57.         k--;
  58.     }while(k > 1);
  59. }
  60.  
  61. int main()
  62. {
  63.     citire();
  64.     for(int i=1; i<=n; i++)
  65.     {
  66.         cout<<"Ciclurile care pornesc din "<<i<<":"<<'\n';
  67.             x[1]=i;
  68.             back();
  69.     }
  70.     cout<<"Avem "<<sol<<" solutii.";
  71.     return 0;
  72. }
  73. /* citire.in */
  74. 6
  75. 0 1 1 0 1 0
  76. 1 0 1 1 0 0
  77. 1 1 0 1 0 0
  78. 0 1 1 0 1 0
  79. 1 0 0 1 0 1
  80. 0 0 1 0 1 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement