大神求调
查看原帖
大神求调
1026187
lys_99楼主2024/6/29 18:17
#include<bits/stdc++.h>
using namespace std;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
struct node{
    int x,y,step;
};
char a[509][509];
bool vis[509][509];
int n,m,x1,x2,y11,y2;
deque<node> q;
int main(){
	while(cin>>n>>m&&n!=0&&m!=0){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>a[i][j]; 
			}
		}
		for(int i=1;i<=501;i++){
			for(int j=1;j<=501;j++){
				vis[i][j]=0;
			}
		}
		while(!q.empty()) q.pop_front();
		cin>>x1>>x2>>y11>>y2;
		node t;
		t.x=x1;
		t.y=x2;
		t.step=0;
		vis[x1][x2]=1;
		q.push_back(t);
		while(!q.empty()){
			node now=q.front();
			q.pop_front();
			//cout<<"#"<<" "<<now.x<<" "<<now.y<<endl;
			if(now.x==y11&&now.y==y2){
				cout<<now.step<<endl;
				break;
			}
			for(int i=0;i<=3;i++){
				node New=now;
				New.x=now.x+dx[i];
				New.y=now.y+dy[i];
				if(New.x<n&&New.y<m&&New.x>1&&New.y>1&&vis[New.x][New.y]==0){
					vis[New.x][New.y]=1;
					if(a[New.x][New.y]==a[now.x][now.y]) q.push_front(New);
					else q.push_back(New);
				}
			}
		}
	}
	return 0;
}
2024/6/29 18:17
加载中...