Jayakrishna14

Smart Squares

Sep 8th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main() {
  6.     ios::sync_with_stdio(false);
  7.     cin.tie(nullptr);
  8.     cout.tie(nullptr);
  9.     vector<vector<vector<int>>> smartSquares = {
  10.         {{1,2,7},{6,5,4},{3,8,9}},
  11.         {{1,2,7},{6,5,4},{8,3,9}},
  12.         {{1,2,7},{6,5,9},{3,8,4}},
  13.         {{1,2,7},{6,5,9},{8,3,4}},
  14.         {{1,6,3},{2,5,8},{7,4,9}},
  15.         {{1,6,3},{2,5,8},{7,9,4}},
  16.         {{1,6,3},{7,5,8},{2,4,9}},
  17.         {{1,6,3},{7,5,8},{2,9,4}},
  18.         {{1,6,8},{2,5,3},{7,4,9}},
  19.         {{1,6,8},{2,5,3},{7,9,4}},
  20.         {{1,6,8},{7,5,3},{2,4,9}},
  21.         {{1,6,8},{7,5,3},{2,9,4}},
  22.         {{1,7,2},{6,5,4},{3,8,9}},
  23.         {{1,7,2},{6,5,4},{8,3,9}},
  24.         {{1,7,2},{6,5,9},{3,8,4}},
  25.         {{1,7,2},{6,5,9},{8,3,4}},
  26.         {{2,4,9},{7,5,8},{1,6,3}},
  27.         {{2,4,9},{7,5,8},{6,1,3}},
  28.         {{2,4,9},{7,5,3},{1,6,8}},
  29.         {{2,4,9},{7,5,3},{6,1,8}},
  30.         {{2,7,6},{4,5,1},{9,8,3}},
  31.         {{2,7,6},{4,5,1},{9,3,8}},
  32.         {{2,7,6},{9,5,1},{4,8,3}},
  33.         {{2,7,6},{9,5,1},{4,3,8}},
  34.         {{2,7,1},{4,5,6},{9,8,3}},
  35.         {{2,7,1},{4,5,6},{9,3,8}},
  36.         {{2,7,1},{9,5,6},{4,8,3}},
  37.         {{2,7,1},{9,5,6},{4,3,8}},
  38.         {{2,9,4},{7,5,3},{6,1,8}},
  39.         {{2,9,4},{7,5,3},{1,6,8}},
  40.         {{2,9,4},{7,5,8},{6,1,3}},
  41.         {{2,9,4},{7,5,8},{1,6,3}},
  42.         {{3,1,6},{8,5,2},{4,9,7}},
  43.         {{3,1,6},{8,5,2},{9,4,7}},
  44.         {{3,1,6},{8,5,7},{4,9,2}},
  45.         {{3,1,6},{8,5,7},{9,4,2}},
  46.         {{3,6,1},{8,5,2},{4,9,7}},
  47.         {{3,6,1},{8,5,2},{9,4,7}},
  48.         {{3,6,1},{8,5,7},{4,9,2}},
  49.         {{3,6,1},{8,5,7},{9,4,2}},
  50.         {{3,8,4},{1,5,9},{6,2,7}},
  51.         {{3,8,4},{1,5,9},{6,7,2}},
  52.         {{3,8,4},{6,5,9},{1,2,7}},
  53.         {{3,8,4},{6,5,9},{1,7,2}},
  54.         {{3,8,9},{6,5,4},{1,2,7}},
  55.         {{3,8,9},{6,5,4},{1,7,2}},
  56.         {{3,8,9},{1,5,4},{6,2,7}},
  57.         {{3,8,9},{1,5,4},{6,7,2}},
  58.         {{4,3,8},{9,5,6},{7,2,1}},
  59.         {{4,3,8},{9,5,6},{2,7,1}},
  60.         {{4,3,8},{9,5,1},{7,2,6}},
  61.         {{4,3,8},{9,5,1},{2,7,6}},
  62.         {{4,8,3},{9,5,6},{7,2,1}},
  63.         {{4,8,3},{9,5,6},{2,7,1}},
  64.         {{4,8,3},{9,5,1},{7,2,6}},
  65.         {{4,8,3},{9,5,1},{2,7,6}},
  66.         {{4,9,7},{3,5,2},{8,1,6}},
  67.         {{4,9,7},{3,5,2},{8,6,1}},
  68.         {{4,9,7},{8,5,2},{3,1,6}},
  69.         {{4,9,7},{8,5,2},{3,6,1}},
  70.         {{4,9,2},{8,5,7},{3,1,6}},
  71.         {{4,9,2},{8,5,7},{3,6,1}},
  72.         {{4,9,2},{3,5,7},{8,6,1}},
  73.         {{4,9,2},{3,5,7},{8,1,6}},
  74.         {{6,2,7},{1,5,4},{3,8,9}},
  75.         {{6,2,7},{1,5,4},{8,3,9}},
  76.         {{6,2,7},{1,5,9},{3,8,4}},
  77.         {{6,2,7},{1,5,9},{8,3,4}},
  78.         {{6,1,3},{2,5,8},{7,4,9}},
  79.         {{6,1,3},{2,5,8},{7,9,4}},
  80.         {{6,1,3},{7,5,8},{2,4,9}},
  81.         {{6,1,3},{7,5,8},{2,9,4}},
  82.         {{6,1,8},{2,5,3},{7,4,9}},
  83.         {{6,1,8},{2,5,3},{7,9,4}},
  84.         {{6,1,8},{7,5,3},{2,4,9}},
  85.         {{6,1,8},{7,5,3},{2,9,4}},
  86.         {{6,7,2},{1,5,4},{3,8,9}},
  87.         {{6,7,2},{1,5,4},{8,3,9}},
  88.         {{6,7,2},{1,5,9},{3,8,4}},
  89.         {{6,7,2},{1,5,9},{8,3,4}},
  90.         {{7,2,6},{4,5,1},{9,8,3}},
  91.         {{7,2,6},{4,5,1},{9,3,8}},
  92.         {{7,2,6},{9,5,1},{4,8,3}},
  93.         {{7,2,6},{9,5,1},{4,3,8}},
  94.         {{7,2,1},{4,5,6},{9,8,3}},
  95.         {{7,2,1},{4,5,6},{9,3,8}},
  96.         {{7,2,1},{9,5,6},{4,8,3}},
  97.         {{7,2,1},{9,5,6},{4,3,8}},
  98.         {{7,4,9},{2,5,8},{1,6,3}},
  99.         {{7,4,9},{2,5,8},{6,1,3}},
  100.         {{7,4,9},{2,5,3},{1,6,8}},
  101.         {{7,4,9},{2,5,3},{6,1,8}},
  102.         {{7,9,4},{2,5,8},{1,6,3}},
  103.         {{7,9,4},{2,5,8},{6,1,3}},
  104.         {{7,9,4},{2,5,3},{1,6,8}},
  105.         {{7,9,4},{2,5,3},{6,1,8}},
  106.         {{8,3,4},{6,5,9},{1,7,2}},
  107.         {{8,3,4},{6,5,9},{1,2,7}},
  108.         {{8,3,4},{1,5,9},{6,2,7}},
  109.         {{8,3,4},{1,5,9},{6,7,2}},
  110.         {{8,3,9},{6,5,4},{1,7,2}},
  111.         {{8,3,9},{6,5,4},{1,2,7}},
  112.         {{8,3,9},{1,5,4},{6,7,2}},
  113.         {{8,3,9},{1,5,4},{6,2,7}},
  114.         {{8,6,1},{3,5,2},{4,9,7}},
  115.         {{8,6,1},{3,5,2},{9,4,7}},
  116.         {{8,6,1},{3,5,7},{4,9,2}},
  117.         {{8,6,1},{3,5,7},{9,4,2}},
  118.         {{8,1,6},{3,5,7},{4,9,2}},
  119.         {{8,1,6},{3,5,7},{9,4,2}},
  120.         {{8,1,6},{3,5,2},{4,9,7}},
  121.         {{8,1,6},{3,5,2},{9,4,7}},
  122.         {{9,3,8},{4,5,6},{7,2,1}},
  123.         {{9,3,8},{4,5,6},{2,7,1}},
  124.         {{9,3,8},{4,5,1},{7,2,6}},
  125.         {{9,3,8},{4,5,1},{2,7,6}},
  126.         {{9,4,2},{3,5,7},{8,6,1}},
  127.         {{9,4,2},{3,5,7},{8,1,6}},
  128.         {{9,4,2},{8,5,7},{3,6,1}},
  129.         {{9,4,2},{8,5,7},{3,1,6}},
  130.         {{9,4,7},{3,5,2},{8,6,1}},
  131.         {{9,4,7},{3,5,2},{8,1,6}},
  132.         {{9,4,7},{8,5,2},{3,6,1}},
  133.         {{9,4,7},{8,5,2},{3,1,6}},
  134.         {{9,8,3},{4,5,6},{7,2,1}},
  135.         {{9,8,3},{4,5,6},{2,7,1}},
  136.         {{9,8,3},{4,5,1},{7,2,6}},
  137.         {{9,8,3},{4,5,1},{2,7,6}}
  138.     };
  139.    
  140.  
  141.     int Q;
  142.     cin >> Q;
  143.     while(Q--){
  144.         vector<vector<int>> arr(3, vector<int>(3));
  145.         for(int i=0; i<3; i++){
  146.             for(int j=0; j<3; j++){
  147.                 cin >> arr[i][j];
  148.             }
  149.         }
  150.  
  151.         int mn = 1000;
  152.         for(vector<vector<int>> square: smartSquares){
  153.             int val = 0;
  154.             for(int i=0; val < mn && i<9; i++){
  155.                 val += abs(square[i/3][i%3] - arr[i/3][i%3]);
  156.             }
  157.             mn = min(mn, val);
  158.             if(mn == 0) break;
  159.         }
  160.        
  161.         cout << mn << endl;
  162.     }
  163.     return 0;
  164. }
  165.  
  166.  
Advertisement
Add Comment
Please, Sign In to add comment