fork(1) 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. return;
  27. }
  28. int sledeci_red = red;
  29. if (kolona == n - 1)
  30. sledeci_red++;
  31. int sledeca_kolona;
  32. if (kolona == n - 1)
  33. sledeca_kolona = 0;
  34. else
  35. sledeca_kolona = kolona + 1;
  36. if (latinski_kvadrat[red][kolona] != 0)
  37. resi(sledeci_red, sledeca_kolona);
  38. else
  39. for (int broj = 1; broj <= n; broj++)
  40. if (moze_broj(red, kolona, broj))
  41. {
  42. latinski_kvadrat[red][kolona] = broj;
  43. resi(sledeci_red, sledeca_kolona);
  44. latinski_kvadrat[red][kolona] = 0;
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. cin >> n;
  51. for (int i = 0; i < n; i++)
  52. {
  53. string linija;
  54. cin >> linija;
  55. for (int j = 0; j < n; j++)
  56. latinski_kvadrat[i][j] = linija[j] - '0';
  57. }
  58. resi(0, 0);
  59. return 0;
  60. }
Success #stdin #stdout 0s 5316KB
stdin
3
120
000
000
stdout
123
231
312
123
312
231