[Ybtoj High-efficiency Advanced 1.5] [Wide Search] Walk the maze

[Ybtoj High-efficiency Advanced 1.5] [Wide Search] Walk the maze

topic

Insert picture description here


Problem-solving ideas

Guangsou template has
nothing to say


Code

#include<iostream>
#include<cstdio>
using namespace std;
struct lzf{
    
    
	int x,y;
}q[1000020];
char a[1005][1005];
int n,t,h,qx,qy,zx,zy;
int p[1005][1005],f[1005][1005];
int fx[5]={
    
    0,1,0,-1,0},fy[5]={
    
    0,0,1,0,-1};
int main()
{
    
    
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	    for (int j=1;j<=n;j++)
            cin>>a[i][j];
	scanf("%d%d%d%d",&qx,&qy,&zx,&zy);
	q[1].x=qx;
	q[1].y=qy;
	t=1;
	p[qx][qy]=1;
	do{
    
    
		h++;
		for (int i=1;i<=4;i++)
		{
    
    
		    int xx=q[h].x+fx[i],yy=q[h].y+fy[i];
		    if (!p[xx][yy]&&xx>0&&xx<=n&&yy>0&&yy<=n&&a[xx][yy]=='0')  //是否能走
		    {
    
    
		       f[xx][yy]=f[q[h].x][q[h].y]+1;
		       if (xx==zx&&yy==zy)  //到终点啦
		       {
    
    
		       	  printf("%d\n",f[xx][yy]);
		       	  return 0;
			   }
		       p[xx][yy]=1;  //标记
		       q[++t].x=xx;
		       q[t].y=yy;  //入队
			}
		}
	}while (h<t);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/112132283