BFS 80pts
  • 板块P2802 回家
  • 楼主Zouzhuoxuan
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/2/25 09:25
  • 上次更新2023/10/23 23:53:30
查看原帖
BFS 80pts
800322
Zouzhuoxuan楼主2023/2/25 09:25

MLE on #1#4

#include<bits/stdc++.h>
#include<queue>
using namespace std;
struct inf
{
   int x,y,b,s;
}t;
queue<inf>q;
int n,m,a[10][10],px,py,sx,sy,fx,fy,dx[]={0,-1,0,1,0},dy[]={0,0,-1,0,1};
void bfs()
{
   while(!q.empty()) q.pop();
   q.push(inf{sx,sy,6,0});
   while(!q.empty())
   {
   	t=q.front(),q.pop();
   	if(!t.b) continue;
   	if(a[t.x][t.y]==4) t.b=6;
   	if(t.x==fx and t.y==fy)
   	{
   		printf("%d\n",t.s);
   		exit(0);
   	}
   	for(int i=1;i<=4;i++)
   	{
   		px=t.x+dx[i],py=t.y+dy[i];
   		if(px<=0 or px>n or py<=0 or py>m or !a[px][py]) continue;
   		q.push(inf{px,py,t.b-1,t.s+1}); 
   	}
   }
   printf("-1\n");
   exit(0);
}
int main()
{
   int i,j;
   scanf("%d%d",&n,&m);
   for(i=1;i<=n;i++) for(j=1;j<=m;j++)
   {
   	scanf("%d",&a[i][j]);
   	if(a[i][j]==2) sx=i,sy=j,a[i][j]=1;
   	else if(a[i][j]==3) fx=i,fy=j,a[i][j]=1;
   }
   bfs();
}
2023/2/25 09:25
加载中...