#include "vision.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<math.h>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

std::vector<int> getVisionPattern1d()
{
    return { 3,1,1,1,2,1 };
}

int getMove1d(std::vector<int> v)
{
    ll m = (v.size() - 1) / 2;
    set<pll> s;
    for (ll i = 0; i < v.size(); i++) {
        if (i == m) { continue; }
        s.insert({ v[i], i });
    }
    vpll now;
    for (auto i : s) {
        now.push_back(i);
    }
    if (m == 1) {
        return now[1].second;
    }
    else if (m == 2) {
        if (v[m + 2] == 3) {
            return m + 1;
        }
        else {
            return m - 1;
        }
    }
    else {
        if (v[m + 2] == 2) {
            return m - 3;
        }
        else {
            return m + 3;
        }
    }
}

std::vector<std::vector<int>> getVisionPattern2d()
{
    return {
        {1,1,1,1,1},
        {1,2,1,1,1},
        {1,1,1,2,1},
        {1,1,1,1,3}
    };
}
    
std::pair<int, int> getMove2d(std::vector<std::vector<int>> v)
{
    ll m = (v.size() - 1) / 2;
    vector<pair<ll, pll>> now;
    for (ll i = 0; i < v.size(); i++) {
        for (ll j = 0; j < v.size(); j++) {
            if (i == m && j == m) { continue; }
            now.push_back({ v[i][j], {i,j} });
        }
    }
    sort(now.begin(), now.end());
    if (m == 1) {
        return now.back().second;
    }
    else if (m == 2) {
        pll maxval = now.back().second;
        if (abs(m - maxval.second) <= 1) {
            return now.back().second;
        }
        else {
            if (m > maxval.second) {
                return { m, m + 2 };
            }
            else {
                return { m, m - 2 };
            }
        }
    }
    else {
        pll ans = { 2 * m, 2 * m };
        bool x_flip = true;
        for (ll i = 0; i < 2 * m + 1; i++) {
            if (v[m - 1][i] != 1) {
                x_flip = false;
            }
        }
        if (x_flip) { ans.first = 0; }
        bool y_fliped = true;
        for (ll i = m - 1; i <= m + 1; i++) {
            if (v[i][m - 1] != 1) {
                y_fliped = false;
            }
        }
        if (y_fliped) {
            ans.second = 0;
        }
        return ans;
    }
}