Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int N = 5;
  4. int board[N][N];
  5. int chess[N][N];
  6.  
  7.  
  8. int abs(int a){
  9.     return (a>0 ? a : -a);
  10. }
  11.  
  12. void zeroIt(){
  13.     for(int x = 0; x < N; x++){
  14.         for(int y = 0; y < N; y++){
  15.             chess[x][y]=0;
  16.         }
  17.     }
  18. }
  19.  
  20. bool isSafe(int w, int k){
  21.     if(w==0)return true;
  22.     for(int i = 0; i < N; i++){
  23.         if(chess[w-1][i] == 1){
  24.             if(abs(k-i) < 1)return false;
  25.         }
  26.     }
  27.     return true;
  28. }
  29.  
  30. bool rek(int n, int suma){
  31.     if(n == N and suma == 0){
  32.         return true;
  33.     }
  34.     bool kurwa = false;
  35.     for(int i = 0; i < N; i++){
  36.         if(n==N){
  37.  
  38.         }
  39.         if(isSafe(n, i)){
  40.             chess[n][i]=1;
  41.             kurwa = rek(n++, suma+=board[n][i]);
  42.  
  43.         }
  44.  
  45.         chess[n][i]=0;
  46.         suma-=board[n][i];
  47.     }
  48.  
  49.     return kurwa;
  50.  
  51. }
  52.  
  53. void print(){
  54.     for(int x = 0; x < N; x++){
  55.         for(int y = 0; y < N; y++){
  56.             cout<<chess[x][y] << " ";
  57.         }
  58.         cout<<endl;
  59.     }
  60. }
  61. int main() {
  62.  
  63.     zeroIt();
  64.     for(int x = 0; x < N; x++){
  65.         for(int y = 0; y < N; y++){
  66.             int m;
  67.             cin >>m;
  68.             chess[x][y] = m;
  69.         }
  70.         cout << endl;
  71.     }
  72.  
  73.     if(rek(0, 0)){
  74.         cout<<"a mz";
  75.         print();
  76.     }else {
  77.         cout << " CHUJ";
  78.     }
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement