Go: package main

import (
	"fmt"
	"math"
)

type Position struct {
	X int64
	Y int64
}

func (one Position) Distance(another Position) int64 {
	return int64(math.Abs(float64(one.X-another.X)) + math.Abs(float64(one.Y-another.Y)))
}

type PersonPosition struct {
	Takayuki Position
	Kazuyuki Position
}

func (one PersonPosition) Distance() int64 {
	return one.Takayuki.Distance(one.Kazuyuki)
}

type Status struct {
	Person PersonPosition
	Turn   int64
}

type PQStatus []*Status

func (pq PQStatus) Len() int {
	return len(pq)
}
func (pq PQStatus) Less(i, j int) bool {
	if pq[i].Turn == pq[j].Turn {
		return pq[i].Person.Distance() < pq[j].Person.Distance()
	}
	return pq[i].Turn < pq[j].Turn
}

func (pq PQStatus) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PQStatus) Push(x interface{}) {
	item := x.(*Status)
	*pq = append(*pq, item)
}

func (pq *PQStatus) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

func solve(h, w int64) {
	var tx, ty, kx, ky int64
	fmt.Scan(&tx, &ty, &kx, &ky)
	tx--
	ty--
	kx--
	ky--
	m := make([][]int64, h)
	for i := range m {
		m[i] = make([]int64, w)
		for j := int64(0); j < w; j++ {
			fmt.Scan(&m[i][j])
		}
	}
	start := &Status{Person: PersonPosition{Takayuki: Position{X: tx, Y: ty}, Kazuyuki: Position{X: kx, Y: ky}}, Turn: 0}
	pq := make(PQStatus, 0)
	//heap.Init(&pq)
	//heap.Push(&pq, start)
	pq.Push(start)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]int64, 0)
	ans := int64(101)
	for pq.Len() > 0 {
		//now := heap.Pop(&pq).(*Status)
		now := pq.Pop().(*Status)
		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}
		if v, ok := memo[now.Person]; ok && v <= now.Turn {
			continue
		}
		memo[now.Person] = now.Turn
		for i := 0; i < 4; i++ {
			next := &Status{}
			*next = *now
			next.Person.Kazuyuki.X += dx[i]
			next.Person.Kazuyuki.Y += dy[i]
			if next.Person.Kazuyuki.X < 0 || next.Person.Kazuyuki.X >= w ||
				next.Person.Kazuyuki.Y < 0 || next.Person.Kazuyuki.Y >= h ||
				m[next.Person.Kazuyuki.Y][next.Person.Kazuyuki.X] == 1 {
				next.Person.Kazuyuki.X -= dx[i]
				next.Person.Kazuyuki.Y -= dy[i]
			}
			next.Person.Takayuki.X -= dx[i]
			next.Person.Takayuki.Y -= dy[i]
			if next.Person.Takayuki.X < 0 || next.Person.Takayuki.X >= w ||
				next.Person.Takayuki.Y < 0 || next.Person.Takayuki.Y >= h ||
				m[next.Person.Takayuki.Y][next.Person.Takayuki.X] == 1 {
				next.Person.Takayuki.X += dx[i]
				next.Person.Takayuki.Y += dy[i]
			}
			next.Turn += 1
			if next.Turn < 100 {
				//heap.Push(&pq, next)
				pq.Push(next)
			}
		}
	}
	if ans == 101 {
		fmt.Println("NA")
	} else {
		fmt.Println(ans)
	}
}

