Language: C++

#include <iostream>
#include <queue>
#include <set>
#define N 50
using namespace std;

void bfs();

typedef struct dat{
  int y1,x1,y2,x2;
} d;

int w,h,sy1,sx1,sy2,sx2,gy1,gx1,gy2,gx2,ans;
string r1[N],r2[N];
set<d> visited;

int main(){
  while(1){
    cin>>w>>h;
    if(!w&&!h) break;
    for(int i=0;i<h;i++) cin>>r1[i]>>r2[i];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
	if(r1[i][j]=='L') sy1=i,sx1=j;
	if(r1[i][j]=='%') gy1=i,gx1=j;
	if(r2[i][j]=='R') sy2=i,sx2=j;
	if(r2[i][j]=='%') gy2=i,gx2=j;
      }
    ans=0;
    bfs();
    if(ans) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
  }
  return 0;
}

void bfs(){
  int dy[4]={0,-1,0,1},dx[4]={-1,0,1,0};
  queue<d> q;
  d k=(d){sy1,sx1,sy2,sx2};
  q.push(k);
  visited.insert(k);
  while(!q.empty()){
    d t=q.front(); q.pop();
    for(int i=0;i<4;i++){
      int ny1=t.y1+dy[i],nx1=t.x1+dx[i];
      int ny2=t.y2-dy[i],nx2=t.y2-dx[i];
      if(ny1<0) ny1=0;
      if(nx1<0) nx1=0;
      if(h<=ny1) ny1=h;
      if(w<=nx1) nx1=w;
      if(ny2<0) ny2=0;
      if(nx2<0) nx2=0;
      if(h<=ny2) ny2=h;
      if(w<=nx2) nx2=w;
      if(r1[ny1][nx1]=='#') ny1=t.y1,nx1=t.x1;
      if(r2[ny2][nx2]=='#') ny2=t.y2,nx2=t.x2;
      d u=(d){ny1,nx1,ny2,nx2};
      if(visited.find(u)!=visited.end()) continue;
      visited.insert(u);
      if(ny1==gy1&&nx1==gx1&&ny2==gy2&&nx2==gx2){
	ans=1;
	break;
      }
    }
    if(ans) break;
  }
}

Compiler output:
In file included from /usr/include/c++/14/string:49,
                 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/stl_function.h: In instantiation of 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = dat]':
/usr/include/c++/14/bits/stl_tree.h:2532:33:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = dat; _Val = dat; _KeyOfValue = std::_Identity<dat>; _Compare = std::less<dat>; _Alloc = std::allocator<dat>; iterator = std::_Rb_tree<dat, dat, std::_Identity<dat>, std::less<dat>, std::allocator<dat> >::iterator]'
 2532 |               || _M_impl._M_key_compare(__k,
      |                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~
 2533 |                                         _S_key(__j._M_node))) ? end() : __j;
      |                                         ~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:797:25:   required from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = dat; _Compare = std::less<dat>; _Alloc = std::allocator<dat>; iterator = std::_Rb_tree<dat, dat, std::_Identity<dat>, std::less<dat>, std::allocator<dat> >::const_iterator; key_type = dat]'
  797 |       { return _M_t.find(__x); }
      |                ~~~~~~~~~^~~~~
a.cc:59:22:   required from here
   59 |       if(visited.find(u)!=visited.end()) continue;
      |          ~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_function.h:405:20: error: no match for 'operator<' (operand types are 'const dat' and 'const dat')
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/string:48:
/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 dat' 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 dat' 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 dat' 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 dat' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~