Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- vector<vector<vector<int>>> smartSquares = {
- {{1,2,7},{6,5,4},{3,8,9}},
- {{1,2,7},{6,5,4},{8,3,9}},
- {{1,2,7},{6,5,9},{3,8,4}},
- {{1,2,7},{6,5,9},{8,3,4}},
- {{1,6,3},{2,5,8},{7,4,9}},
- {{1,6,3},{2,5,8},{7,9,4}},
- {{1,6,3},{7,5,8},{2,4,9}},
- {{1,6,3},{7,5,8},{2,9,4}},
- {{1,6,8},{2,5,3},{7,4,9}},
- {{1,6,8},{2,5,3},{7,9,4}},
- {{1,6,8},{7,5,3},{2,4,9}},
- {{1,6,8},{7,5,3},{2,9,4}},
- {{1,7,2},{6,5,4},{3,8,9}},
- {{1,7,2},{6,5,4},{8,3,9}},
- {{1,7,2},{6,5,9},{3,8,4}},
- {{1,7,2},{6,5,9},{8,3,4}},
- {{2,4,9},{7,5,8},{1,6,3}},
- {{2,4,9},{7,5,8},{6,1,3}},
- {{2,4,9},{7,5,3},{1,6,8}},
- {{2,4,9},{7,5,3},{6,1,8}},
- {{2,7,6},{4,5,1},{9,8,3}},
- {{2,7,6},{4,5,1},{9,3,8}},
- {{2,7,6},{9,5,1},{4,8,3}},
- {{2,7,6},{9,5,1},{4,3,8}},
- {{2,7,1},{4,5,6},{9,8,3}},
- {{2,7,1},{4,5,6},{9,3,8}},
- {{2,7,1},{9,5,6},{4,8,3}},
- {{2,7,1},{9,5,6},{4,3,8}},
- {{2,9,4},{7,5,3},{6,1,8}},
- {{2,9,4},{7,5,3},{1,6,8}},
- {{2,9,4},{7,5,8},{6,1,3}},
- {{2,9,4},{7,5,8},{1,6,3}},
- {{3,1,6},{8,5,2},{4,9,7}},
- {{3,1,6},{8,5,2},{9,4,7}},
- {{3,1,6},{8,5,7},{4,9,2}},
- {{3,1,6},{8,5,7},{9,4,2}},
- {{3,6,1},{8,5,2},{4,9,7}},
- {{3,6,1},{8,5,2},{9,4,7}},
- {{3,6,1},{8,5,7},{4,9,2}},
- {{3,6,1},{8,5,7},{9,4,2}},
- {{3,8,4},{1,5,9},{6,2,7}},
- {{3,8,4},{1,5,9},{6,7,2}},
- {{3,8,4},{6,5,9},{1,2,7}},
- {{3,8,4},{6,5,9},{1,7,2}},
- {{3,8,9},{6,5,4},{1,2,7}},
- {{3,8,9},{6,5,4},{1,7,2}},
- {{3,8,9},{1,5,4},{6,2,7}},
- {{3,8,9},{1,5,4},{6,7,2}},
- {{4,3,8},{9,5,6},{7,2,1}},
- {{4,3,8},{9,5,6},{2,7,1}},
- {{4,3,8},{9,5,1},{7,2,6}},
- {{4,3,8},{9,5,1},{2,7,6}},
- {{4,8,3},{9,5,6},{7,2,1}},
- {{4,8,3},{9,5,6},{2,7,1}},
- {{4,8,3},{9,5,1},{7,2,6}},
- {{4,8,3},{9,5,1},{2,7,6}},
- {{4,9,7},{3,5,2},{8,1,6}},
- {{4,9,7},{3,5,2},{8,6,1}},
- {{4,9,7},{8,5,2},{3,1,6}},
- {{4,9,7},{8,5,2},{3,6,1}},
- {{4,9,2},{8,5,7},{3,1,6}},
- {{4,9,2},{8,5,7},{3,6,1}},
- {{4,9,2},{3,5,7},{8,6,1}},
- {{4,9,2},{3,5,7},{8,1,6}},
- {{6,2,7},{1,5,4},{3,8,9}},
- {{6,2,7},{1,5,4},{8,3,9}},
- {{6,2,7},{1,5,9},{3,8,4}},
- {{6,2,7},{1,5,9},{8,3,4}},
- {{6,1,3},{2,5,8},{7,4,9}},
- {{6,1,3},{2,5,8},{7,9,4}},
- {{6,1,3},{7,5,8},{2,4,9}},
- {{6,1,3},{7,5,8},{2,9,4}},
- {{6,1,8},{2,5,3},{7,4,9}},
- {{6,1,8},{2,5,3},{7,9,4}},
- {{6,1,8},{7,5,3},{2,4,9}},
- {{6,1,8},{7,5,3},{2,9,4}},
- {{6,7,2},{1,5,4},{3,8,9}},
- {{6,7,2},{1,5,4},{8,3,9}},
- {{6,7,2},{1,5,9},{3,8,4}},
- {{6,7,2},{1,5,9},{8,3,4}},
- {{7,2,6},{4,5,1},{9,8,3}},
- {{7,2,6},{4,5,1},{9,3,8}},
- {{7,2,6},{9,5,1},{4,8,3}},
- {{7,2,6},{9,5,1},{4,3,8}},
- {{7,2,1},{4,5,6},{9,8,3}},
- {{7,2,1},{4,5,6},{9,3,8}},
- {{7,2,1},{9,5,6},{4,8,3}},
- {{7,2,1},{9,5,6},{4,3,8}},
- {{7,4,9},{2,5,8},{1,6,3}},
- {{7,4,9},{2,5,8},{6,1,3}},
- {{7,4,9},{2,5,3},{1,6,8}},
- {{7,4,9},{2,5,3},{6,1,8}},
- {{7,9,4},{2,5,8},{1,6,3}},
- {{7,9,4},{2,5,8},{6,1,3}},
- {{7,9,4},{2,5,3},{1,6,8}},
- {{7,9,4},{2,5,3},{6,1,8}},
- {{8,3,4},{6,5,9},{1,7,2}},
- {{8,3,4},{6,5,9},{1,2,7}},
- {{8,3,4},{1,5,9},{6,2,7}},
- {{8,3,4},{1,5,9},{6,7,2}},
- {{8,3,9},{6,5,4},{1,7,2}},
- {{8,3,9},{6,5,4},{1,2,7}},
- {{8,3,9},{1,5,4},{6,7,2}},
- {{8,3,9},{1,5,4},{6,2,7}},
- {{8,6,1},{3,5,2},{4,9,7}},
- {{8,6,1},{3,5,2},{9,4,7}},
- {{8,6,1},{3,5,7},{4,9,2}},
- {{8,6,1},{3,5,7},{9,4,2}},
- {{8,1,6},{3,5,7},{4,9,2}},
- {{8,1,6},{3,5,7},{9,4,2}},
- {{8,1,6},{3,5,2},{4,9,7}},
- {{8,1,6},{3,5,2},{9,4,7}},
- {{9,3,8},{4,5,6},{7,2,1}},
- {{9,3,8},{4,5,6},{2,7,1}},
- {{9,3,8},{4,5,1},{7,2,6}},
- {{9,3,8},{4,5,1},{2,7,6}},
- {{9,4,2},{3,5,7},{8,6,1}},
- {{9,4,2},{3,5,7},{8,1,6}},
- {{9,4,2},{8,5,7},{3,6,1}},
- {{9,4,2},{8,5,7},{3,1,6}},
- {{9,4,7},{3,5,2},{8,6,1}},
- {{9,4,7},{3,5,2},{8,1,6}},
- {{9,4,7},{8,5,2},{3,6,1}},
- {{9,4,7},{8,5,2},{3,1,6}},
- {{9,8,3},{4,5,6},{7,2,1}},
- {{9,8,3},{4,5,6},{2,7,1}},
- {{9,8,3},{4,5,1},{7,2,6}},
- {{9,8,3},{4,5,1},{2,7,6}}
- };
- int Q;
- cin >> Q;
- while(Q--){
- vector<vector<int>> arr(3, vector<int>(3));
- for(int i=0; i<3; i++){
- for(int j=0; j<3; j++){
- cin >> arr[i][j];
- }
- }
- int mn = 1000;
- for(vector<vector<int>> square: smartSquares){
- int val = 0;
- for(int i=0; val < mn && i<9; i++){
- val += abs(square[i/3][i%3] - arr[i/3][i%3]);
- }
- mn = min(mn, val);
- if(mn == 0) break;
- }
- cout << mn << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment