#include<iostream>
#include<algorithm>
#include<vector>
#include "locmin.h"
#define MAXN 500
#define MAXM 500
using namespace std;
long long int known[MAXN][MAXM];
int n,m;
long long int query (int r, int c) {
    if ((r<0)||(r>=n)||(c<0)||(c>=m)) return 1e18;
    if (known[r][c]!=0) return known[r][c];
    return known[r][c]=value(r+1,c+1);
}
int drow[]={ -1, -0, +0, +1 };
int dcol[]={ -0, -1, +1, +0 };
pair <int, int> find_locmin_point (int n, int m) {
    ::n=n; ::m=m;
    int r1=0,r2=n-1,c1=0,c2=m-1;
    int r=rand()%n,c=rand()%m;
    //cout << r+1 << " " << c+1 << endl ;
    for (;;) {
        bool flag=true;
        for (int i=0; i<4; i++) {
            int row=r+drow[i],col=c+dcol[i];
            if (query(row,col)<query(r,c)) {
                r+=drow[i]; c+=dcol[i];
                flag=false;
                break;
            }
        }
        if (flag==true) return {r+1, c+1};
    }
}
