Language: C++

#include <iostream>
#include <queue>

using namespace std;
struct PointPair {
        int step;
        int tx, ty, kx, ky;
};

const int dx[] = { -1, 1, 0, 0 };
const int dy[] = { 0, 0, -1, 1 };

bool passed[50][50][50][50];
bool field[50][50];

int main(){
        while(true){
                memset(passed, 0, sizeof(passed));
                memset(field, 0, sizeof(field));
                int X, Y, tx, ty, kx, ky;
                cin >> X >> Y;
                if(X == 0 || Y == 0){ break; }
                cin >> tx >> ty >> kx >> ky;
                for(int i = 0; i < Y; ++i){
                        for(int j = 0; j < X; ++j){
                                int f;
                                cin >> f;
                                field[i][j] = (f != 0);
                        }
                }
                --tx; --ty; --kx; --ky;
                PointPair init = { 0, tx, ty, kx, ky };
                queue<PointPair> q;
                q.push(init);
                passed[tx][ty][kx][ky] = true;
                int answer = -1;
                while(!q.empty()){
                        PointPair p = q.front();
                        if(p.step >= 100){ break; }
                        if(p.tx == p.kx && p.ty == p.ky){
                                answer = p.step;
                                break;
                        }
                        q.pop();
                        for(int i = 0; i < 4; ++i){
                                PointPair next;
                                next.step = p.step + 1;
                                next.tx = p.tx + dx[i];
                                next.ty = p.ty + dy[i];
                                if(field[next.tx][next.ty]){
                                        next.tx = p.tx;
                                        next.ty = p.ty;
                                }else{
                                        if(next.tx < 0){ next.tx = 0; }
                                        if(next.tx >= X){ next.tx = X - 1; }
                                        if(next.ty < 0){ next.ty = 0; }
                                        if(next.ty >= Y){ next.ty = Y - 1; }
                                }
                                next.kx = p.kx - dx[i];
                                next.ky = p.ky - dy[i];
                                if(field[next.kx][next.ky]){
                                        next.kx = p.kx;
                                        next.ky = p.ky;
                                }else{
                                        if(next.kx < 0){ next.kx = 0; }
                                        if(next.kx >= X){ next.kx = X - 1; }
                                        if(next.ky < 0){ next.ky = 0; }
                                        if(next.ky >= Y){ next.ky = Y - 1; }
                                }
                                if(!passed[next.tx][next.ty][next.kx][next.ky]){
                                        passed[next.tx][next.ty][next.kx][next.ky] = true;
                                        q.push(next);
                                }
                        }
                }
                if(answer < 0){
                        cout << "NA" << endl;
                }else{
                        cout << answer << endl;
                }
        }
        return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:18:17: error: 'memset' was not declared in this scope
   18 |                 memset(passed, 0, sizeof(passed));
      |                 ^~~~~~
a.cc:3:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    2 | #include <queue>
  +++ |+#include <cstring>
    3 |