52分,向大佬求助
查看原帖
52分,向大佬求助
837059
shadowworrier楼主2023/3/19 20:24
#include <bits/stdc++.h>
using namespace std; 

int b[350][350]={0},n,m;
char a[350][350];
struct cow
{
	int x,y,time;
};
void transport(int &nx,int &ny,char word)   //如果奶牛走到了传送门上,进行传送 
{
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(a[i][j]==word&&i!=nx&&j!=ny){
				nx=i; ny=j;
				return;
			}
		}
	}
}
int main()
{
	int fx[4]={0,0,1,-1},fy[4]={1,-1,0,0};    //4个方向 
	int i,j,nx,ny,k;
	string s;
	cow node,first;
	cin>>n>>m;
	for(i=0;i<n;i++){
		cin>>s;
		for(j=0;j<m;j++){
			a[i][j]=s[j];
			if(a[i][j]=='@'){
				node.x=i; node.y=j;
			}
		}
	}
	node.time=0;
	queue<cow> Q;
	Q.push(node);
	while(!Q.empty()){                        //标准bfs 
		first=Q.front();
		Q.pop();
		if(a[first.x][first.y]=='='){
			k=first.time;
			cout<<k;
			return 0;
		}
		if(a[first.x][first.y]>='A'&&a[first.x][first.y]<='Z'){
			transport(first.x,first.y,a[first.x][first.y]);          //传送 
		}
		for(i=0;i<4;i++){
			nx=first.x+fx[i]; ny=first.y+fy[i];
			if(a[nx][ny]!='#'&&nx<n&&nx>=0&&ny<m&&ny>=0&&b[nx][ny]!=1){
				node.x=nx; node.y=ny;
				node.time=first.time+1;
				b[nx][ny]=1;
				Q.push(node);
			}
		}
	}
}
2023/3/19 20:24
加载中...