Language: C++

#include <iostream>
#include <queue>

using namespace std;

class Status{
public:
  int wx, wy, cx, cy;
  int cost;
};

int w, h;
int tx, ty;
int gx, gy;
char data[7][7];
bool vis[7][7];
int dx[]={0, 1, 0, -1};
int dy[]={-1, 0, 1, 0};
int rdx[]={0, -1, 0, 1};
int rdy[]={1, 0, -1, 0};
int bfs(Status);
bool dfs(int, int);
main(){
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    Status tmp;
    tmp.cost=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> data[i][j];
	if(data[i][j]=='2'){
	  data[i][j]='0';
	  tmp.cx=j;
	  tmp.cy=i;
	}
	if(data[i][j]=='4'){
	  data[i][j]='0';
	  tmp.wx=j;
	  tmp.wy=i;
	}
	if(data[i][j]=='3'){
	  gx=j;
	  gy=i;
	  data[i][j]='0';
	}
      }
    }

    cout << bfs(tmp) << endl;
  }
  return 0;
}

int bfs(Status in){
  Status u, v;
  queue<Status> Q;
  Q.push(in);
  bool visited[7][7][7][7];
  for(int a=0;a<h;a++){
    for(int b=0;b<w;b++){
      for(int c=0;c<h;c++){
	for(int d=0;d<w;d++){
	  visited[a][b][c][d]=false;
	}
      }
    }
  }
  visited[in.cy][in.cx][in.wy][in.wx]=true;
  while(!Q.empty()){
    u=Q.front(), Q.pop();
    if(u.cx==gx && u.cy==gy) return u.cost;
    for(int i=0;i<4;i++){
      ty=u.cy+dy[i];
      tx=u.cx+dx[i];
      if(ty>=0 && ty<h){
	if(tx>=0 && tx<w){
	  if(data[ty][tx]=='0'){
	    int rtx=u.cx+rdx[i], rty=u.cy+rdy[i];
	    if(rty>=0 && rty<h){
	      if(rtx>=0 && rtx<w){
		if(data[rty][rtx]=='0' && !visited[rty][rtx][u.cy][u.cx]){
		  for(int j=0;j<h;j++){
		    for(int k=0;k<w;k++){
		      vis[j][k]=false;
		    }
		  }
		  data[u.cy][u.cx]='1';
		  if(dfs(u.wy, u.wx)){
		    v.wy=u.cy;
		    v.wx=u.cx;
		    v.cy=rty;
		    v.cx=rtx;
		    v.cost=u.cost+1;
		    visited[v.cy][v.cx][v.wy][v.wx]=true;
		    Q.push(v);
		  }
		  data[u.cy][u.cx]='0';
		}
	      }
	    }
	  }
	}
      }
    }
  }
  return -1;
}

bool dfs(int y, int x){
  bool ans=false;
  if(y==ty && x==tx) return true;
  vis[y][x]=true;
  for(int i=0;i<4;i++){
    int ttx=x+dx[i];
    int tty=y+dy[i];
    if(tty>=0 && tty<h){
      if(ttx>=0 && ttx<w){
	if(!vis[tty][ttx] && data[tty][ttx]=='0'){
	  if(dfs(tty, ttx)) ans=true;;
	}
      }
    }
  }
  return ans;
}
#include <iostream>
#include <queue>

using namespace std;

class Status{
public:
  int wx, wy, cx, cy;
  int cost;
};

int w, h;
int tx, ty;
int gx, gy;
char data[7][7];
bool vis[7][7];
int dx[]={0, 1, 0, -1};
int dy[]={-1, 0, 1, 0};
int rdx[]={0, -1, 0, 1};
int rdy[]={1, 0, -1, 0};
int bfs(Status);
bool dfs(int, int);
main(){
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    Status tmp;
    tmp.cost=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> data[i][j];
	if(data[i][j]=='2'){
	  data[i][j]='0';
	  tmp.cx=j;
	  tmp.cy=i;
	}
	if(data[i][j]=='4'){
	  data[i][j]='0';
	  tmp.wx=j;
	  tmp.wy=i;
	}
	if(data[i][j]=='3'){
	  gx=j;
	  gy=i;
	  data[i][j]='0';
	}
      }
    }

    cout << bfs(tmp) << endl;
  }
  return 0;
}

int bfs(Status in){
  Status u, v;
  queue<Status> Q;
  Q.push(in);
  bool visited[7][7][7][7];
  for(int a=0;a<h;a++){
    for(int b=0;b<w;b++){
      for(int c=0;c<h;c++){
	for(int d=0;d<w;d++){
	  visited[a][b][c][d]=false;
	}
      }
    }
  }
  visited[in.cy][in.cx][in.wy][in.wx]=true;
  while(!Q.empty()){
    u=Q.front(), Q.pop();
    if(u.cx==gx && u.cy==gy) return u.cost;
    for(int i=0;i<4;i++){
      ty=u.cy+dy[i];
      tx=u.cx+dx[i];
      if(ty>=0 && ty<h){
	if(tx>=0 && tx<w){
	  if(data[ty][tx]=='0'){
	    int rtx=u.cx+rdx[i], rty=u.cy+rdy[i];
	    if(rty>=0 && rty<h){
	      if(rtx>=0 && rtx<w){
		if(data[rty][rtx]=='0' && !visited[rty][rtx][u.cy][u.cx]){
		  for(int j=0;j<h;j++){
		    for(int k=0;k<w;k++){
		      vis[j][k]=false;
		    }
		  }
		  data[u.cy][u.cx]='1';
		  if(dfs(u.wy, u.wx)){
		    v.wy=u.cy;
		    v.wx=u.cx;
		    v.cy=rty;
		    v.cx=rtx;
		    v.cost=u.cost+1;
		    visited[v.cy][v.cx][v.wy][v.wx]=true;
		    Q.push(v);
		  }
		  data[u.cy][u.cx]='0';
		}
	      }
	    }
	  }
	}
      }
    }
  }
  return -1;
}

bool dfs(int y, int x){
  bool ans=false;
  if(y==ty && x==tx) return true;
  vis[y][x]=true;
  for(int i=0;i<4;i++){
    int ttx=x+dx[i];
    int tty=y+dy[i];
    if(tty>=0 && tty<h){
      if(ttx>=0 && ttx<w){
	if(!vis[tty][ttx] && data[tty][ttx]=='0'){
	  if(dfs(tty, ttx)) ans=true;;
	}
      }
    }
  }
  return ans;
}

Compiler output:
a.cc:23:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   23 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:31:16: error: reference to 'data' is ambiguous
   31 |         cin >> data[i][j];
      |                ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:32:12: error: reference to 'data' is ambiguous
   32 |         if(data[i][j]=='2'){
      |            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:33:11: error: reference to 'data' is ambiguous
   33 |           data[i][j]='0';
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:37:12: error: reference to 'data' is ambiguous
   37 |         if(data[i][j]=='4'){
      |            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:38:11: error: reference to 'data' is ambiguous
   38 |           data[i][j]='0';
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:42:12: error: reference to 'data' is ambiguous
   42 |         if(data[i][j]=='3'){
      |            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:45:11: error: reference to 'data' is ambiguous
   45 |           data[i][j]='0';
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc: In function 'int bfs(Status)':
a.cc:78:14: error: reference to 'data' is ambiguous
   78 |           if(data[ty][tx]=='0'){
      |              ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:15:6: note:                 'char data [7][7]'
   15 | char data[7][7];
      |      ^~~~
a.cc:82:20: error: reference to 'data' is ambiguous
   82 |                 if(data[rty][rtx]=='0' && !visited[rty][rtx][u.cy][u.cx]){
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&_