func main() {
	var h, w int64
	for {
		fmt.Scan(&h, &w)
		if h == 0 && w == 0 {
			break
		}
		solve(h, w)
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=1e9;
const int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int memo[50][50][50][50];
struct data{
    int tx,ty,kx,ky,cnt;
    data(int a,int b,int c,int d,int e)
        :tx(a),ty(b),kx(c),ky(d),cnt(e){}

    data(){}
};
int main(){
    int w,h,tx,ty,kx,ky;
    while(cin>>w>>h,w||h){
        cin>>tx>>ty>>kx>>ky;
        tx--;ty--;kx--;ky--;
        int field[50][50];
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                cin>>field[i][j];

        fill(memo[0][0][0],memo[0][0][0]+50*50*50*50,INF);

        queue<data>Q;
        Q.push(data(tx,ty,kx,ky,0));
        while(!Q.empty()){
            data d=Q.front();Q.pop();
            if(d.tx<0||d.tx>=w||d.ty<0||d.ty>=h)continue;
            if(d.kx<0||d.kx>=w||d.ky<0||d.ky>=h)continue;
            if(memo[d.tx][d.ty][d.kx][d.ky]<d.cnt)continue;
            memo[d.tx][d.ty][d.kx][d.ky]=d.cnt;
            //cout<<d.tx<<" "<<d.ty<<" "<<d.kx<<" "<<d.ky<<" "<<d.cnt<<endl;
            for(int i=0;i<4;i++){
                int ax=d.tx+dx[i],ay=d.ty+dy[i],bx=d.kx+dx[3-i],by=d.ky+dy[3-i];
                if(ax<0||ax>=w||ay<0||ay>=h||field[ay][ax]){
                    ax-=dx[i];ay-=dy[i];
                }
                if(bx<0||bx>=w||by<0||by>=h||field[by][bx]){
                    bx-=dx[3-i];by-=dy[3-i];
                }
                Q.push(data(ax,ay,bx,by,d.cnt+1));
            }
        }
        int Min=INF;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                Min=min(Min,memo[j][i][j][i]);
            }
        }
        if(Min<100)cout<<Min<<endl;
        else cout<<"NA"<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;
const int INF = 99999999;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<PP, int> PPP;
vector< vector<char> > Field;
bool used1[51][51];
bool used2[51][51];
int dx1[] = {-1,  0,  0,  1};
int dy1[] = { 0, -1,  1,  0};
int dx2[] = { 1,  0,  0, -1};
int dy2[] = { 0,  1, -1,  0};

void solve()
{
	int x, y;
	while(cin >> x >> y, x || y)
	{
		memset(used1, 0, sizeof(used1));
		memset(used2, 0, sizeof(used2));
		P t, k;
		cin >> t.first >> t.second >> k.first >> k.second;
		Field.resize(y + 2);
		for(int i = 0; i < y + 2; ++i)
		{
			Field[i].resize(x + 2);
			for(int j = 0; j < x + 2; ++j)
			{
				Field[i][j] = '1';
			}
		}
		for(int i = 1; i <= y; ++i)
		{
			for(int j = 1; j <= x; ++j)
			{
				cin >> Field[i][j];
			}
		}
		queue<PPP> Que;
		PPP ppp;
		ppp.first.first = t;
		ppp.first.second = k;
		ppp.second = 0;
		int count = 0;
		Que.push(ppp);
		while(!Que.empty())
		{
			PPP front = Que.front();
			Que.pop();
			if(front.second >= 100)
			{
				break;
			}
			if(front.first.first.first == front.first.second.first && front.first.first.second == front.first.second.second)
			{
				cout << front.second << endl;
				goto NEXT;
			}
			used1[front.first.first.second][front.first.first.first] = true;
			used2[front.first.second.second][front.first.second.first] = true;
			for(int i = 0; i < 4; ++i)
			{
				int nx1 = front.first.first.first + dx1[i];
				int ny1 = front.first.first.second + dy1[i];
				int nx2 = front.first.second.first + dx2[i];
				int ny2 = front.first.second.second + dy2[i];
				if(Field[ny1][nx1] == '0' && Field[ny2][nx2] == '0' && !used1[ny1][nx1] && !used2[ny2][nx2])
				{
					PPP next;
					next.first.first.first = nx1;
					next.first.first.second = ny1;
					next.first.second.first = nx2;
					next.first.second.second = ny2;
					next.second = front.second + 1;
					Que.push(next);
				}
				else if(Field[ny1][nx1] == '1' && Field[ny2][nx2] == '0' && !used2[ny2][nx2])
				{
					PPP next;
					next.first.first.first = front.first.first.first;
					next.first.first.second = front.first.first.second;
					next.first.second.first = nx2;
					next.first.second.second = ny2;
					next.second = front.second + 1;
					Que.push(next);
				}
				else if(Field[ny1][nx1] == '0' && Field[ny2][nx2] == '1' && !used1[ny1][nx1])
				{
					PPP next;
					next.first.first.first = nx1;
					next.first.first.second = ny1;
					next.first.second.first = front.first.second.first;
					next.first.second.second = front.first.second.second;
					next.second = front.second + 1;
					Que.push(next);
				}
			}
		}
		cout << "NA" << endl;
NEXT:
		;
	}
}

int main()
{
	solve();
	return(0);
}