求大佬帮忙改改代码
查看原帖
求大佬帮忙改改代码
181715
gjh303987897楼主2022/3/3 00:04
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 201;
const int jump_x[5]={0,1,-1,0,0};
const int jump_y[5]={0,0,0,1,-1};
int vis[maxn][maxn];
struct data{
	int nx,ny;
	int lx,ly;
}t[10000001];
int l,r,R,c,ans_x[100001],ans_y[100001];
char map[maxn][maxn];
void bfs(int x,int y){
	l=r=1; vis[x][y]=1;
	t[l].nx=x; t[l].ny=y;
	while(l<=r){
		for(int i=1;i<=4;i++){
			int xx=t[l].nx+jump_x[i]; 
			int yy=t[l].ny+jump_y[i]; 
			//cout<<xx<<"-"<<yy<<" ";
			if(xx>0&&yy>0&&xx<=R&&yy<c&&vis[xx][yy]!=1&&map[xx][yy]=='.'){
				t[xx].lx=t[l].nx; t[yy].ly=t[l].ny; vis[xx][yy]=1; //cout<<t[xx].lx<<"-"<<xx<<" ";
				t[++r].nx=xx; t[r].ny=yy;  
				if(xx==R&&yy==c){
					return ;
				}
			}
		}
		l++;
	}
}
int main(){
	cin>>R>>c;
	for(int i=1;i<=R;i++){
		for(int j=1;j<=c;j++){
			cin>>map[i][j];
		}
	}
	bfs(1,1);
	int len=1; ans_x[len]=R; ans_y[len]=c;
	while(t[R].lx!=0||t[c].ly!=0){
		ans_x[++len]=t[R].lx; R=t[R].lx;
		ans_y[len]=t[R].ly; c=t[c].ly;
	}
	for(int i=len;i>0;--i){
		cout<<ans_x[i]<<" "<<ans_y[i]<<endl;
	}
//	cout<<endl<<t[R].lx<<" "<<t[c].ly;
	return 0;
}
2022/3/3 00:04
加载中...