Language: C++

#include<queue>
#include<set>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
struct S{
	char p,x,y;
	vector<pair<char,char> > w,c;
	S(char p=0,char x=0,char y=0):p(p),x(x),y(y){
	}
};
int main(){
	int i,j,k,l;
	int h,w;
	while(scanf("%d%d",&h,&w),h||w){
		int a[10][10]={},x,y;
		S s;
		for(i=0;i<h;++i){
			getchar();
			for(j=0;j<w;++j){
				char p=getchar();
				if(false){
				}else if(p=='#'){
					a[j][i]=-1;
				}else if(p=='@'){
					s.x=j;
					s.y=i;
				}else if(p=='w'){
					s.w.push_back(make_pair(j,i));
				}else if(p=='c'){
					s.c.push_back(make_pair(j,i));
				}else if(p=='E'){
					x=j;
					y=i;
				}
			}
		}
		set<S> e;
		e.insert(s);
		queue<S> b;
		b.push(s);
		int dx[]={1,0,-1,0},dy[]={0,1,0,-1},mn=0x7fff;
		while(!b.empty()){
			S s=b.front();
			b.pop();
			for(i=0;i<(int)s.w.size();++i)
				a[s.w[i].first][s.w[i].second]=-2;
			for(i=0;i<(int)s.c.size();++i)
				a[s.c[i].first][s.c[i].second]=-1;
			int c[10][10];
			memset(c,-1,sizeof(c));
			c[s.x][s.y]=s.p;
			queue<S> d;
			d.push(S(s.p,s.x,s.y));
			while(!d.empty()){
				S t=d.front();
				d.pop();
				for(i=0;i<4;++i){
					if(a[t.x+dx[i]][t.y+dy[i]]||c[t.x+dx[i]][t.y+dy[i]]>=0)
						continue;
					c[t.x+dx[i]][t.y+dy[i]]=t.p+1;
					d.push(S(t.p+1,t.x+dx[i],t.y+dy[i]));
				}
			}
			if(c[x][y]>=0&&mn>c[x][y])
				mn=c[x][y];
			for(i=0;i<(int)s.c.size();++i){
				for(j=0;j<4;++j){
					if(c[s.c[i].first+dx[(j+2)%4]][s.c[i].second+dy[(j+2)%4]]<0||a[s.c[i].first+dx[j]][s.c[i].second+dy[j]]==-1)
						continue;
					for(k=1;!a[s.c[i].first+dx[j]*k][s.c[i].second+dy[j]*k];++k);
					S t(c[s.c[i].first+dx[(j+2)%4]][s.c[i].second+dy[(j+2)%4]]+1,s.c[i].first,s.c[i].second);
					t.w=s.w;
					t.c=s.c;
					if(a[s.c[i].first+dx[j]*k][s.c[i].second+dy[j]*k]==-1){
						t.c[i]=make_pair(s.c[i].first+dx[j]*(k-1),s.c[i].second+dy[j]*(k-1));
					}else{
						for(l=0;s.w[l].first!=s.c[i].first+dx[j]*k||s.w[l].second!=s.c[i].second+dy[j]*k;++l);
						t.w.erase(t.w.begin()+l);
						t.c.erase(t.c.begin()+i);
					}
					S u=t;
					u.p=0;
					if(e.count(u))
						continue;
					e.insert(u);
					b.push(t);
				}
			}
			for(i=0;i<(int)s.w.size();++i)
				a[s.w[i].first][s.w[i].second]=0;
			for(i=0;i<(int)s.c.size();++i)
				a[s.c[i].first][s.c[i].second]=0;
		}
		printf("%d\n",mn==0x7fff?-1:mn);
	}
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/bits/refwrap.h:39,
                 from /usr/include/c++/14/deque:67,
                 from /usr/include/c++/14/queue:62,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h: In instantiation of 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = S]':
/usr/include/c++/14/bits/stl_tree.h:2545:33:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) const [with _Key = S; _Val = S; _KeyOfValue = std::_Identity<S>; _Compare = std::less<S>; _Alloc = std::allocator<S>; const_iterator = std::_Rb_tree<S, S, std::_Identity<S>, std::less<S>, std::allocator<S> >::const_iterator]'
 2545 |               || _M_impl._M_key_compare(__k,
      |                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~
 2546 |                                         _S_key(__j._M_node))) ? end() : __j;
      |                                         ~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:751:25:   required from 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::count(const key_type&) const [with _Key = S; _Compare = std::less<S>; _Alloc = std::allocator<S>; size_type = long unsigned int; key_type = S]'
  751 |       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
      |                ~~~~~~~~~^~~~~
a.cc:85:16:   required from here
   85 |                                         if(e.count(u))
      |                                            ~~~~~~~^~~
/usr/include/c++/14/bits/stl_function.h:405:20: error: no match for 'operator<' (operand types are 'const S' and 'const S')
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/deque:62:
/usr/include/c++/14/bits/stl_pair.h:1045:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1045 |     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1045:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const S' is not derived from 'const std::pair<_T1, _T2>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:448:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  448 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:448:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const S' is not derived from 'const std::reverse_iterator<_Iterator>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  493 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const S' is not derived from 'const std::reverse_iterator<_Iterator>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1694 |     operator<(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const S' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1760:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1760 |     operator<(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1760:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const S' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/deque:66:
/usr/include/c++/14/bits/stl_deque.h:2334:5: note: candidate: 'template<class _Tp, class _Alloc> bool std::operator<(const deque<_Tp, _Alloc>&, const deque<_Tp, _Alloc>&)'
 2334 |     operator<(const deque<_Tp, _Alloc>& __x, const deque<_Tp, _Alloc>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_deque.h:2334:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const S' is not derived from 'const std::deque<_Tp, _Alloc>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~