Advertisement
STANAANDREY

337pbinfo

Nov 23rd, 2020
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. //https://www.pbinfo.ro/probleme/337/saritura-calului
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int n, m, istart, jstart;
  6. int a[15][15];
  7. int gasit = 0;
  8.  
  9. const int di[] = { -2, -2, -1, -1, +1, +1, +2, +2 },
  10.           dj[] = { -1, +1, -2, +2, -2, +2, -1, +1 };
  11. void afis(int n, int m)
  12. {
  13.     gasit = 1;
  14.     for (int i = 1; i <= n; ++i) {
  15.         for (int j = 1; j <= m; ++j)
  16.             cout << a[i][j] << " ";
  17.         cout << endl;
  18.     }
  19. }
  20.  
  21. void back(int i, int j, int pas)
  22. {
  23.     if (!gasit)
  24.         if (i > 0 && j > 0 && i <= n && j <= m && a[i][j] == 0) {
  25.             a[i][j] = pas;
  26.             if (pas == n * m)
  27.                 afis(n, m);
  28.             else
  29.                 for (int k = 0; k < 8; ++k)
  30.                     back(i + di[k], j + dj[k], pas + 1);
  31.             a[i][j] = 0;
  32.         }
  33. }
  34.  
  35. int main()
  36. {
  37.     cin >> n >> m >> istart >> jstart;
  38.     back(istart, jstart, 1);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement