Language: C++

#include<string.h>
using namespace std;
struct Y{
int hx,hy,rx,ry,tim;
};
int mp[52][52],vh[52][52],vr[52][52];
int main(){
int n,w,h,i,j,a[2]={1,-1},b[2],g,k;
Y e;
queue<Y> q;
while(1){
	g=0;
	k=0;
	cin>>w>>h;
	if(w==0&&h==0)break;
	cin>>e.hx>>e.hy>>e.rx>>e.ry;
	e.tim=0;
	q.push(e);
	memset(mp,0,sizeof(mp));
	memset(vr,0,sizeof(vr));
	memset(vh,0,sizeof(vh));
	for(i=0;i<=h+1;i++){
		mp[i][0]=1;
		mp[i][w+1]=1;
	}
	for(i=0;i<=w+1;i++){
		mp[0][i]=1;
		mp[h+1][i]=1;
	}
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>mp[j][i];
			if(mp[j][i]==1)k++;
		}
	}
	while(1){
		e=q.front();
		q.pop();
				if(vh[e.hy][e.hx]!=1)g++;
				if(vr[e.ry][e.rx]!=1)g++;
				vh[e.hy][e.hx]=1;
				vr[e.ry][e.rx]=1;
		if(e.tim>=100||e.hx==e.rx&&e.hy==e.ry||g==(w*h-k)*2)break;
		e.tim++;
		for(i=0;i<2;i++){
			if(mp[e.hy+a[i]][e.hx]!=1&&vh[e.hy+a[i]][e.hx]!=1||mp[e.ry-a[i]][e.rx]!=1&&vr[e.ry-a[i]][e.rx]!=1){
				b[0]=0;
				b[1]=0;
				if(mp[e.hy+a[i]][e.hx]!=1){
					e.hy+=a[i];
					b[0]=1;
				}
				if(mp[e.ry-a[i]][e.rx]!=1){
					e.ry-=a[i];
					b[1]=1;
				}
				if(vh[e.hy][e.hx]!=1)g++;
				if(vr[e.ry][e.rx]!=1)g++;
				vh[e.hy][e.hx]=1;
				vr[e.ry][e.rx]=1;
				q.push(e);
				if(b[0]==1)e.hy-=a[i];
				if(b[1]==1)e.ry+=a[i];
			}																									
			if(mp[e.hy][e.hx+a[i]]!=1&&vh[e.hy][e.hx+a[i]]!=1||mp[e.ry][e.rx-a[i]]!=1&&vr[e.ry][e.rx-a[i]]!=1){
				b[0]=0;
				b[1]=0;
				if(mp[e.hy][e.hx+a[i]]!=1){
					e.hx+=a[i];
					b[0]=1;
				}
				if(mp[e.ry][e.rx-a[i]]!=1){
					e.rx-=a[i];
					b[1]=1;
				}
				if(vh[e.hy][e.hx]!=1)g++;
				if(vr[e.ry][e.rx]!=1)g++;
				vh[e.hy][e.hx]=1;
				vr[e.ry][e.rx]=1;
				q.push(e);
				if(b[0]==1)e.hx-=a[i];
				if(b[1]==1)e.rx+=a[i];
			}
		}
		e.tim--;
	}
	while(!q.empty())q.pop();
	if(e.tim>=100||g==(w*h-k)*2)printf("NA\n");
	else cout<<e.tim<<endl;
}
return 0;
}	

Compiler output:
a.cc: In function 'int main()':
a.cc:10:1: error: 'queue' was not declared in this scope
   10 | queue<Y> q;
      | ^~~~~
a.cc:2:1: note: 'std::queue' is defined in header '<queue>'; this is probably fixable by adding '#include <queue>'
    1 | #include<string.h>
  +++ |+#include <queue>
    2 | using namespace std;
a.cc:10:8: error: expected primary-expression before '>' token
   10 | queue<Y> q;
      |        ^
a.cc:10:10: error: 'q' was not declared in this scope
   10 | queue<Y> q;
      |          ^
a.cc:14:9: error: 'cin' was not declared in this scope
   14 |         cin>>w>>h;
      |         ^~~
a.cc:2:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    1 | #include<string.h>
  +++ |+#include <iostream>
    2 | using namespace std;
a.cc:88:37: error: 'printf' was not declared in this scope
   88 |         if(e.tim>=100||g==(w*h-k)*2)printf("NA\n");
      |                                     ^~~~~~
a.cc:2:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    1 | #include<string.h>
  +++ |+#include <cstdio>
    2 | using namespace std;
a.cc:89:14: error: 'cout' was not declared in this scope
   89 |         else cout<<e.tim<<endl;
      |              ^~~~
a.cc:89:14: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:89:27: error: 'endl' was not declared in this scope
   89 |         else cout<<e.tim<<endl;
      |                           ^~~~
a.cc:2:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    1 | #include<string.h>
  +++ |+#include <ostream>
    2 | using namespace std;