Advertisement
PedalaVasile

Untitled

Dec 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("hercule.in");
  8. ofstream fout("hercule.out");
  9.  
  10. int di[] = {1, 0};
  11. int dj[] = {0, 1};
  12.  
  13. int res = 0;
  14.  
  15. int a[11][11];
  16. int n, m;
  17.  
  18. bool e_corect(int i, int j, int TIMP)
  19. {
  20.     return i <= n && j <= m && a[i][j] - TIMP > 0;
  21. }
  22.  
  23. void backtr(int i, int j, int TIMP = 1)
  24. {
  25.     a[i][j] -= TIMP;
  26.  
  27.     if(i == n && j == m)
  28.     {
  29.         for(int i = 1; i <= n; i++)
  30.         {
  31.             for(int j = 1; j <= m; j++)
  32.             {
  33.                 fout << setw(4) << a[i][j] << ' ';
  34.             }
  35.             fout << '\n';
  36.         }
  37.  
  38.         fout << "\n\n";
  39.  
  40.         res++;
  41.         return;
  42.     }
  43.  
  44.     for(int k = 0; k < 2; k++)
  45.     {
  46.         int nexti = i + di[k];
  47.         int nextj = j + dj[k];
  48.  
  49.         if(e_corect(nexti, nextj, TIMP))
  50.         {
  51.             backtr(nexti, nextj, TIMP + 1);
  52.             a[i][j] += TIMP;
  53.         }
  54.  
  55.         }
  56.  
  57.  
  58. }
  59.  
  60. int main()
  61. {
  62.     fin >> n >> m;
  63.  
  64.     for(int i = 1; i <= n; i++)
  65.         for(int j = 1; j <= m; j++)
  66.             fin >> a[i][j];
  67.  
  68.     backtr(1, 1);
  69.  
  70.     fout << res;
  71.  
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement