Advertisement
YEZAELP

SMMR-138: Rook

Jun 7th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,mx=-100000;
  4. int ar[11][11];
  5. int column[11],line[11];
  6. vector <int> x,y,ansx,ansy;
  7.  
  8. void rook(int i,int j,int sum){
  9.  
  10.     if(sum>mx && j==n) {
  11.         ansx=x;
  12.         ansy=y;
  13.         mx=sum;
  14.         return ;
  15.     }
  16.  
  17.     for(int I=1;I<=n;I++){
  18.         if(I!=i && column[j+1]==0 && line[I]==0){
  19.             column[j+1]++;
  20.             line[I]++;
  21.             x.push_back(I);
  22.             y.push_back(j+1);
  23.             rook(I,j+1,sum+ar[I][j+1]);
  24.             x.pop_back();
  25.             y.pop_back();
  26.             column[j+1]--;
  27.             line[I]--;
  28.         }
  29.     }
  30. }
  31.  
  32.  
  33. int main(){
  34.  
  35.     scanf("%d",&n);
  36.     for(int i=1;i<=n;i++){
  37.         for(int j=1;j<=n;j++){
  38.             column[j]=0;
  39.             line[i]=0;
  40.             scanf("%d",&ar[i][j]);
  41.         }
  42.     }
  43.  
  44.     for(int i=1;i<=n;i++){
  45.         column[1]++;
  46.         line[i]++;
  47.         x.push_back(i);
  48.         y.push_back(1);
  49.         rook(i,1,ar[i][1]);
  50.         x.pop_back();
  51.         y.pop_back();
  52.         column[1]--;
  53.         line[i]--;
  54.     }
  55.  
  56.     printf("%d\n",mx);
  57.     for(int i=0;i<ansx.size();i++){
  58.         printf("%d %d\n",ansx[i],ansy[i]);
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement