《花花绿绿》
查看原帖
《花花绿绿》
474789
yczhou楼主2022/8/31 20:29

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using PLL=pair<LL,LL>;
LL n,m;
const int N=2e2+10;
char a[N][N];
bool vis[N][N];
LL dis[N][N];
LL dx[]={0,-1,0,1, 0};
LL dy[]={0, 0,1,0,-1};
void cs(LL &nx,LL &ny){
    for(LL i=1;i<=n;i++){
        for(LL j=1;j<=m;j++){
            if(a[i][j]==a[nx][ny]&&(i!=nx||j!=ny)){
                nx=i;
                ny=j;
                return ;
            }
        }
    }
}
void bfs(LL x,LL y){
	queue<PLL> q;
	q.push({x,y});
	vis[x][y]=1;
	dis[x][y]=0;
	while (q.size()){
		PLL t=q.front();
		q.pop();
		for (LL i=1; i<=4; i++){
			LL nx=t.first+dx[i];
			LL ny=t.second+dy[i];
			if (1<=nx && nx<=n && 1<=ny && ny<=m && 
			vis[nx][ny]==false){
				if (a[nx][ny]=='.'){
					vis[nx][ny]=1;
					dis[nx][ny]=dis[t.first][t.second]+1;
					q.push({nx,ny});
				}else if(a[nx][ny]>='A' && a[nx][ny]<='Z'){
					cs(nx,ny);
					vis[nx][ny]=1;
					dis[nx][ny]=dis[t.first][t.second]+1;
					q.push({nx,ny});
				}else if (a[nx][ny]=='@'){
					vis[nx][ny]=1;
					dis[nx][ny]=dis[t.first][t.second]+1;
				}
			} 
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	LL bx,by,jx,jy;
	for (LL i=1; i<=n; i++){
		for (LL j=1; j<=m; j++){
			cin>>a[i][j];
			if (a[i][j]=='='){
				bx=i;
				by=j;
			}else if(a[i][j]=='@'){
				jx=i;
				jy=j;
			}
		}
	}
	bfs(bx,by);
	cout<<dis[jx][jy]<<"\n";
	return 0;
} 

为啥啊?

急,在线等

2022/8/31 20:29
加载中...