Click Here to go back to the homepage.

Unlock Pattern Solution:


#include <bits/stdc++.h>

using namespace std;

double next(int*, int*);

int grid[3][3];

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    
    int curr_r, curr_c;
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            cin >> grid[i][j];
            if(grid[i][j] == 1){
                curr_r = i;
                curr_c = j;
            }
        }
    }
    double distance = 0;
    for(int i = 0; i < 8; i++){
        distance += next(&curr_r, &curr_c);
    }
    cout << setprecision(8) << distance << endl;
    
    return 0;
}

double next(int *curr_row, int *curr_column){
    int curr_num = grid[*curr_row][*curr_column];
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(grid[i][j] == curr_num + 1){
                int row_diff = abs(i - *curr_row);
                int column_diff = abs(j - *curr_column);
                *curr_row = i;
                *curr_column = j;

                if((row_diff == 1 && column_diff == 0) || (row_diff == 0 && column_diff == 1)){
                    return 1;
                }
                if((row_diff == 2 && column_diff == 0) || (row_diff == 0 && column_diff == 2)){
                    return 2;
                }
                if(row_diff == 1 && column_diff == 1){
                    return 1.41421356237;
                }
                if(row_diff == 2 && column_diff == 2){
                    return 2 * 1.41421356237;
                }
                return 2.23606797749979;
            }

        }
    }
    return -1;
    // shouldn't ever happen, don't call function with index of 9.
}