View Code of Problem 3852

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};
int arr[101][101];
int a[101][101];
int step;
int T, m, n, L;
int min_step;
void to_find(int x, int y)
{
    if (x != n || y != m)
    {
        for (int i = 0; i < 4; i++)
        {
            int X = x + xx[i];
            int Y = y + yy[i];
            if (X >= 1 && X <= n && Y >= 1 && Y <= m && abs(arr[X][Y] - arr[x][y]) <= L && a[X][Y] == 0){
                a[x][y] = 1;
                step++;
                to_find(X, Y);
                step--;
                a[x][y] = 0;
            }
        }
    }else{
        if(min_step > step)
            min_step = step;
    }
}

int main(void)
{
    cin >> T;
    while (T--)
    {
        cin >> n >> m >> L;
        
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> arr[i][j];
            }
        }
        step = 0;
        min_step = 99999;
        memset(a, 0, sizeof(a));
        to_find(1, 1);
        cout << min_step << endl;
    }
}

Double click to view unformatted code.


Back to problem 3852