#include<bits/stdc++.h>
#include<cctype>
using namespace std;
const int maxn=305;
int n,m;
int sx,sy,sxx,syy;
char c[maxn][maxn];
int a[maxn][maxn];
bool vis[maxn][maxn];
struct node{
int x,y;
bool operator< (const node& p) const
{
return p.x<x;
}
};
map<char,node> bd;
map<node,node> mp;
void bfs()
{
queue<node> q;
q.push(node{sx,sy});
a[sx][sy]=0;
vis[sx][sy]=1;
while(!q.empty())
{
node tp=q.front();
int x=tp.x,y=tp.y;
cout<<x<<' '<<y<<" "<<a[x][y]<<endl;
q.pop();
if(x==sxx&&y==syy)
{
return ;
}
if(y+1<=m&&!vis[x][y+1]&&c[x][y+1]!='#')
{
int x1=x,y1=y+1;
q.push(node{x1,y1});
a[x1][y1]=a[x][y]+1;
vis[x1][y1]=1;
if(isupper(c[x1][y1]))
{
q.push(node{mp[node{x1,y1}].x,mp[node{x1,y1}].y});
a[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=a[x][y]+1;
vis[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=1;
}
}
if(x+1<=n&&!vis[x+1][y]&&c[x+1][y]!='#')
{
int x1=x+1,y1=y;
q.push(node{x1,y1});
a[x1][y1]=a[x][y]+1;
vis[x1][y1]=1;
if(isupper(c[x1][y1]))
{
q.push(node{mp[node{x1,y1}].x,mp[node{x1,y1}].y});
a[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=a[x][y]+1;
vis[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=1;
}
}
if(y-1>=1&&!vis[x][y-1]&&c[x][y-1]!='#')
{
int x1=x,y1=y-1;
q.push(node{x1,y1});
a[x1][y1]=a[x][y]+1;
vis[x1][y1]=1;
if(isupper(c[x1][y1]))
{
q.push(node{mp[node{x1,y1}].x,mp[node{x1,y1}].y});
a[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=a[x][y]+1;
vis[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=1;
}
}
if(x-1>=1&&!vis[x-1][y]&&c[x-1][y]!='#')
{
int x1=x-1,y1=y;
q.push(node{x1,y1});
a[x1][y1]=a[x][y]+1;
vis[x1][y1]=1;
if(isupper(c[x1][y1]))
{
q.push(node{mp[node{x1,y1}].x,mp[node{x1,y1}].y});
a[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=a[x][y]+1;
vis[mp[node{x1,y1}].x][mp[node{x1,y1}].y]=1;
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>c[i][j];
if(c[i][j]=='.')continue;
if(c[i][j]=='@')
{
sx=i;
sy=j;
}
else if(c[i][j]=='=')
{
sxx=i;
syy=j;
}
else if(c[i][j]!='#')
{
if(bd[c[i][j]].x==0&&bd[c[i][j]].y==0)
{
bd[c[i][j]]=node{i,j};
// cout<<c[i][j]<<" "<<i<<" "<<j<<endl;
}
else
{
mp[bd[c[i][j]]]=node{i,j};
mp[node{i,j}]=bd[c[i][j]];
cout<<bd[c[i][j]].x<<" "<<bd[c[i][j]].y<<" "<<i<<" "<<j<<endl;
}
}
}
bfs();
cout<<a[sxx][syy];
return 0;
}fffff
文章信息与版本
文章信息
- 收录
- 2025年12月12日
- 更新
- 2025年12月12日
- 篇幅
- 约 5 分钟
版本
仅有当前版本