#include <bits/stdc++.h>
#include "locmin.h"

#define ull unsigned long long

using namespace std;

///long long value(int r, int c)

std::pair <int, int> find_locmin_point(int n, int m) {
    ull v[n+5][m+5];
    int d[4][2]={1,  0,
                -1,  0,
                 0,  1,
                 0, -1};

    ull bord=0;bord--;
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)v[i][j]=bord-1;
    for(int i=0;i<=m+1;i++){v[0][i]=bord;v[n+1][i]=bord;}
    for(int i=0;i<=n+1;i++){v[i][0]=bord;v[i][m+1]=bord;}

    //for(int i=0;i<=n+1;i++){for(int j=0;j<=m+1;j++){cout<<v[i][j]<<" ";}cout<<"\n";}

    int curX=n/2, curY=m/2, curVal;
    if(curX==0)curX++;
    if(curY==0)curY++;
    v[curX][curY]=value(curX, curY);

    bool passed=0;
    while(1){
        passed=true;
        curVal=v[curX][curY];
        if(curVal==1)return {curX, curY};
        for(int i=0;passed&&i<4;i++){
            if(v[curX+d[i][0]][curY+d[i][1]]==bord-1){
                v[curX+d[i][0]][curY+d[i][1]]=value(curX+d[i][0], curY+d[i][1]);
                if(v[curX+d[i][0]][curY+d[i][1]]==1)return {curX+d[i][0] ,curY+d[i][1]};
                if(v[curX+d[i][0]][curY+d[i][1]]<curVal){
                    passed=false;
                    curX+=d[i][0];
                    curY+=d[i][1];
                }
            }
        }
        if(passed)return {curX, curY};
    }



}
