Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1 << 30)

int xs[] = {0, 1, 0, -1}, ys[] = {1, 0, -1, 0};
int x, y;
int field[50][50];
char dp[50][50][50][50];

int solve(int tx, int ty, int kx, int ky){
  int ntx, nty, nkx, nky;
  if(dp[tx][ty][kx][ky] != -1){
    return dp[tx][ty][kx][ky];
  }
  dp[tx][ty][kx][ky] = INF;
  for(int i = 0;i < 4;i++){
    ntx = tx + xs[i];
    nty = ty + ys[i];
    nkx = kx - xs[i];
    nky = ky - ys[i];
    if(ntx < 0 || ntx >= x || nty < 0 || nty >= y || field[ntx][nty] == 1){
      ntx = tx;
      nty = ty;
    }
    if(nkx < 0 || nkx >= x || nky < 0 || nky >= y || field[nkx][nky] == 1){
      nkx = kx;
      nky = ky;
    }
    dp[tx][ty][kx][ky] = min(dp[tx][ty][kx][ky], solve(ntx, nty, nkx, nky) + 1);
  }
  return dp[tx][ty][kx][ky];
}

int main(){
  int tx, ty, kx, ky;
  while(true){
    scanf("%d%d", &x, &y);
    memset(dp, -1, sizeof(dp));
    if(x == 0 && y == 0) return 0;
    scanf("%d%d", &tx, &ty);
    scanf("%d%d", &kx, &ky);
    for(int i = 0;i < y;i++){
      for(int j = 0;j < x;j++){
	scanf("%d", &field[x][y]);
	dp[i][j][i][j] = 0;
      }
    }
    int ans = solve(tx - 1, ty - 1, kx - 1, ky - 1);
    if(ans > 100)
      puts("NA");
    else
      printf("%d\n", ans);
  }
}

Compiler output:
a.cc: In function 'int solve(int, int, int, int)':
a.cc:5:16: warning: overflow in conversion from 'int' to 'char' changes value from '1073741824' to '0' [-Woverflow]
    5 | #define INF (1 << 30)
      |             ~~~^~~~~~
a.cc:17:24: note: in expansion of macro 'INF'
   17 |   dp[tx][ty][kx][ky] = INF;
      |                        ^~~
a.cc:31:29: error: no matching function for call to 'min(char&, int)'
   31 |     dp[tx][ty][kx][ky] = min(dp[tx][ty][kx][ky], solve(ntx, nty, nkx, nky) + 1);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:31:29: note:   deduced conflicting types for parameter 'const _Tp' ('char' and 'int')
   31 |     dp[tx][ty][kx][ky] = min(dp[tx][ty][kx][ky], solve(ntx, nty, nkx, nky) + 1);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:31:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'char'
   31 |     dp[tx][ty][kx][ky] = min(dp[tx][ty][kx][ky], solve(ntx, nty, nkx, nky) + 1);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~