fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int latinski_kvadrat[6][6];
  6. int n;
  7.  
  8. bool moze_broj(int red, int kolona, int broj)
  9. {
  10. for (int x = 0; x < n; x++)
  11. if (latinski_kvadrat[red][x] == broj or latinski_kvadrat[x][kolona] == broj)
  12. return false;
  13. return true;
  14. }
  15.  
  16. void resi(int red, int kolona)
  17. {
  18. if (red == n)
  19. {
  20. for (int i = 0; i < n; i++)
  21. {
  22. for (int j = 0; j < n; j++)
  23. cout << latinski_kvadrat[i][j];
  24. cout << endl;
  25. }
  26. cout << endl;
  27. return;
  28. }
  29. int sledeci_red = red;
  30. if (kolona == n - 1)
  31. sledeci_red++;
  32. int sledeca_kolona;
  33. if (kolona == n - 1)
  34. sledeca_kolona = 0;
  35. else
  36. sledeca_kolona = kolona + 1;
  37. if (latinski_kvadrat[red][kolona] != 0)
  38. resi(sledeci_red, sledeca_kolona);
  39. else
  40. for (int broj = 1; broj <= n; broj++)
  41. if (moze_broj(red, kolona, broj))
  42. {
  43. latinski_kvadrat[red][kolona] = broj;
  44. resi(sledeci_red, sledeca_kolona);
  45. latinski_kvadrat[red][kolona] = 0;
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. cin >> n;
  52. for (int i = 0; i < n; i++)
  53. {
  54. string linija;
  55. cin >> linija;
  56. for (int j = 0; j < n; j++)
  57. latinski_kvadrat[i][j] = linija[j] - '0';
  58. }
  59. resi(0, 0);
  60. return 0;
  61. }
Success #stdin #stdout 0s 5320KB
stdin
3
120
000
000
stdout
123
231
312

123
312
231