here WA on #10,#11,#13,#15
#include <bits/stdc++.h>
using namespace std;
struct cor
{
int x,y,step;
};
int n,m;
int sx,sy,ex,ey;
queue<cor> q;
int tele[305][305][2];
int mp[305][305];
int tmp[35][2];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
bool flag;
bool check(int x,int y)
{
if(x<1||x>n||y<1||y>m||mp[x][y]==1)
return false;
return true;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
cin.get();
for(int j=1;j<=m;++j)
{
char c;
cin>>c;
if(c=='#')
mp[i][j]=1;
else
mp[i][j]=0;
if(c=='@')
{
sx=i;
sy=j;
}
else if(c=='=')
{
ex=i;
ey=j;
}
else if(c!='#'&&c!='@'&&c!='#'&&c!='.')
{
if(!tmp[c-'A'][0]&&!tmp[c-'A'][1])
{
tmp[c-'A'][0]=i;
tmp[c-'A'][1]=j;
}
else
{
int u=tmp[c-'A'][0],v=tmp[c-'A'][1];
tele[u][v][0]=i;
tele[u][v][1]=j;
tele[i][j][0]=u;
tele[i][j][1]=v;
mp[u][v]=-1;
mp[i][j]=-1;
}
}
}
}
q.push({sx,sy,0});
++mp[sx][sy];
while(!q.empty())
{
cor t=q.front();
q.pop();
int x,y;
x=t.x;
y=t.y;
if(tele[x][y][0]&&tele[x][y][1]&&check(tele[x][y][0],tele[x][y][1])&&!flag)
{
if(check(tele[x][y][0],tele[x][y][1])&&!flag)
{
flag=true;
q.push({tele[x][y][0],tele[x][y][1],t.step});
++mp[tele[x][y][0]][tele[x][y][1]];
if(tele[x][y][0]==ex&&tele[x][y][1]==ey)
{
cout<<t.step;
break;
}
}
/*else
{
flag=false;
for(int i=0;i<4;++i)
{
if(check(x+dx[i],y+dy[i]))
{
q.push({x+dx[i],y+dy[i],t.step+1});
++mp[x+dx[i]][y+dy[i]];
if(x+dx[i]==ex&&y+dy[i]==ey)
{
cout<<t.step+1;
break;
}
}
}
}*/
}
else
{
flag=false;
for(int i=0;i<4;++i)
{
if(check(x+dx[i],y+dy[i]))
{
q.push({x+dx[i],y+dy[i],t.step+1});
++mp[x+dx[i]][y+dy[i]];
if(x+dx[i]==ex&&y+dy[i]==ey)
{
cout<<t.step+1;
return 0;
}
}
}
}
}
return 0;
}