58求助大佬
查看原帖
58求助大佬
753833
Mmatt楼主2022/8/4 16:27
//1825广搜 
#include<iostream>
#include<queue>
using namespace std;
struct s{
	int x,y,t;
};
queue<s>node;
int n,m,px,py,nx,ny,k,ex,ey;
char a[310][310];
bool bo[310][310];
int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
void chuan(int &xx,int &yy,int k)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]==a[xx][yy]&&(i!=xx||j!=yy)){
            	xx=i;
                yy=j;
                bo[xx][yy]=1;
                return;
			}
                
            }
        }
    }
    
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
			if(a[i][j]=='@')
			{
			px=i;py=j;
			}
			if(a[i][j]=='=')
			{
			ex=i;ey=j;
			}
			if(a[i][j]=='#')bo[i][j]=1;	
		}
	}
	node.push(s{px,py,0});

	while(!node.empty())
    {
        s f=node.front();
        node.pop();
        if(f.x==ex&&f.y==ey)
        {
            cout<<f.t;
            return 0;
        }
        if(a[f.x][f.y]>='A'&&a[f.x][f.y]<='Z')
        {
        
        chuan(f.x,f.y,f.t);bo[f.x][f.y]=1;
        }
        for(int i=0;i<4;i++)
        {
            nx=f.x+dx[i];
            ny=f.y+dy[i];
            if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!='#'&&!bo[nx][ny])
            {
                bo[nx][ny]=true;
                node.push(s{nx,ny,f.t+1});
            }
        
		}
        
    }
		return 0;
}
2022/8/4 16:27
加载中...