13分。。。
查看原帖
13分。。。
538683
Enoch006楼主2023/1/21 17:14

我调一上午都没调出来,求大佬们康康,悬关

#include <bits/stdc++.h>
using namespace std;
int b[3000][3000],n,m,sum,k,x,y,ax,ay; 
char a[3000][3000];
struct kkk{
    int x,y,k;
};
queue<kkk> s;
void gogogo_weishihao(int &x,int &y){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]==a[x][y]&&x!=i&&y!=j){
				x=i;
				y=j;
				return;
			}
		}
	}
}
int bfs(){
    while(!s.empty()){
    	if(a[s.front().x][s.front().y]=='='){
    		//cout<<s.front().x<<" "<<s.front().y<<" "<<s.front().k;
    		cout<<s.front().k;
    		return 0;
		}
        if(a[s.front().x][s.front().y]>='A'&&a[s.front().x][s.front().y]<='Z'&&b[s.front().x][s.front().y]==0){
        	int xx=s.front().x,yy=s.front().y;
			gogogo_weishihao(s.front().x,s.front().y);
        	s.push((kkk){s.front().x,s.front().y,s.front().k+1});
            b[xx][yy]=1;
        	s.pop();
        	continue;
		}
        if(a[s.front().x-1][s.front().y]!='#'&&s.front().x-1<=n&&s.front().x-1>=1&&b[s.front().x-1][s.front().y]==0){
            s.push((kkk){s.front().x-1,s.front().y,s.front().k+1});
            b[s.front().x-1][s.front().y]=1;
        }
        if(a[s.front().x][s.front().y-1]!='#'&&s.front().y-1<=m&&s.front().y-1>=1&&b[s.front().x][s.front().y-1]==0){
            s.push((kkk){s.front().x,s.front().y-1,s.front().k+1});
            b[s.front().x][s.front().y-1]=1;
        }
        if(a[s.front().x+1][s.front().y]!='#'&&s.front().x+1<=n&&s.front().x+1>=1&&b[s.front().x+1][s.front().y]==0){
            s.push((kkk){s.front().x+1,s.front().y,s.front().k+1});
            b[s.front().x+1][s.front().y]=1;
        }
        if(a[s.front().x][s.front().y+1]!='#'&&s.front().y+1<=m&&s.front().y+1>=1&&b[s.front().x][s.front().y+1]==0){
            s.push((kkk){s.front().x,s.front().y+1,s.front().k+1});
            b[s.front().x][s.front().y+1]=1;
        }
        s.pop(); 
    }
}

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]=='@'){
    			s.push((kkk){i,j,0});
			}
		}
    }
    bfs();
    return 0;
}
2023/1/21 17:14
加载中...